The median filter is a non-linear
digital filter
In signal processing, a digital filter is a system that performs mathematical operations on a sampled, discrete-time signal to reduce or enhance certain aspects of that signal. This is in contrast to the other major type of electronic filter, t ...
ing technique, often used to remove
noise
Noise is unwanted sound considered unpleasant, loud or disruptive to hearing. From a physics standpoint, there is no distinction between noise and desired sound, as both are vibrations through a medium, such as air or water. The difference arise ...
from an image or signal. Such
noise reduction
Noise reduction is the process of removing noise from a signal. Noise reduction techniques exist for audio and images. Noise reduction algorithms may distort the signal to some degree. Noise rejection is the ability of a circuit to isolate an und ...
is a typical pre-processing step to improve the results of later processing (for example,
edge detection
Edge detection includes a variety of mathematical methods that aim at identifying edges, curves in a digital image at which the image brightness changes sharply or, more formally, has discontinuities. The same problem of finding discontinuitie ...
on an image). Median filtering is very widely used in digital
image processing
An image is a visual representation of something. It can be two-dimensional, three-dimensional, or somehow otherwise feed into the visual system to convey information. An image can be an artifact, such as a photograph or other two-dimensiona ...
because, under certain conditions, it preserves edges while removing noise (but see the discussion below), also having applications in
signal processing
Signal processing is an electrical engineering subfield that focuses on analyzing, modifying and synthesizing ''signals'', such as audio signal processing, sound, image processing, images, and scientific measurements. Signal processing techniq ...
.
Algorithm description
The main idea of the median filter is to run through the signal entry by entry, replacing each entry with the
median
In statistics and probability theory, the median is the value separating the higher half from the lower half of a data sample, a population, or a probability distribution. For a data set, it may be thought of as "the middle" value. The basic fe ...
of neighboring entries. The pattern of neighbors is called the "window", which slides, entry by entry, over the entire signal. For one-dimensional signals, the most obvious window is just the first few preceding and following entries, whereas for two-dimensional (or higher-dimensional) data the window must include all entries within a given radius or
ellipsoid
An ellipsoid is a surface that may be obtained from a sphere by deforming it by means of directional scalings, or more generally, of an affine transformation.
An ellipsoid is a quadric surface; that is, a surface that may be defined as the ...
al region (i.e. the median filter is not a
separable filter
Separability may refer to:
Mathematics
* Separable algebra, a generalization to associative algebras of the notion of a separable field extension
* Separable differential equation, in which separation of variables is achieved by various means
...
).
Worked one-dimensional example
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 one-dimensional signal:
: ''x'' = (2, 3, 80, 6, 2, 3).
So, the median filtered output signal ''y'' will be:
: ''y''
1 = med(2, 3, 80) = 3, (already 2, 3, and 80 are in the increasing order so no need to arrange them)
: ''y''
2 = med(3, 80, 6) = med(3, 6, 80) = 6, (3, 80, and 6 are rearranged to find the median)
: ''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
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.
Two-dimensional median filter pseudo code
Code for a simple two-dimensional median filter algorithm might look like this:
1. allocate outputPixelValue
mage width
Mage most commonly refers to:
* Mage (paranormal) or magician, a practitioner of magic derived from supernatural or occult sources
* Mage (fantasy) or magician, a type of character in mythology, folklore, and fiction
*Mage, a character class in so ...
image height]
2. allocate window
indow width × window height 3. edgex := (window width / 2) rounded down
4. edgey := (window height / 2) rounded down
for ''x'' from edgex to image width - edgex do
for ''y'' from edgey to image height - edgey do
i = 0
for ''fx'' from 0 to window width do
for ''fy'' from 0 to window height do
window
:= inputPixelValue
+ fx - edgexy + fy - edgey]
i := i + 1
sort entries in window[]
outputPixelValue[x][y] := window[window width * window height / 2]
This algorithm:
* Processes one color channel only,
* Takes the "not processing boundaries" approach (see above discussion about boundary issues).
Algorithm implementation issues
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 algorithm
In computer science, a selection algorithm is an algorithm for finding the ''k''th smallest number in a list or array; such a number is called the ''k''th ''order statistic''. This includes the cases of finding the minimum, maximum, and median e ...
s can be much more efficient. Furthermore, some types of signals (very often the case for images) use whole number representations: in these cases,
histogram
A histogram is an approximate representation of the distribution of numerical data. The term was first introduced by Karl Pearson. To construct a histogram, the first step is to " bin" (or "bucket") the range of values—that is, divide the ent ...
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.
Edge preservation 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
In image processing, a Gaussian blur (also known as Gaussian smoothing) is the result of blurring an image by a Gaussian function (named after mathematician and scientist Carl Friedrich Gauss).
It is a widely used effect in graphics software, ...
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
Speckle, speckle pattern, or speckle noise is a granular noise texture degrading the quality as a consequence of interference among wavefronts in coherent imaging systems, such as radar, synthetic aperture radar (SAR), medical ultrasound and op ...
and
salt-and-pepper noise
Salt-and-pepper noise, also known as impulse noise, is a form of noise sometimes seen on digital images. This noise can be caused by sharp and sudden disturbances in the image signal. It presents itself as sparsely occurring white and black pix ...
(impulsive noise), it is particularly effective.
Because of this, median filtering is very widely used in digital
image processing
An image is a visual representation of something. It can be two-dimensional, three-dimensional, or somehow otherwise feed into the visual system to convey information. An image can be an artifact, such as a photograph or other two-dimensiona ...
.
See also
*
Edge-preserving filtering Edge-preserving smoothing or edge-preserving filtering is an image processing technique that smooths away noise or textures while retaining sharp edges. Examples are the median filter, median, bilateral filter, bilateral, guided filter, guided, anis ...
*
Image noise
Image noise is random variation of brightness or color information in images, and is usually an aspect of electronic noise. It can be produced by the image sensor and circuitry of a Image scanner, scanner or digital camera. Image noise can also ...
*
Weighted median
In statistics, a weighted median of a sample is the 50% weighted percentile. It was first proposed by F. Y. Edgeworth in 1888. Like the median, it is useful as an estimator of central tendency, robust against outliers. It allows for non-unifor ...
*
pseudo-median filter
*
Lulu smoothing In signal processing, Lulu smoothing is a nonlinear mathematical technique for removing impulsive Noise (signal processing), noise from a data sequence such as a time series. It is a nonlinear equivalent to taking a moving average (or other smoothin ...
*
Bilateral filter
A bilateral filter is a non-linear, edge-preserving, and noise-reducing smoothing filter for images. It replaces the intensity of each pixel with a weighted average of intensity values from nearby pixels. This weight can be based on a Gaussian d ...
*
Average with limited data validity In image analysis, the average with limited data validity is an image filter for feature-preserving noise removal, consisting in a smoothing filter that only involves pixels satisfying some validity criterion. If some feature of noise elements is k ...
*
Smoothing
In statistics and image processing, to smooth a data set is to create an approximating function that attempts to capture important patterns in the data, while leaving out noise or other fine-scale structures/rapid phenomena. In smoothing, the dat ...
References
External links
Fast MATLAB one-dimensional median filter implementation– 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
Rosetta Code is a wiki-based programming website with implementations of common algorithms and solutions to various programming problems in many different programming languages. It is named for the Rosetta Stone, which has the same text inscribe ...
)
Dr Dobbs articleCircle median filterMedian filter for circle-valued data such as phase or orientation images (C++/Matlab)
{{Noise, state=uncollapsed
Nonlinear filters
Signal processing
Image noise reduction techniques
Articles with example pseudocode