Comparison of the three smoothing filters (mean value, mean value, and edge persistence with direction characteristics)
The obtained original image usually carries some noise. In order to eliminate these noise, some smoothing filters can be applied to the image, but the smoothing filter is often prone to image blur. the Smoothing Effects of the three common smoothing filters are compared below.
1. Mean Filtering
The principle is that the sub-image is in N * n sub-blocks, and the gray scale of the detection point is the average gray scale of the block. This method disperses the gray scale of the burst point in its adjacent points to achieve smooth effect, the operation is also simple, but this smoothing often results in blurred images. N is selected to be large, and the more serious the Blur is.
F (x, y) = 1/(n * n)
2. Median Filtering
The mean-value filtering smoothing is also performed in N * n sub-blocks of the image. It is different from the mean-value smoothing when the average gray value of the neighboring point is selected as the gray value of the detection point, the median smoothing uses the intermediate gray level of the neighboring point as the gray level of the detection point, that is
F (x, y) = mid {f (1, 1), F (1, 2 )... F (n, n)} nε [1, N]
3. Edge persistence filtering with direction Detection
There are many filtering methods, one of which is to use five direction templates.
Calculate the variance D and mean e of each template
Definition
Select D for the minimum template of S, and compare it with the variance D of the square template. Select a smaller template as the template used by pixels, the gray scale of the target pixel is the average gray scale of the selected template.
This orientation-oriented template can save the edge of the image, so that the edge of the image line is not blurred due to smoothness, but the calculation is relatively large.
Look at the three smooth Effects
----------------------------------------------
We can see that the three images have become smooth, but the mean filtering blur is very serious. The median filtering filters out the finer lines, and the edge persistence filtering performs better on the edge persistence, select as needed.
Below are four of my smooth C code
// ---------------------- Compiled in VC. net.
/*
Author: lingch (sboom)
Date: 2005-1-26
Subject: Smooth Filter
*/
Bool filterav (unsigned char * image, int height, int width) // Mean Filter
{
Int I, J;
Unsigned char * P = (unsigned char *) malloc (height * width );
For (I = 1; I {
For (j = 1; j <width-1; j ++)
{
P [I * width + J] = (unsigned char) (INT) image [(I-1) * width + J-1]
+ (INT) image [(I-1) * width + J]
+ (INT) image (I-1) * width + J + 1]
+ (INT) image [I * width + J-1]
+ (INT) image [I * width + J]
+ (INT) image [I * width + J + 1]
+ (INT) image [(I + 1) * width + J-1]
+ (INT) image [(I + 1) * width + J]
+ (INT) image [(I + 1) * width + J + 1])/9 );
}
}
For (I = 1; I {
For (j = 1; j <width-1; j ++)
{
Image [I * width + J] = P [I * width + J];
}
}
Free (P );
Return true;
}
// ------------------------------ Median Filter
Bool filtermid (unsigned char * image, int height, int width)
{
Int I, J, K, L;
Int Pos;
Unsigned char temp;
Unsigned char PSR [9];
Unsigned char * P = (unsigned char *) malloc (height * width );
For (I = 1; I {
For (j = 1; j <width-1; j ++)
{// --- 3*3 window Matrix
PSR [0] = image [(I-1) * width + J-1];
PSR [1] = image [(I-1) * width + J];
PSR [2] = image [(I-1) * width + J + 1];
PSR [3] = image [I * width + J-1];
PSR [4] = image [I * width + J];
PSR [5] = image [I * width + J + 1];
PSR [6] = image [(I + 1) * width + J-1];
PSR [7] = image [(I + 1) * width + J];
PSR [8] = image [(I + 1) * width + J + 1];
// -------- Select sorting
For (k = 0; k <9; k ++)
{
Pos = K;
For (L = K; L <9; L ++)
{
If (PSR [l] <PSR [POS])
Pos = L;
}
Temp = PSR [k];
His/her [k] = his/her [POS];
PSR [POS] = temp;
}
// ------ Obtain the value
P [I * width + J] = PSR [4];
}
}
For (I = 1; I {
For (j = 1; j <width-1; j ++)
{
Image [I * width + J] = P [I * width + J];
}
}
Free (P );
Return true;
}
// ----------------- Edge persistence Filter
Bool filter5 (unsigned char * image, int height, int width)
{
Int I, J, K, L;
Double AV [5], av2 [5];
Double S;
Double SD [4];
Unsigned char * P = (unsigned char *) malloc (height * width );
For (I = 2; I {
For (j = 2; j <width-2; j ++)
{
AV [0] = 0.0; AV [1] = 0.0; AV [2] = 0.0; AV [3] = 0.0; AV [4] = 0.0;
Av2 [0] = 0.0; av2 [1] = 0.0; av2 [2] = 0.0; av2 [3] = 0.0; av2 [4] = 0.0;
AV [0] = (
+ Image [I * width + J-1]
+ Image [I * width + J]
+ Image [I * width + J + 1]
)/3;
Av2 [0] = (
+ Image [I * width + J-1] * image [I * width + J-1]
+ Image [I * width + J] * image [I * width + J]
+ Image [I * width + J + 1] * image [I * width + J + 1]
)/3-av [0] * AV [0];
AV [1] = (
+ Image [(I + 1) * width + J-1]
+ Image [I * width + J]
+ Image [(I-1) * width + J + 1]
)/3;
Av2 [1] = (
+ Image [(I + 1) * width + J-1] * image [(I + 1) * width + J-1]
+ Image [I * width + J] * image [I * width + J]
+ Image [(I-1) * width + J + 1] * image [(I-1) * width + J + 1]
)/3-av [1] * AV [1];
AV [2] = (
+ Image [(I-1) * width + J]
+ Image [I * width + J]
+ Image [(I + 1) * width + J]
)/3;
Av2 [2] = (
+ Image [(I-1) * width + J] * image [(I-1) * width + J]
+ Image [I * width + J] * image [I * width + J]
+ Image [(I + 1) * width + J] * image [(I + 1) * width + J]
)/3-av [2] * AV [2];
AV [3] = (
+ Image [(I-1) * width + J-1]
+ Image [I * width + J]
+ Image [(I + 1) * width + J + 1]
)/3;
Av2 [3] = (
+ Image [(I-1) * width + J-1] * image [(I-1) * width + J-1]
+ Image [I * width + J] * image [I * width + J]
+ Image [(I + 1) * width + J + 1] * image [(I + 1) * width + J + 1]
)/3-av [3] * AV [3];
AV [4] = 0.0; av2 [4] = 0.0;
For (k = I-1; k <= I + 1; k ++)
{
For (L = J-1; L <= J + 1; l ++)
{
AV [4] + = image [K * width + L];
Av2 [4] + = image [K * width + L] * image [K * width + L];
}
}
AV [4] = Av [4]/9.0;
Av2 [4] = av2 [4]/9.0-av [4] * AV [4];
For (k = 0; k <4; k ++)
SD [k] = av2 [k]/av2 [(k + 2) % 4];
L = 0;
For (k = 0; k <4; k ++)
If (SD [k] <SD [l])
L = K;
If (av2 [l] <AV [4])
P [I * width + J] = Av [l];
Else
P [I * width + J] = Av [4];
}
}
For (I = 0; I For (j = 0; j <width; j ++)
Image [I * width + J] = P [I * width + J];
Free (P );
Return true;
}