SaltAndPepper

Targets:
image
volume
Image Types:uint8, float32

Apply salt and pepper noise to the input image.

Salt and pepper noise is a form of impulse noise that randomly sets pixels to either maximum value (salt) or minimum value (pepper). The amount and proportion of salt vs pepper can be controlled. The same noise mask is applied to all channels of the image to preserve color consistency.

Arguments
amount
tuple[float, float]
[0.01,0.06]

Range for total amount of noise (both salt and pepper). Values between 0 and 1. For example:

  • 0.05 means 5% of all pixels will be replaced with noise
  • (0.01, 0.06) will sample amount uniformly from 1% to 6% Default: (0.01, 0.06)
salt_vs_pepper
tuple[float, float]
[0.4,0.6]

Range for ratio of salt (white) vs pepper (black) noise. Values between 0 and 1. For example:

  • 0.5 means equal amounts of salt and pepper
  • 0.7 means 70% of noisy pixels will be salt, 30% pepper
  • (0.4, 0.6) will sample ratio uniformly from 40% to 60% Default: (0.4, 0.6)
p
float
0.5

Probability of applying the transform. Default: 0.5.

Examples
>>> import albumentations as A
>>> import numpy as np

# Apply salt and pepper noise with default parameters
>>> transform = A.SaltAndPepper(p=1.0)
>>> noisy_image = transform(image=image)["image"]

# Heavy noise with more salt than pepper
>>> transform = A.SaltAndPepper(
...     amount=(0.1, 0.2),       # 10-20% of pixels will be noisy
...     salt_vs_pepper=(0.7, 0.9),  # 70-90% of noise will be salt
...     p=1.0
... )
>>> noisy_image = transform(image=image)["image"]
Notes
  • Salt noise sets pixels to maximum value (255 for uint8, 1.0 for float32)
  • Pepper noise sets pixels to 0
  • The noise mask is generated once and applied to all channels to maintain color consistency (i.e., if a pixel is set to salt, all its color channels will be set to maximum value)
  • The exact number of affected pixels matches the specified amount as masks are generated without overlap
See Also
  • GaussNoise: For additive Gaussian noise
  • MultiplicativeNoise: For multiplicative noise
  • ISONoise: For camera sensor noise simulation