Home |
Contents |
<< Prev. |
>> Next |

You can download the complete source codes of the Silverlight solution directories of this chapter. |

is the most popular and simple nonlinear lowpass filter.

It
reduces noise without blurring.

The pixels inside the |

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

******************************************************************************************************

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... ===================== }

******************************************************************************************************

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 |