Add Gaussian noise¶
| Fact | Value |
|---|---|
| Also called | Gaussian noise, Additive white Gaussian noise |
| Status | Implemented and tested |
| Family | Utilities |
| Engine | Browser |
| Type | Nonlinear, statistical, additive random noise |
| Output | Image |
| Operator shown as | random-process (exact) |
| Support radius | 0 |
| Code | web/src/core/filters/utility/noise.ts, gaussianNoiseFilter |
| Operator code | web/src/core/math/random.ts, positionNormal |
| Channels | RGB |
Overview¶
This tool adds random values with a normal distribution to an image. Use it to make test images for denoising filters, with a noise level you know exactly.
How it works¶
- For each pixel and channel, pick a random number. Most numbers are near 0.
- Multiply it by sigma.
- Add it to the value, and keep the result between 0 and 255.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input channel value
- \(I_{\text{out}}\): Output channel value before clipping and rounding
- \((x, y, c)\): Pixel position and channel
- \(n\): A standard normal random number
- \(\sigma\): Standard deviation of the noise in 8-bit levels
Every pixel and channel gets a value drawn from a normal distribution with mean 0 and standard deviation sigma. The values come from two uniform numbers through the Box-Muller transform. The uniform numbers depend only on the seed, the position and the channel. So a region gives the same noise as the whole image, and the same seed always gives the same result. Results outside 0 to 255 are clipped.
The added values are independent and follow a normal distribution with mean 0 and standard deviation sigma. Each one comes from two uniform random numbers through the Box-Muller transform. Clipping at 0 and 255 changes the distribution for values near black and white.
Kernel or operator¶
The Inspector shows the distribution and the seed rule, not a kernel. The random numbers depend only on the seed, the pixel position and the channel. So a region gets the same noise as the whole image, and the same seed always repeats the same noise.
Algorithm¶
- For each pixel and channel, hash the seed, the position and the channel into two uniform numbers.
- Turn them into one standard normal number with the Box-Muller transform.
- Add sigma times that number to the value.
- Clip to 0 to 255 and round. Keep alpha unchanged.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Sigma sigma |
10 | 0 to 100 levels | Standard deviation of the noise, in 8-bit levels. |
Monochrome monochrome |
off | On or off | Add the same noise value to red, green and blue. |
Seed seed |
1 | 0 to 999999 | The starting number of the random generator. |
Implementation¶
positionNormal turns the seed, the position and the channel into a normal number. It hashes them with an integer hash by Chris Wellons and applies Box-Muller. The filter runs in the browser engine, in Web Workers. The studio splits the image into strips. Each strip gets a halo as wide as the support radius, so the strips give exactly the pixels of a whole-image run. The shared filter framework in web/src/core/filters/run.ts handles the border mode, the channel scope, alpha and the final rounding to 8 bits.
Worked example¶
For seed 1, the pixel at x 0, y 0 and the red channel get the normal number n = -1.8715. With sigma 10 on a flat gray of 128, the red value becomes 128 + 10 x -1.8715 = 109.29, which rounds to 109. The same seed and position always give the same n.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels.
Artifacts and limitations¶
Limitations
- Real camera noise depends on the brightness. This noise does not.
- Clipping at 0 and 255 changes the distribution near black and white.
Artifacts
- Colored speckles, unless Monochrome is on.
Tests¶
One test checks that the standard deviation on a flat image is within 0.3 of sigma. Others check that the same seed gives the same pixels, a different seed gives different pixels, and any region matches the whole image.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 5.
- Box, G. E. P. and Muller, M. E. A note on the generation of random normal deviates. The Annals of Mathematical Statistics, 29(2), 1958. DOI 10.1214/aoms/1177706645.
- Wellons, C. Prospecting for hash functions, 2018. The source of the integer hash. https://nullprogram.com/blog/2018/07/31/