Add salt-and-pepper noise¶
| Fact | Value |
|---|---|
| Also called | Impulse noise, Salt and pepper noise |
| Status | Implemented and tested |
| Family | Utilities |
| Engine | Browser |
| Type | Nonlinear, statistical, random impulse replacement |
| Output | Image |
| Operator shown as | random-process (exact) |
| Support radius | 0 |
| Code | web/src/core/filters/utility/noise.ts, saltPepperNoiseFilter |
| Operator code | web/src/core/math/random.ts, positionUniform |
| Channels | RGB |
Overview¶
This tool turns a random share of the pixels pure white or pure black. It makes test images with impulse noise, the kind that the median filter removes well.
How it works¶
- For each pixel, pick a random number between 0 and 1.
- If it is below the amount, the pixel becomes noise.
- Pick a second number to choose white or black.
- All other pixels stay as they are.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input pixel
- \(I_{\text{out}}\): Output pixel
- \(u, v\): Uniform random numbers between 0 and 1 for this pixel
- \(p\): Amount, the probability that a pixel becomes noise
- \(s\): Salt share, the probability that a noisy pixel is white
Two uniform random numbers u and v between 0 and 1 are made for each pixel from the seed and the position. If u is below the amount p, the pixel becomes noise: white when v is below the salt share s, black otherwise. All three channels change together.
Each pixel becomes noise with probability p, the amount, independently of the others. A noisy pixel is white with probability s, the salt share, and black otherwise. The noise replaces the value, so a noisy pixel carries no information about the true value.
Kernel or operator¶
The Inspector shows the probabilities and the seed rule. The two uniform numbers depend only on the seed and the pixel position.
Algorithm¶
- For each pixel, hash the seed and the position into two uniform numbers u and v.
- If u is at least the amount, keep the pixel.
- Otherwise set red, green and blue to 255 when v is below the salt share, and to 0 when it is not.
- Keep alpha unchanged.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Amount amount |
0.05 | 0 to 1 | The share of pixels that become white or black. |
Salt share salt |
0.5 | 0 to 1 | Of the noisy pixels, the share that becomes white. The rest become black. |
Seed seed |
1 | 0 to 999999 | The starting number of the random generator. |
Implementation¶
positionUniform makes the two numbers for each pixel. All three channels of a noisy pixel get the same value, so the dots are white or black, not colored. 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¶
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| u | 0.169 | 0.915 | 0.826 | 0.861 | 0.827 | 0.725 | 0.206 | 0.731 |
| v | 0.519 | 0.987 | 0.212 | 0.113 | 0.461 | 0.503 | 0.383 | 0.353 |
| Result | black | kept | kept | kept | kept | kept | kept | kept |
The first eight pixels of row 0 for seed 1, amount 0.2 and salt share 0.5. A pixel becomes noise when u is below 0.2. Then v below 0.5 makes it white.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels.
Artifacts and limitations¶
Limitations
- Real impulse noise, such as dead pixels, sits at fixed positions and may not be pure white or black.
Artifacts
- Isolated white and black dots.
Tests¶
One test checks that the share of noisy pixels is within 0.01 of the amount. Another checks that the share of white dots is within 0.05 of the salt share. A third checks that any region matches the whole image.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 5.
- Wellons, C. Prospecting for hash functions, 2018. The source of the integer hash. https://nullprogram.com/blog/2018/07/31/