Median Filtering in C# by asif

Brief Description

The median filter is normally used to reduce noise in an image, somewhat like the mean filter.

How It Works

Like the mean filter, the median filter considers each pixel in the image in turn and looks at its nearby neighbors to decide whether or not it is representative of its surroundings. Instead of simply replacing the pixel value with the mean of neighboring pixel values, it replaces it with the median of those values. The median is calculated by first sorting all the pixel values from the surrounding neighborhood into numerical order and then replacing the pixel being considered with the middle pixel value. (If the neighborhood under consideration contains an even number of pixels, the average of the two middle pixel values is used.) Figure 1 illustrates an example calculation.

Figure 1 Calculating the median value of a pixel neighborhood. As can be seen, the central pixel value of 150 is rather unrepresentative of the surrounding pixels and is replaced with the median value: 124. A 3×3 square neighborhood is used here — larger neighborhoods will produce more severe smoothing.

In general, the median filter allows a great deal of high spatial frequency detail to pass while remaining very effective at removing noise on images where less than half of the pixels in a smoothing neighborhood have been effected. (As a consequence of this, median filtering can be less effective at removing noise from images corrupted with Gaussian noise.)

One of the major problems with the median filter is that it is relatively expensive and complex to compute. To find the median it is necessary to sort all the values in the neighborhood into numerical order and this is relatively slow, even with fast sorting algorithms such as quicksort. The basic algorithm can, however,be enhanced somewhat for speed. A common technique is to notice that when the neighborhood window is slid across the image, many of the pixels in the window are the same from one step to the next, and the relative ordering of these with each other will obviously not have changed. Clever algorithms make use of this to improve performance.

Basic Working:

The effect of a median Filter on the Image:

The result after convolution is as:

Sample Project:

The GUI of the whole Project is as:

The project is a part of the series of the image processing articles written just for the prosperity and help for the students searching for Image Processing free stuff.


R. Boyle and R. Thomas Computer Vision: A First Course, Blackwell Scientific Publications, 1988, pp 32 – 34.

E. Davies Machine Vision: Theory, Algorithms and Practicalities, Academic Press, 1990, Chap. 3.

A. Marion An Introduction to Image Processing, Chapman and Hall, 1991, p 274.

D. Vernon Machine Vision, Prentice-Hall, 1991, Chap. 4.


Project Files Median Filtering

However, it often does a better job than the mean filter of preserving useful detail in the image. Median filtering is a non-linear signal enhancement technique  for the smoothing of signals, the suppression of impulse noise, and preserving of edges. In the one-dimensional case it consists of sliding a window of an odd number of elements along the signal, replacing the centre sample by the median of the samples in the window.