# Median filter

The **Median Filter** is a non-linear digital filtering technique, often used to remove noise from an image or signal. Such noise reduction is a typical pre-processing step to improve the results of later processing (for example, edge detection on an image). Median filtering is very widely used in digital image processing because, under certain conditions, it preserves edges while removing noise (but see discussion below), also having applications in signal processing.

## Contents

## Algorithm description[edit]

The main idea of the median filter is to run through the signal entry by entry, replacing each entry with the median of neighboring entries; the pattern of neighbors is called the "window", which slides, entry by entry, over the entire signal. For 1D signals, the most obvious window is just the first few preceding and following entries, whereas for 2D (or higher-dimensional) signals such as images, more complex window patterns are possible (such as "box" or "cross" patterns). Note that if the window has an odd number of entries, then the median is simple to define: it is just the middle value after all the entries in the window are sorted numerically. For an even number of entries, there is more than one possible median, see median for more details.

## Worked 1D example[edit]

To demonstrate, using a window size of three with one entry immediately preceding and following each entry, a median filter will be applied to the following simple 1D signal:

*x*= (2, 3, 80, 6).

So, the median filtered output signal *y* will be:

*y*_{1}= med(2, 3, 80) = 3,*y*_{2}= med(3, 80, 6) = med(3, 6, 80) = 6,*y*_{3}= med(80, 6, 2) = med(2, 6, 80) = 6,*y*_{4}= med(6, 2, 3) = med(2, 3, 6) = 3,

i.e. *y* = (3, 6, 6, 3).

## Boundary issues[edit]

Note that, in the example above, because there is no entry preceding the first value, the first value is repeated, as with the last value, to obtain enough entries to fill the window; this is one way of handling missing window entries at the boundaries of the signal, but there are other schemes that have different properties that might be preferred in particular circumstances:

- Avoid processing the boundaries, with or without cropping the signal or image boundary afterwards,
- Fetching entries from other places in the signal. With images for example, entries from the far horizontal or vertical boundary might be selected,
- Shrinking the window near the boundaries, so that every window is full.

## 2D median filter pseudo code[edit]

Code for a simple 2D median filter algorithm might look like this:

allocate outputPixelValue[image width][image height] allocate window[window width * window height] edgex := (window width / 2) rounded down edgey := (window height / 2) rounded down for x from edgex to image width - edgex for y from edgey to image height - edgey i = 0 for fx from 0 to window width for fy from 0 to window height window[i] := inputPixelValue[x + fx - edgex][y + fy - edgey] i := i + 1 sort entries in window[] outputPixelValue[x][y] := window[window width * window height / 2]

Note that this algorithm:

- Processes one color channel only,
- Takes the "not processing boundaries" approach (see above discussion about boundary issues).

## Algorithm implementation issues[edit]

Typically, by far the majority of the computational effort and time is spent on calculating the median of each window; because the filter must process every entry in the signal, for large signals such as images, the efficiency of this median calculation is a critical factor in determining how fast the algorithm can run. The naïve implementation described above sorts every entry in the window to find the median; however, since only the middle value in a list of numbers is required, selection algorithms can be much more efficient. Furthermore, some types of signals (very often the case for images) use whole number representations: in these cases, histogram medians can be far more efficient because it is simple to update the histogram from window to window, and finding the median of a histogram is not particularly onerous.^{[1]}

## Edge preservation properties[edit]

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.^{[2]} 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;^{[3]} because of this, median filtering is very widely used in digital image processing.

## See also[edit]

- Image noise
- Signal processing
- Gaussian blur
- Weighted median
- pseudo-median filter
- Lulu smoothing
- Bilateral filter
- Average with limited data validity
- Smoothing

## References[edit]

**^**Huang, Thomas S.; Yang, George J.; Tang, Gregory Y. (February 1979). "A fast two-dimensional median filtering algorithm" (PDF).*IEEE Transactions on Acoustics, Speech, and Signal Processing*.**27**(1): 13–18. doi:10.1109/TASSP.1979.1163188.**^**Arias-Castro, Ery; Donoho, David L. (June 2009). "Does median filtering truly preserve edges better than linear filtering?".*Annals of Statistics*.**37**(3): 1172–2009. arXiv:math/0612422. Bibcode:2006math.....12422A. doi:10.1214/08-AOS604. MR 2509071. Zbl 1160.62086.**^**Arce, Gonzalo R. (2005).*Nonlinear Signal Processing: A Statistical Approach*. New Jersey, USA: Wiley. ISBN 0-471-67624-1.

## External links[edit]

- Fast Matlab 1D median filter implementation
- Mathematica MedianFilter function
- Median filter
- Fast 2D median filter
- Implementation of 2D Median filter in constant time (GPL license) – the running time per pixel of this algorithm is proportional to the number of elements in a histogram (typically this is , where
*n*is the number of bits per channel), even though this in turn is a constant. - Implementation written in different programming languages (on Rosetta Code)
- Dr Dobbs article
- 100+ Times Faster Weighted Median Filter