skimage2.util.random_noise#
- skimage2.util.random_noise(image, mode='gaussian', rng=None, clip=True, **kwargs)[source]#
Function to add random noise of various types to a floating-point image.
- Parameters:
- imagendarray
Input image data. Will be converted to float.
- modestr, optional
One of the following strings, selecting the type of noise to add:
- âgaussianâ (default)
Gaussian-distributed additive noise.
- âlocalvarâ
Gaussian-distributed additive noise, with specified local variance at each point of
image.- âpoissonâ
Poisson-distributed noise generated from the data.
- âsaltâ
Replaces random pixels with 1.
- âpepperâ
Replaces random pixels with 0 (for unsigned images) or -1 (for signed images).
- âs&pâ
Replaces random pixels with either 1 or
low_val, wherelow_valis 0 for unsigned images or -1 for signed images.- âspeckleâ
Multiplicative noise using
out = image + n * image, wherenis Gaussian noise with specified mean & variance.
- rng{
numpy.random.Generator, int}, optional Pseudo-random number generator. By default, a PCG64 generator is used (see
numpy.random.default_rng()). Ifrngis an int, it is used to seed the generator.- clipbool, optional
If True (default), the output will be clipped after noise is applied. This may be needed to maintain the proper image data range. If False, clipping is not applied, and the output may extend beyond the range [-1, 1].
- meanfloat, optional
Mean of random distribution. Used in âgaussianâ and âspeckleâ. Default : 0.
- varfloat, optional
Variance of random distribution. Used in âgaussianâ and âspeckleâ. Note: variance = (standard deviation) ** 2. Default : 0.01
- local_varsndarray, optional
Array of positive floats, same shape as
image, defining the local variance at every image point. Used in âlocalvarâ.- amountfloat, optional
Proportion of image pixels to replace with noise on range [0, 1]. Used in âsaltâ, âpepperâ, and âsalt & pepperâ. Default : 0.05
- salt_vs_pepperfloat, optional
Proportion of salt vs. pepper noise for âs&pâ on range [0, 1]. Higher values represent more salt. Default : 0.5 (equal amounts)
- Returns:
- outndarray
Output floating-point image data on range [0, 1] or [-1, 1] if the input
imagewas unsigned or signed, respectively.
Notes
Speckle, Poisson, Localvar, and Gaussian noise may generate noise outside the valid image range. The default is to clip (not alias) these values, but they may be preserved by setting
clip=False. Note that in this case the output may contain values outside the ranges [0, 1] or [-1, 1]. Use this option with care.Because of the prevalence of exclusively positive floating-point images in intermediate calculations, it is not possible to intuit if an input is signed based on dtype alone. Instead, negative values are explicitly searched for. Only if found does this function assume signed input. Unexpected results only occur in rare, poorly exposes cases (e.g. if all values are above 50 percent gray in a signed
image). In this event, manually scaling the input to the positive domain will solve the problem.The Poisson distribution is only defined for positive integers. To apply this noise type, the number of unique values in the image is found and the next round power of two is used to scale up the floating-point result, after which it is scaled back down to the floating-point image range.
To generate Poisson noise against a signed image, the signed image is temporarily converted to an unsigned image in the floating point domain, Poisson noise is generated, then it is returned to the original range.