Image Processing & Filtering

median filter

The median filter replaces each pixel not with the average of its neighbours but with their median, the middle value once the neighbourhood is sorted. This one change from mean to median makes it a nonlinear filter with a remarkable property: it is robust to outliers. A few wildly wrong pixels cannot move the middle of a sorted list, so they are simply ignored rather than smeared around.

This is exactly why the median filter is the standard cure for salt-and-pepper (impulse) noise, where scattered pixels are stuck at pure black or pure white. Take a 3-by-3 window: sort its nine values and output the fifth. A lone white-hot speck sits at the top of the sorted list and never becomes the median, so it vanishes, while genuine image structure (where most neighbours agree) is preserved. Equally important, the median filter preserves sharp step edges far better than any linear blur, because along a clean edge the majority value still wins, so the boundary stays crisp instead of being averaged into a ramp.

Because it requires sorting rather than a weighted sum, the median filter is not a convolution and obeys none of the linear-system theory; there is no frequency response in the usual sense. Naive implementations sort every window, but constant-time-per-pixel algorithms exist, Huang's running histogram that updates as the window slides, and Perreault's O(1) constant-time median filter. Its weaknesses: it is not optimal for Gaussian (additive) noise where averaging wins, and large windows round off sharp corners, erase thin lines, and remove small features. Variants such as the weighted median, adaptive median, and vector median (for colour) address specific shortcomings.

On a face photo corrupted with 10% salt-and-pepper noise, a 3x3 median filter removes virtually every black and white speck while leaving the eyelashes and lip edges sharp; a same-size mean filter leaves grey ghosts of every speck and softens the whole face.

Why it matters: the mean-versus-median choice is the classic robustness lesson of image processing. Use the median when noise is impulsive (outliers) and edges must stay sharp; use averaging/Gaussian when noise is Gaussian and mild blurring is acceptable.

Also called
rank/order-statistic filtermedian smoothing