Home Contents << Prev. >> Next

Image Processing: Filters

1.2.   The Sigma Filter
1.2.1 The 3x3 Sigma Filter
1.2.2 Sigma Filtering Of Real Images
1.2.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 directories and click ***.sln.

 

SigmaFilter3x3.zip.
SigmaFilterForRealImages.zip.

The Sigma Filter

is a non-linear variant of the linear averaging filter aiming to supress noise without blurring the image too much.
It was invented 1983 by Jong-Sen LEE who wrote: "This filter is motivated by the sigma probability of the Gaussian distribution, and it smooths the image noise by averaging only those neighborhood pixels which have the intensities within a fixed sigma range of the center pixel. Consequently, image edges are preserved, and subtle details and thin lines such as roads are retained."
The filter became polular because it improves noisy images and flattens local differences with minimal loss of sharpness.
It replaces any pixel value by the vote of some, but not all pixels of its rectangular neighborhood.
The idea is to exclude the outsiders which differ too much (=more than sigma) from the center and to restrict democratic voting to a subset of sufficiently conforme neighbors.
Here is the pseudo-code for gray value images:
1. Take the pixel's gray value and add the gray values of those of its eight neighbors,
    which differ less than sigma,
2. divide the sum by the no of the voting neighbors, round the result to integer, and
3. write it to the output image at the same x,y position.

Pseudo Code:
for each pixel Input(x,y) 
{ sum=0; no=0; MidPixel=Input(x,y);
  for each neighbor inside the kernel centered around (x,y):
    if (abs(Input(x,y)-MidPixel) <= sigma) { sum+=Input(x,y); no++; }
  if (no != 0) Output(X,Y)=Round(sum/no); else Output(x,y)=Input(x,y);
}
Comparison Average Filter Sigma Filter
Blurringsevere depends on the sigma value
Filter strength Growing kernel size → growing strength Additionaly to kernel size: growing sigma → growing strength.
sigma = 0 → no filtering at all; sigma = 255 → identical blur as av. filter
Steep step or edgetransformed to ramp step or edge persist when sigma fits
Noise suppressed suppressed
Performance Fast averaging is fast with any kernel size slow, double kernel size → 4 times longer
Border handling by copying the outmost values by automatical kernel shrink when it overlaps the border.
Parameter setting problems How to find the best kernel size ? How to find the best sigma and the best kernel size ?

The 3x3 Sigma Filter

Get Microsoft Silverlight

******************************************************************************************************
Experiments:
******************************************************************************************************
1. Try out sigma = 255.
2. Press several times the "More Noise" button.
3. Try out which sigma produces the best noise suppression.
******************************************************************************************************
C#-Code of the SigmaFilter3x3 click event:

private void filter( int sigma ) 
{ int midPixel, anyPixel, sum, no, x, xx, xxx, y, yy, yyy;
  for ( y=0; y < ySize; y++ ) //=============
  { for ( x=0; x < xSize; x++ ) //===========
    { midPixel = image1[y,x];   //input is the noisy image
      sum = no = 0;
      for ( yy=-1; yy <= 1; yy++ ) //==========
      { yyy = y + yy;
        if ( yyy < 0 | yyy >= ySize ) continue;   //beyond the border
        for ( xx=-1; xx <= 1; xx++ )//=========
        { xxx = xx + x;
          if ( xxx < 0 | xxx >= xSize ) continue; //beyond the border
          anyPixel = image1[yyy,xxx];
          if ( Math.Abs( midPixel - anyPixel ) < sigma )
          { sum += anyPixel; no++; }
        } //====== end for (int xx... ================
      } //======== end for (int yy... ================
      if ( no > 0 ) image2[y,x] = Convert.ToByte( (float)sum / no );
      else          image2[y,x] = (Byte)midPixel; //just copy
    } //============ end for (int x... =====================
  } //============== end for (int y... =====================
}

Sigma Filtering Of Real Images

Get Microsoft Silverlight

******************************************************************************************************
Experiments:
******************************************************************************************************
1. Try out a kernel of 11x11 with many sigmas.
2. Try out sigma = 0 and sigma = 255.
3. Try out extreme kernels such as 128x1 and 1x106. 4. Load bigger images but do not apply kernels bigger than 15x15. Otherwise you have to be very patient.
5. When a filter takes too long, you can shorten the thread by simply shifting the FWidth slider to 1.
******************************************************************************************************

Questions & Answers

******************************************************************************************************
Q: What is a sigma filter ? One line pseudo code ?
A: It's a variant of the average filter with a selection, what neighbors inside the kernel have the right to vote.
Outsiders, which differ heavily from the central pixel are excluded from being averaged.
if ( neighborPixel < sigma ) average it; else forget it;
It is the best and most flexible noise suppression filter.
******************************************************************************************************
Q: What happens when sigma = 0 ? What happens when sigma = 255 ?
A: Sigma = 0 → just identical neighbors are averaged → no filtering at all.
Sigma = 255 → all neighbors are averaged → same result as the average filter.
*******************************************************************************************************
Q: How works the sigma filters border handling ? Advantages, Disadvantages ?
A: The kernel automatically shrinks where it overlaps the image borders. The filter covers the complete image.
Advantages: No black spaces are left along the borders. Border handling is part of the algorithm.
Disadvantages: Filter strength is weaker along the borders and it is time consuming.
*******************************************************************************************************
Q: Why is the sigma filter slower than the simple average filter ?
A: The sigma algorithm replaces simple addition by conditional addition →
if ( Math.Abs(neighbor-center) < sigma ) {add neighbor to sum;} else {forget it;};
*******************************************************************************************************
Q: Why there is no fast sigma filter ?
A: The filter can't be separated in a horizontal and a vertical partial filter and
there is no effective way to use the precedent sum to compute the next one.
*******************************************************************************************************
Q: How to find the best kernel size and the best sigma ?
A: The sigma filter works best with a kernel size between 5x5 and 15X15 and with a sigma of about 128.
The final values must be found by trial and error.
*******************************************************************************************************
*******************************************************************************************************
Further readings:
*******************************************************************************************************
Kovalevsky/ImProc/L01_Noise/Noise_e.htm


Home Contents << Prev. >> Next