Median filter

From Wikipedia, the free encyclopedia

Example of 3 median filters of varying radii applied to the same noisy photograph. Implemented in Adobe Photoshop.
Example of 3 median filters of varying radii applied to the same noisy photograph. Implemented in Adobe Photoshop.

In image processing it is usually necessary to perform high degree of noise reduction in an image before performing higher-level processing steps, such as edge detection. The median filter is a non-linear digital filtering technique, often used to remove noise from images or other signals. The idea is to examine a sample of the input and decide if it is representative of the signal. This is performed using a window consisting of an odd number of samples. The values in the window are sorted into numerical order; the median value, the sample in the center of the window, is selected as the output. The oldest sample is discarded, a new sample acquired, and the calculation repeats.

Median filtering is a common step in image processing. It is particularly useful to reduce speckle noise and salt and pepper noise. Its edge-preserving nature makes it useful in cases where edge blurring is undesirable.

Contents

[edit] Example

To demonstrate, the median filter will be applied to the following array with a window size of 3, repeating edge values:

x = [2 80 6 3]

y[1] = Median[2 2 80] = 2
y[2] = Median[2 80 6] = Median[2 6 80] = 6
y[3] = Median[80 6 3] = Median[3 6 80] = 6
y[4] = Median[6 3 3] = Median[3 3 6] = 3

so
y = [2 6 6 3]

where y is the median filtered output of x

[edit] Common problems

A common problem with all filters based on all adjacent pixels is how to process the edges of the image. As the filter nears the edges, a median filter may not preserve its odd number of samples criteria. It is also more complex to write a filter that includes a method to specifically deal with the edges. Common solutions to the problem are:

  • Not processing edges, with or without a crop of the image edges afterwards.
  • Fetching pixels from other places in the image. Typically the other horizontal edge on horizontal edges, and the other vertical edge on vertical edges are fetched.
  • Making the filter process fewer pixels on the edges.
  • Comparing the filtered sample to the original sample to determine if that sample is an outlier before replacing it with the filtered one.

[edit] Pseudo code

A simple median filter may look like this:

   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:
           colorArray[window width][window height];
           for fx from 0 to window width:
               for fy from 0 to window height:
                   colorArray[fx][fy] := pixelvalue[x + fx - edgex][y + fy - edgey]
           Sort colorArray[][];
           pixelValue[x][y] := colorArray[window width / 2][window height / 2];

Notice that:

  • This filter only processes one color channel.
  • This filter takes a "Not processing edges" approach.
  • "Window" refers to the pixel area we are processing for each pixel and "image" refers to our actual image.
  • The algorithms described in External Links are much faster.

[edit] See also

[edit] External links

This applied mathematics-related article is a stub. You can help Wikipedia by expanding it.