I want to know about median filtering. Recently, I read that it is best for preserving edges. But how does it preserve edges? I need complete knowledge of median filters. Why it is non-linear filter and why it is best, compared to linear filter?
A linear filter is mathematically described by the convolution sum (for discrete signals) and the convolution integral for continuous signals. The median cannot be found using a linear function except in the trivial case where you have a discrete filter of size 1, which is why the median filter is non-linear.
Edge Preserving Properties.
Median filtering is one kind of smoothing technique, as is linear Gaussian filtering. All smoothing techniques are effective at removing noise in smooth patches or smooth regions of a signal, but adversely affect edges. Often though, at the same time as reducing the noise in a signal, it is important to preserve the edges. Edges are of critical importance to the visual appearance of images, for example. For small to moderate levels of (Gaussian) noise, the median filter is demonstrably better than Gaussian blur at removing noise whilst preserving edges for a given, fixed window size. However, its performance is not that much better than Gaussian blur for high levels of noise, whereas, for speckle noise and salt and pepper noise (impulsive noise), it is particularly effective. Because of this, median filtering is very widely used in digital image processing.
A median filter changes the value of one given pixel by the median value of a patch of pixels (most often around the given pixel). Generally, the patch contains an odd number of pixels. I will details three basic scenarii:
- clean edge (1D vision): suppose that the image is all black on the left ($0$ value), white on the right ($255$ value), a clear vertical edge. If you take a line, across the edge, the values will be $(\ldots,0,0,0,255,255,255\ldots)$. A 3-point median does a perfect job: the patch $(0,0,255)$ yields $0$, the patch $(0,255,255)$ yields $255$. Now look across columns: they are constant, so the output of a A 3-point median yelds the same constant. This also works with any $2m+1\times 2n+1$ patch. So the clean edge is fully preserved, while a linear filter (non-trivial) would average values, and yields non integer pixels, introducing additional rounding errors
- pure flat+gaussian noise: the median is a robust estimator of the flat value, and can reduce the variance of the noise (I can add references if needed)
- pure flat (or pure noise)+ isolated outliers: depending on the number of outliers and the size of patch, the outliers are strongly shrunk toward the flat or a noise value.
It is non-linear, since the median of $(0,1,2)$ is $1$, the median of $(3,1,1)$ is $1$, but the median of $(0,1,2)+(3,1,1)$ is $3$, and not $1+1$.
So it can be better than linear filters, but this depends on the nature of edges, noise properties and patch size. Others sources on SE.DSP: