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?

  • $\begingroup$ try to delete it .but can't delete. $\endgroup$
    – sufi
    Commented Jul 15, 2017 at 14:10
  • $\begingroup$ hey sufi, you might want to look at this other answer. $\endgroup$ Commented Jul 17, 2017 at 7:13

2 Answers 2



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.

Source: Wikipedia

  • $\begingroup$ i want to how it preserve edge since edge and noise both have high frequency. ? $\endgroup$
    – sufi
    Commented Jul 11, 2017 at 17:13
  • 2
    $\begingroup$ It's not really useful to think of the spectral properties when performing non-linear filtering since non-linear filters by definition don't have a transfer function (which is based on the convolution sum). Median filtering can be thought of as a "despiker" function. I found pdfs.semanticscholar.org/1d86/… which describes this in more detail. While the term "low-pass" median filter is often used, its really a misnomer because median filters don't pass only low frequencies. $\endgroup$
    – jodag
    Commented Jul 11, 2017 at 17:58
  • $\begingroup$ i wonder how many pixels wide is a median filter used that preserves the edges? $\endgroup$ Commented Jul 17, 2017 at 7:22

A median filter changes the value of one given pixel by the median value (or a weighted version) of a patch of pixels (most often around the given pixel). Generally, the patch contains an odd number of pixels: $\pm K$ above and below, left and right, with a total of $(2K+1)^2$.

I will details three basic scenarios:

  • 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:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.