HOME

TheInfoList



OR:

The median filter is a non-linear
digital filter In signal processing, a digital filter is a system that performs mathematical operations on a Sampling (signal processing), sampled, discrete-time signal to reduce or enhance certain aspects of that signal. This is in contrast to the other ma ...
ing technique, often used to remove
noise Noise is sound, chiefly unwanted, unintentional, or harmful sound considered unpleasant, loud, or disruptive to mental or hearing faculties. From a physics standpoint, there is no distinction between noise and desired sound, as both are vibrat ...
from an image, signal, and video. 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 u ...
is a typical pre-processing step to improve the results of later processing (for example,
edge detection Edge or EDGE may refer to: Technology Computing * Edge computing, a network load-balancing system * Edge device, an entry point to a computer network * Adobe Edge, a graphical development application * Microsoft Edge, a web browser developed b ...
on an image). Median filtering is very widely used in digital
image processing An image or picture is a visual representation. An image can be two-dimensional, such as a drawing, painting, or photograph, or three-dimensional, such as a carving or sculpture. Images may be displayed through other media, including a pr ...
because, under certain conditions, it preserves edges while removing noise (but see the discussion below for which kinds of noise), 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, Scalar potential, potential fields, Seismic tomograph ...
.


Algorithm description

The main idea of the median filter is to run through the signal entry by entry, replacing each entry with the
median The median of a set of numbers is the value separating the higher half from the lower half of a Sample (statistics), data sample, a statistical population, population, or a probability distribution. For a data set, it may be thought of as the “ ...
of the entry and its neighboring entries. The idea is very similar to a
moving average In statistics, a moving average (rolling average or running average or moving mean or rolling mean) is a calculation to analyze data points by creating a series of averages of different selections of the full data set. Variations include: #Simpl ...
filter, which replaces each entry with the
arithmetic mean In mathematics and statistics, the arithmetic mean ( ), arithmetic average, or just the ''mean'' or ''average'' is the sum of a collection of numbers divided by the count of numbers in the collection. The collection is often a set of results fr ...
of the entry and its neighbors. 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 can be obtained from a sphere by deforming it by means of directional Scaling (geometry), scalings, or more generally, of an affine transformation. An ellipsoid is a quadric surface;  that is, a Surface (mathemat ...
al or rectangular region (i.e., the median filter is not a
separable filter A separable filter in image processing can be written as product of two more simple filters. Typically a 2-dimensional convolution operation is separated into two 1-dimensional filters. This reduces the computational costs on an N\times M image wit ...
).


Worked one-dimensional example

To demonstrate, using a window size of three with one entry immediately preceding and following each entry, and zero-padded boundaries, a median filter will be applied to the following simple one-dimensional signal: : ''x'' = (2, 3, 80, 6, 2, 3). This signal has mainly small valued entries, except for one entry that is unusually high and considered to be a noise spike, and the aim is to eliminate it. So, the median filtered output signal ''y'' will be: : ''y''0 = med(0, 2, 3) = 2, (the boundary value is taken to be 0) : ''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, : ''y''5 = med(2, 3, 0) = med(0, 2, 3) = 2, i.e., :''y'' = (2, 3, 6, 6, 3, 2). It is clear that the noise spike has been essentially eliminated (and the signal has also been smoothed a bit). The result of a
moving average In statistics, a moving average (rolling average or running average or moving mean or rolling mean) is a calculation to analyze data points by creating a series of averages of different selections of the full data set. Variations include: #Simpl ...
filter with the same window width on the same dataset would be ''y'' = (1.7, 28.3, 29.7, 29.3, 3.7, 1.7). It can be seen that the noise spike has infected neighbouring elements in the moving average signal, and that the median filter has performed much better (for this type of impulse noise). Median filtering works well for both positive impulses (spikes) and negative impulses (dropouts), so long as a window can be chosen so that the number of entries infected with impulse noise is (almost) always smaller than half of the window size.


Boundary issues

When implementing a median filter, the boundaries of the signal must be handled with special care, as there are not enough entries to fill an entire window. There are several schemes that have different properties that might be preferred in particular circumstances: * When calculating the median of a value near the boundary, missing values are filled by repeating the boundary value to obtain enough entries to fill the window. * Avoid processing the boundaries, with or without cropping the signal or image boundary afterwards, * Fetching entries from other places in the signal such as values from the far ends (repeating boundary conditions) or reversing the signal (reflected boundary conditions). With 2D images for example, entries from the far horizontal or vertical boundary might be selected, or repeating in reverse order the points at the same boundary * Shrinking the window near the boundaries, so that every window is full, * Assuming zero-padded boundaries.


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 ...
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 kth smallest value in a collection of ordered values, such as numbers. The value that it finds is called the order statistic. Selection includes as special cases the p ...
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 a visual representation of the frequency distribution, distribution of quantitative data. To construct a histogram, the first step is to Data binning, "bin" (or "bucket") the range of values— divide the entire range of values in ...
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.


Worked two-dimensional example

The median filter operates by considering a local window (also known as a kernel) around each pixel in the image. The steps for applying the median filter are as follows: # Window Selection: #* Choose a window of a specific size (e.g., 3x3, 5x5) centered around the pixel to be filtered. #* For our example, let’s use a 3x3 window. # Collect Pixel Values: #* Collect the pixel values within the window. #* For the center pixel, we have the following values: #** Window: \begin 1 & 2 & 3 \\ 4 & 8 & 6 \\ 7 & 5 & 9 \end #** Center pixel: 8 # Sort the Values: #* Sort the collected pixel values in ascending order. #* For the center pixel, the sorted values are: , 2, 3, 4, 5, 6, 7, 8, 9# Choose the Median Value: #* The median value is the middle value in the sorted list. #* In our case, the median value is 5. # Replace the Center Pixel: #* Replace the original center pixel value (8) with the median value (5). # Repeat for All Pixels: #* Repeat steps 2-5 for all pixels in the image.


Converted Image

After applying the median filter to all pixels, the converted image becomes: \begin 2 & 3 & 3 \\ 4 & 5 & 6 \\ 7 & 7 & 8 \end This filtered image effectively removes noisy pixels while preserving important features. Remember that we assumed virtual rows and columns with repeated border pixel values to handle the edge pixels.


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 and
salt-and-pepper noise Salt-and-pepper noise, also known as impulse noise, is a form of noise sometimes seen on digital images. For black-and-white or grayscale images, it presents as sparsely occurring white and black pixels, giving the appearance of an image sprinkl ...
(impulsive noise), it is particularly effective. Because of this, median filtering is very widely used in digital
image processing An image or picture is a visual representation. An image can be two-dimensional, such as a drawing, painting, or photograph, or three-dimensional, such as a carving or sculpture. Images may be displayed through other media, including a pr ...
.


See also

* Edge-preserving filtering *
Image noise Image noise is random variation of brightness or color information in images. It can originate in film grain and in the unavoidable shot noise of an ideal photon detector. In digital photography is usually an aspect of electronic noise, produ ...
* Weighted median * pseudo-median filter * Lulu smoothing *
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 Gaussia ...
* Average with limited data validity *
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 d ...
*
Sharpening Sharpening is the process of creating or refining a blade, the edge joining two non-coplanar faces into a converging apex, thereby creating an edge of appropriate shape on a tool or implement designed for cutting. Improving sharpness is don ...
*
Unsharp masking Unsharp masking (USM) is an image sharpening technique, first implemented in darkroom photography, but now commonly used in digital image processing software. Its name derives from the fact that the technique uses a blurred, or "unsharp", negat ...
*
High-pass filter A high-pass filter (HPF) is an electronic filter that passes signals with a frequency higher than a certain cutoff frequency and attenuates signals with frequencies lower than the cutoff frequency. The amount of attenuation for each frequency ...


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 2^n, 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 chrestomathy website with implementations of common algorithms and solutions to various computer programming, programming problems in many different programming languages. It is named for the Rosetta Stone ...
)
Dr Dobbs article

Circle median filter
Median 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