Median filter

Last updated
Example of 3 median filters of varying radiuses applied to the same noisy photograph. Median filter example.jpg
Example of 3 median filters of varying radiuses applied to the same noisy photograph.

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 the discussion below for which kinds of noise), also having applications in signal processing.

Contents

Algorithm description

The main idea of the median filter is to run through the signal entry by entry, replacing each entry with the median of the entry and its neighboring entries. The idea is very similar to a moving average filter, which replaces each entry with the arithmetic mean 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 ellipsoidal or rectangular region (i.e., the median filter is not a separable filter).[ citation needed ]

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:

y0 = med(0, 2, 3) = 2, (the boundary value is taken to be 0)
y1 = med(2, 3, 80) = 3, (already 2, 3, and 80 are in the increasing order so no need to arrange them)
y2 = med(3, 80, 6) = med(3, 6, 80) = 6, (3, 80, and 6 are rearranged to find the median)
y3 = med(80, 6, 2) = med(2, 6, 80) = 6,
y4 = med(6, 2, 3) = med(2, 3, 6) = 3,
y5 = 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 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:

Two-dimensional median filter pseudo code

Code for a simple two-dimensional median filter algorithm might look like this:

1. allocate outputPixelValue[image width][image height] 2. allocate window[window width × window height] 3. edgex := (window width / 2) rounded down 4. edgey := (window height / 2) rounded down
forx from edgex to image width - edgex dofory from edgey to image height - edgey do         i = 0         forfx from 0 to window width doforfy from 0 to window height do                 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]

This algorithm:

Use of a median filter to improve an image severely corrupted by defective pixels Medianfilterp.png
Use of a median filter to improve an image severely corrupted by defective pixels

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 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]

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:

  1. 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.
  2. Collect Pixel Values:
    • Collect the pixel values within the window.
    • For the center pixel, we have the following values:
      • Window:
      • Center pixel: 8
  3. Sort the Values:
    • Sort the collected pixel values in ascending order.
    • For the center pixel, the sorted values are: [1, 2, 3, 4, 5, 6, 7, 8, 9]
  4. Choose the Median Value:
    • The median value is the middle value in the sorted list.
    • In our case, the median value is 5.
  5. Replace the Center Pixel:
    • Replace the original center pixel value (8) with the median value (5).
  6. 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:

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 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

Related Research Articles

Digital image processing is the use of a digital computer to process digital images through an algorithm. As a subcategory or field of digital signal processing, digital image processing has many advantages over analog image processing. It allows a much wider range of algorithms to be applied to the input data and can avoid problems such as the build-up of noise and distortion during processing. Since images are defined over two dimensions digital image processing may be modeled in the form of multidimensional systems. The generation and development of digital image processing are mainly affected by three factors: first, the development of computers; second, the development of mathematics ; third, the demand for a wide range of applications in environment, agriculture, military, industry and medical science has increased.

In digital signal processing, spatial anti-aliasing is a technique for minimizing the distortion artifacts (aliasing) when representing a high-resolution image at a lower resolution. Anti-aliasing is used in digital photography, computer graphics, digital audio, and many other applications.

In mathematics, a Gaussian function, often simply referred to as a Gaussian, is a function of the base form

Edge detection includes a variety of mathematical methods that aim at identifying edges, defined as curves in a digital image at which the image brightness changes sharply or, more formally, has discontinuities. The same problem of finding discontinuities in one-dimensional signals is known as step detection and the problem of finding signal discontinuities over time is known as change detection. Edge detection is a fundamental tool in image processing, machine vision and computer vision, particularly in the areas of feature detection and feature extraction.

<span class="mw-page-title-main">Sobel operator</span> Image edge detection algorithm

The Sobel operator, sometimes called the Sobel–Feldman operator or Sobel filter, is used in image processing and computer vision, particularly within edge detection algorithms where it creates an image emphasising edges. It is named after Irwin Sobel and Gary M. Feldman, colleagues at the Stanford Artificial Intelligence Laboratory (SAIL). Sobel and Feldman presented the idea of an "Isotropic 3 × 3 Image Gradient Operator" at a talk at SAIL in 1968. Technically, it is a discrete differentiation operator, computing an approximation of the gradient of the image intensity function. At each point in the image, the result of the Sobel–Feldman operator is either the corresponding gradient vector or the norm of this vector. The Sobel–Feldman operator is based on convolving the image with a small, separable, and integer-valued filter in the horizontal and vertical directions and is therefore relatively inexpensive in terms of computations. On the other hand, the gradient approximation that it produces is relatively crude, in particular for high-frequency variations in the image.

<span class="mw-page-title-main">Canny edge detector</span> Image edge detection algorithm

The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images. It was developed by John F. Canny in 1986. Canny also produced a computational theory of edge detection explaining why the technique works.

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 undesired signal component from the desired signal component, as with common-mode rejection ratio.

<span class="mw-page-title-main">Fractal flame</span>

Fractal flames are a member of the iterated function system class of fractals created by Scott Draves in 1992. Draves' open-source code was later ported into Adobe After Effects graphics software and translated into the Apophysis fractal flame editor.

<span class="mw-page-title-main">Image segmentation</span> Partitioning a digital image into segments

In digital image processing and computer vision, image segmentation is the process of partitioning a digital image into multiple image segments, also known as image regions or image objects. The goal of segmentation is to simplify and/or change the representation of an image into something that is more meaningful and easier to analyze. Image segmentation is typically used to locate objects and boundaries in images. More precisely, image segmentation is the process of assigning a label to every pixel in an image such that pixels with the same label share certain characteristics.

<span class="mw-page-title-main">Unsharp masking</span> Image sharpening technique

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", negative image to create a mask of the original image. The unsharp mask is then combined with the original positive image, creating an image that is less blurry than the original. The resulting image, although clearer, may be a less accurate representation of the image's subject.

The scale-invariant feature transform (SIFT) is a computer vision algorithm to detect, describe, and match local features in images, invented by David Lowe in 1999. Applications include object recognition, robotic mapping and navigation, image stitching, 3D modeling, gesture recognition, video tracking, individual identification of wildlife and match moving.

<span class="mw-page-title-main">Gaussian blur</span> Type of image blur produced by a Gaussian function

In image processing, a Gaussian blur is the result of blurring an image by a Gaussian function.

In signal processing, a nonlinearfilter is a filter whose output is not a linear function of its input. That is, if the filter outputs signals R and S for two input signals r and s separately, but does not always output αR + βS when the input is a linear combination αr + βs.

<span class="mw-page-title-main">Image noise</span> Visible interference in an image

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 scanner or digital camera. Image noise can also originate in film grain and in the unavoidable shot noise of an ideal photon detector. Image noise is an undesirable by-product of image capture that obscures the desired information. Typically the term “image noise” is used to refer to noise in 2D images, not 3D images.

In imaging science, difference of Gaussians (DoG) is a feature enhancement algorithm that involves the subtraction of one Gaussian blurred version of an original image from another, less blurred version of the original. In the simple case of grayscale images, the blurred images are obtained by convolving the original grayscale images with Gaussian kernels having differing width. Blurring an image using a Gaussian kernel suppresses only high-frequency spatial information. Subtracting one image from the other preserves spatial information that lies between the range of frequencies that are preserved in the two blurred images. Thus, the DoG is a spatial band-pass filter that attenuates frequencies in the original grayscale image that are far from the band center.

<span class="mw-page-title-main">Box blur</span> Graphic-art effect

A box blur is a spatial domain linear filter in which each pixel in the resulting image has a value equal to the average value of its neighboring pixels in the input image. It is a form of low-pass ("blurring") filter. A 3 by 3 box blur can be written as matrix

In mathematics, the structure tensor, also referred to as the second-moment matrix, is a matrix derived from the gradient of a function. It describes the distribution of the gradient in a specified neighborhood around a point and makes the information invariant to the observing coordinates. The structure tensor is often used in image processing and computer vision.

<span class="mw-page-title-main">Ordered dithering</span> Image dithering algorithm

Ordered dithering is an image dithering algorithm. It is commonly used to display a continuous image on a display of smaller color depth. For example, Microsoft Windows uses it in 16-color graphics modes. The algorithm is characterized by noticeable crosshatch patterns in the result.

Adaptive histogram equalization (AHE) is a computer image processing technique used to improve contrast in images. It differs from ordinary histogram equalization in the respect that the adaptive method computes several histograms, each corresponding to a distinct section of the image, and uses them to redistribute the lightness values of the image. It is therefore suitable for improving the local contrast and enhancing the definitions of edges in each region of an image.

In image processing, a kernel, convolution matrix, or mask is a small matrix used for blurring, sharpening, embossing, edge detection, and more. This is accomplished by doing a convolution between the kernel and an image. Or more simply, when each pixel in the output image is a function of the nearby pixels in the input image, the kernel is that function.

References

  1. 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.
  2. 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.
  3. Arce, Gonzalo R. (2005). Nonlinear Signal Processing: A Statistical Approach. New Jersey, USA: Wiley. ISBN   0-471-67624-1.