Home Contents << Prev. >> Next

Image Processing 1: Filters

1.1.   The Median Filter
1.1.1 The 7x7 Median Filter
1.2.2 Median Filter of Real Images
1.1.3 Questions & Answers
Copyright © by V. Miszalok, last update: 2011-07-02

Mail me...
Let me know
what you think

You can download the complete source codes of the Silverlight solution directories of this chapter.
When you have installed Visual Web Developer 2010 Express plus Web Platform, you unzip the directory and click ***.sln.

 

    MedianFilterOfRealImages.zip.

The Median Filter

is the most popular and simple nonlinear lowpass filter.
It reduces noise without blurring.

The pixels inside the MxN-kernel are sorted according to intensity in ascending order and arranged into an 1D-array of length M*N and the the value at the mid of the range at position M*N/2 = median is elected.
Advantage: The filter reduces noise without producing a smooth ramp at the border between a dark and a light area.
Sample: A vertical border between a dark area left and a bright one on the right:
As long as the filter center remains on the left side of the border the elected median is dark,
when the center moves right into the light side the elected median switches to light at once.

Drawbacks:
1. Produces unexpected artefacts.
2. Erases fine lines and/or inverts parallel fine lines black -> white and white -> black.
3. Erodes sharp edges.
4. The filter is slow.
Summary: The median filter should be replaced by the sigma filter which is better, faster and produces less artifacts.

 

The filtering has the classical form with 4 nested for-loops. The usual division is replaced by a quick sort algorithm. The algorithm accepts any quadratic filter sizes NxN where N must be a positive odd integer N < b0.Width, N < b0.Height. Time depends on ImageSize*FilterSize = Image.Width*Image.Height*N*N.

Get Microsoft Silverlight

******************************************************************************************************
Experiments:
******************************************************************************************************
1. Try out different filter sizes between 1 and 61.
2. Observe the loss of fine lines and sharp edges.
3. Choose FilterWidth=3 and FilterHeight=1. Observe the vertical fine lines on the left.
    The leftmost and the rightmost lines are lost and the remaining lines are inverted black <-> white.
    With FilterWidth=1 and FilterHeight=3, the same phenomenon occurs with the horizontal lines.
4. Observe the dependency between filter size and time consumption caused by sorting.
5. Try out other and noisy images.
6. Caution: In case of big images, the filter can take several minutes.
******************************************************************************************************

The following median filter algorithm incorporates border handling.
C#-Code of the MedianFilter subroutine:

//global variables
WriteableBitmap img0, img1; //input and output images
Int32 kw, hkw;  //kernel width  and half kernel width,  kw uneven and hkw even
Int32 kh, hkh;  //kernel height and half kernel height, kh uneven and hkh even
Int32 xSize, ySize; //image width and height
Byte[ySize,xSize] R0, G0, B0; //the three color layers of the input image

private void MedianFilter()
{ int R, G, B, no, noh, x, xx, xxx, y, yy, yyy, startOfYRow;
  Byte[] RR = new Byte[kw*kh]; //linear red   array to be sorted
  Byte[] GG = new Byte[kw*kh]; //linear green array to be sorted
  Byte[] BB = new Byte[kw*kh]; //linear blue  array to be sorted
  for ( y=0; y < ySize; y++ ) //==================
  { startOfYRow = y*xSize;
    for ( x=0; x < xSize; x++ ) //===============
    { no=0;
      for ( yy=-hkh; yy <= hkh; yy++ ) //=============
      { yyy = y + yy;
        if ( yyy < 0 | yyy >= ySize ) continue; //beyond border
        for ( xx=-hkw; xx <= hkw; xx++ )//========
        { xxx = x + xx;
          if ( xxx < 0 | xxx >= xSize ) continue; //beyond border
          RR[no] = R0[yyy,xxx]; //copy into the linear array to be sorted
          GG[no] = G0[yyy,xxx];
          BB[no] = B0[yyy,xxx];
          no++;
        } //====== end for (int xx... ================
      } //======== end for (int yy... ================
      Array.Sort( RR, 0, no ); //sorting
      Array.Sort( GG, 0, no ); //sorting
      Array.Sort( BB, 0, no ); //sorting
      noh = no/2; //index in the mid
      R=RR[noh];  //elected red    
      G=GG[noh];  //elected green 
      B=BB[noh];  //elected blue 
      unchecked { img1.Pixels[startOfYRow+x] = (Int32)0xff000000 | R | (G << 8) | (B << 16); }
    } //============ end for (int x... =====================
  } //============== end for (int y... =====================
}

Questions & Answers

******************************************************************************************************
Q: The main idea of the Median filter ? Advantage ? Disadvantages ?
A: The pixels inside the kernel are sorted according to intensity in ascending order and arranged into an 1D-array.
    The value at the mid of the range at position M*N/2 = median is elected.
    Adv: No blurring.
    Disadv:
    1. Produces unexpected artefacts.
    2. Destruction of fine lines.
    3. Erosion of sharp edges.
    4. Sorting is slow.
******************************************************************************************************
Q: Compare the Median filter with the Average, Fog and Gauss filters !
A: Average+Fog+Gauss: Smooth any sharp border and blur the image.
Median: No blurring but unexpected artefacts.
Median is much slower than fast Average+Fog+Gauss.
******************************************************************************************************
Q: Compare the Median filter with the Sigma filter !
A: Sigma is the better alternative.
It's more complicated but it can preserve fine lines and sharp edges.
Both filters are slow.


Home Contents << Prev. >> Next