True: salt-and-pepper noise consists of isolated pixels with extreme values (0 or 255); a median filter replaces each pixel by the middle value of its neighbourhood, so these extremes are thrown out completely while edges are preserved, whereas an averaging filter only spreads them out.
Example. A 3×3 neighbourhood in a smooth region where the centre pixel has been hit by "salt" noise:
12 10 11
13 255 12
11 14 10
Median filter: sort the 9 values:
10, 10, 11, 11, 12, 12, 13, 14, 255
The median (5th value) is 12, so the noisy pixel becomes 12, which matches its surroundings. The noise is removed completely.
Mean (averaging) filter:
\[\frac{12+10+11+13+255+12+11+14+10}{9} = \frac{348}{9} \approx 38.7\]
The pixel becomes about 39, still far brighter than its neighbours, and the same large value also pulls up the averages of the 8 neighbouring pixels, so the noise is smeared into a grey blob.
A "pepper" pixel (0) is handled the same way: it lands at the bottom of the sorted list and is never chosen as the median.
Reasons the median filter is best for this noise
- Impulse values are outliers; the median ignores outliers, while the mean is pulled strongly by them.
- The output is always one of the actual neighbourhood values, so no new, unrealistic grey levels are created.
- It preserves sharp edges better than averaging, which blurs them.
- It works well as long as fewer than half the pixels in the window are noisy; for heavy noise, a larger window or an adaptive median filter is used.