Median Filter

The median filter replaces each pixel with the median of the intensities in a window around it. For a window, the output is

where is the input image. The window values are sorted, and value number becomes the output. The filter applies the same rule at every pixel, so it is shift-invariant. It is nonlinear, because the median of a sum is not the sum of the medians. It is therefore not a convolution, and it has no kernel and no frequency response.

The median is robust to outliers. While fewer than half of the window values are outliers, the output stays between the smallest and largest clean values. This makes the median filter the standard fix for salt-and-pepper noise, in which random pixels are set to pure black or pure white. A Gaussian blur only spreads each such pixel into a gray smudge. For additive Gaussian noise, in which every pixel gets a small random error, a Gaussian or box blur of the same size removes more noise. The median filter keeps step edges sharp. It removes features narrower than half the window, such as one-pixel lines, and it rounds off sharp corners.

The mean of a set of numbers minimizes the sum of squared deviations (L2). The median minimizes the sum of absolute deviations (L1), which is why one extreme value cannot drag it far. In MATLAB the filter is medfilt2(image, [h w]), and in Python it is scipy.ndimage.median_filter(image, size).

Intuition

A linear filter lets every input pixel push the output in proportion to its value. One white pixel of value among dark neighbors adds to every output in its box neighborhood. Salt-and-pepper noise is made of such extreme pixels. A blur turns each speck into a smaller, wider smudge, and the specks survive even a Gaussian. The median depends only on the order of the values. Sorting pushes an extreme value to the end of the list, and the middle of the list does not move. The cost of this robustness is detail. Any structure that fills less than half the window is treated as an outlier and erased.

The diagram shows a row with one outlier and one step, filtered with width- windows. The median removes the outlier and keeps the step exactly. The mean spreads the outlier into a plateau and turns the step into a ramp.

\usepackage{tikz}
\begin{document}
\definecolor{inp}{HTML}{8A8A8A}
\definecolor{med}{HTML}{2F80ED}
\definecolor{avg}{HTML}{D9483B}
\begin{tikzpicture}[x=0.75cm,y=0.022cm,font=\small]
% input row
\foreach \i/\v in {0/20,1/20,2/20,3/90,4/20,5/20,6/20,7/20,8/60,9/60,10/60,11/60} \fill[inp] (\i,\v) circle (2.5pt);
\node[right] at (12,50) {input $f$};
\draw[inp] (-0.5,0) -- (11.5,0);
% median row
\begin{scope}[yshift=-3.4cm]
\draw[inp] (-0.5,0) -- (11.5,0);
\foreach \i/\v in {2/20,3/20,4/20,5/20,6/20,7/20,8/60,9/60} \fill[med] (\i,\v) circle (2.5pt);
\node[right] at (12,50) {median, width $5$};
\end{scope}
% mean row
\begin{scope}[yshift=-6.8cm]
\draw[inp] (-0.5,0) -- (11.5,0);
\foreach \i/\v in {2/34,3/34,4/34,5/34,6/28,7/36,8/44,9/52} \fill[avg] (\i,\v) circle (2.5pt);
\node[right] at (12,50) {mean (box), width $5$};
\foreach \i in {0,...,11} \node[inp,below] at (\i,-2) {\i};
\node[inp,left] at (-0.6,-14) {pixel $i$};
\end{scope}
\end{tikzpicture}
\end{document}

Properties

(Breakdown Point) The output is corrupted only when at least half of the window holds outliers of the same kind.

In sorted order, pepper pixels () sit at the bottom of the list and salt pixels () at the top. The middle value of sorted values is pepper only if at least of them are pepper. The same count of salt pixels is needed for a salt output. Salt and pepper in the same window push from opposite ends and partly cancel. If a fraction of pixels is corrupted, half salt and half pepper, the probability that a output is corrupted is . A box or Gaussian blur has no such threshold. Any corrupted pixel in the window shifts its output.

(Edge Preservation) A median filter leaves any monotonic 1D signal unchanged away from the borders.

In a window of increasing values, the middle sample is the center sample, so ramps and steps pass through untouched. A width- mean turns a step into a ramp pixels long. In 2D, a straight edge between two flat regions survives, because more than half of each window lies on the center pixel’s side. Corners do not survive. At the corner pixel of a bright square, a window holds only bright values out of , so the corner turns dark. An outlier next to an edge can also move the edge by one pixel, because it changes the count on one side of the window.

(Nonlinear but Order-Preserving) The median filter is not linear, but it commutes with every increasing point operation.

The medians of and are both , while the median of their sum is . Linearity fails. For any increasing function , the median of equals applied to the median of , because does not change the sorted order. A special case is for . A gamma or contrast curve from point processing therefore gives the same result before or after median filtering.

(Gaussian Noise) For Gaussian noise the median wastes information, so it leaves more noise than a mean of the same size.

For independent samples with standard deviation , the mean has variance . The median has variance close to for large . A simulation with gives about , so a median leaves about more noise standard deviation than a box. The lecture’s comparison follows from this. A Gaussian blur suits Gaussian noise, and a median filter suits salt-and-pepper noise.

(Cost and Separability) The median filter sorts values per pixel and does not split into row and column passes.

Sorting costs per pixel, so a window sorts values at every pixel. A Gaussian blur can run as two 1D passes, but a median cannot. In the window the true median is , while the median of the row medians is . The filter also has no frequency response, so the FFT cannot speed it up.

Examples

(Easy) One Hot Pixel

A dead sensor pixel reads in a region whose true values are near . Its neighborhood is

(Medium) An Outlier and an Edge in One Row

A row crosses a speck of dust and then the border of a bright object:

indexed to . It is filtered with width- windows in valid mode, at .

(Hard) The Noise Density a Median Can Handle

A scanned document has salt-and-pepper noise at density : each pixel is independently set to with probability and to with probability .

See Also