Home Content «GaussianFilter DCTFilter»
Copyright: V. Miszalok
Last Modified:
It's the most popular and simple nonlinear lowpass filter.
It reduces noise without blurring.
The pixels inside the 3x3
-kernel are sorted according
to intensity in ascending order and arranged into an 1D-array of length 3*3=9
and the the value at the mid of the range (= median) is elected.
Advantage: The filter reduces noise without producing a smooth ramp at the border between a dark and a
light area.
Behaviour at 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:
Produces unexpected artefacts.
Erases fine lines and/or inverts parallel fine lines black -> white and white -> black.
Erodes sharp edges.
The filter is slow.
Summary: The Median filter should be replaced by the Sigma filter which is better, faster and produces less artifacts.
******************************************************************************************************
Experiments:
******************************************************************************************************
Try out different filter sizes between 1 and 61.
Observe the loss of fine lines and sharp edges.
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.
Observe the dependency between filter size and time consumption caused by sorting.
Try out other and noisy images.
Caution: In case of big images, the filter can take several minutes !
The following median filter algorithm incorporates border handling.
It 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
.
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.