Computer Vision
Homework 8
Noise Cleaning
December 8, 1998
R87526001
許為元 William W. HsuProgramming tool:
This program was developed with Borland C++ Builder 3.0 on Windows 98.
This program has the following functions:
1. Dynamic histogram display and multi-image display.
2. Dynamic information (pixel, histogram) update status bar.
3. Binarizing an image.
4. Conducting histogram equalization.
5. Inverting an image.
6. Dynamic binary morphological operation kernel assignment.
7. Conducting binary morphological operations: dilation, erosion, opening, closing and hit-and-miss.
8. Dynamic gray morphological operation kernel assignment with color depth showing its associated value.
9. Conducting gray morphological operations: dilation, erosion, opening and closing.
10. Down sampling the image into smaller sizes.
11. Calculate Yokoi connectivity number.
12. Display binary image in ASCII ‘*’ symbols or in Yokoi connectivity numbers.
13. Conducts binary image thinning operation.
14. Adding Gaussian noise and salt & pepper to the image.
15. Dynamic assignment of filtering kernel.
16. Conducts box filter and mean filter.
The format of the data files used in this program ( *.raw format ) is as follows:
1. The first 4 bytes specify the image height.
2. The second 4 bytes specify the image width.
3. The following 512*512 bytes specifies the image data in gray level.
Conclusions of this Work
These results have been evaluated visually and also by SNR ratio.
1. The box filter works better than the median filter on Gaussian noise.
2. The median filter works better that box filter on salt & pepper noise.
Code Fragment for Adding Gaussian noise
This functions adds Gaussian noise to the image.
void __fastcall IMAGE::GaussianNoise( int amplitude ) { int a, b;
randomize();
MainForm->ProgressBar1->Show();
MainForm->ProgressBar1->Max = sizey;
for( MainForm->ProgressBar1->Position = b = 0; b < sizey;
b++, MainForm->ProgressBar1->Position = b ) for( a = 0; a < sizex; a++ )
data[b][a] += ( char )( RandG( 0.0, 1.0 ) * amplitude );
MainForm->ProgressBar1->Hide();
}
Code Fragment for Adding Salt & Pepper Noise
This functions adds salt & pepper noise to the image. The noise rate is the combined percentage of salt noise and pepper noise.
void __fastcall IMAGE::SaltPepperNoise( int salt, int pepper ) { int a, b;
int val;
randomize();
MainForm->ProgressBar1->Show();
MainForm->ProgressBar1->Max = sizey;
for( MainForm->ProgressBar1->Position = b = 0; b < sizey;
b++, MainForm->ProgressBar1->Position = b ) for( a = 0; a < sizex; a++ )
{
val = random( 100 );
if( val < salt ) data[b][a] = 255;
if( val >= pepper ) data[b][a] = 0;
}
MainForm->ProgressBar1->Hide();
}
Code Fragment for Filter Preprocessing
This function locks the orientation of the filter and determines the filter size.
void __fastcall IMAGE::GetBoxFilterMaskBoundary( BOXFILTER *m ) { int a, b;
m->max_x = 0;
m->max_y = 0;
m->min_x = 45;
m->min_y = 45;
for( b = 1; b < 46; b++ ) for( a = 1; a < 46; a++ )
m->data[b-1][a-1] = atoi( Form7->StringGrid1->Cells[b][a].c_str() );
for( b = 0; b < 45; b++ ) for( a = 0; a < 45; a++ ) if( m->data[b][a] ) {
if( a < m->min_x ) m->min_x = a;
else if( a > m->max_x ) m->max_x = a;
if( b < m->min_y ) m->min_y = b;
else if( b > m->max_y ) m->max_y = b;
}
m->min_x -= 22;
m->max_x -= 22;
m->min_y -= 22;
m->max_y -= 22;
return;
}
Code Fragment for Box Filtering
This function performs box filtering on the image.
void __fastcall IMAGE::BoxFilter( BOXFILTER *m ) { int a, b, c ,d;
int avg;
int value;
IMAGE *temp = new IMAGE;
GetBoxFilterMaskBoundary( m );
for( a = 0; a < sizex; a++ ) for( b = 0; b < sizey; b++ ) temp->data[a][b] = 0;
MainForm->ProgressBar1->Show();
MainForm->ProgressBar1->Max = sizey;
for( MainForm->ProgressBar1->Position = b = 0; b < sizey;
b++, MainForm->ProgressBar1->Position = b ) for( a = 0; a < sizex; a++ )
{
avg = 0;
value = 0;
for( d = m->min_y+22; d <= m->max_y+22; d++ ) for( c = m->min_x+22; c <= m->max_x+22; c++ ) {
if( b+d-22 >= sizey || b+d-22 < 0 ||
a+c-22 >= sizex || a+c-22 < 0 ) continue;
avg += m->data[d][c];
if( m->data[d][c] )
value += ( data[b+d-22][a+c-22] * m->data[d][c] );
}
temp->data[b][a] = ( char )( value / avg );
}
MainForm->ProgressBar1->Hide();
for( b = 0; b < sizey; b++ ) for( a = 0; a < sizex; a++ ) data[b][a] = temp->data[b][a];
delete temp;
return;
}
Code Fragment for Median Filtering
This function performs median filtering on the image.
void __fastcall IMAGE::MedianFilter( BOXFILTER *m ) { int a, b, c ,d;
int avg;
int value;
int sort[45*45];
IMAGE *temp = new IMAGE;
GetBoxFilterMaskBoundary( m );
for( a = 0; a < sizex; a++ ) for( b = 0; b < sizey; b++ ) temp->data[a][b] = 0;
MainForm->ProgressBar1->Show();
MainForm->ProgressBar1->Max = sizey;
for( MainForm->ProgressBar1->Position = b = 0; b < sizey;
b++, MainForm->ProgressBar1->Position = b ) for( a = 0; a < sizex; a++ )
{
value = 0;
for( d = m->min_y+22; d <= m->max_y+22; d++ ) for( c = m->min_x+22; c <= m->max_x+22; c++ ) {
if( b+d-22 >= sizey || b+d-22 < 0 ||
a+c-22 >= sizex || a+c-22 < 0 ) continue;
if( m->data[d][c] )
sort[value++] = data[b+d-22][a+c-22];
}
for( c = 0; c < value - 1; c++ ) for( d = c + 1; d < value; d++ ) if( sort[c] > sort[d] ) {
avg = sort[c];
sort[c] = sort[d];
sort[d] = avg;
}
temp->data[b][a] = ( char ) sort[( value - 1) / 2];
}
MainForm->ProgressBar1->Hide();
for( b = 0; b < sizey; b++ ) for( a = 0; a < sizex; a++ ) data[b][a] = temp->data[b][a];
delete temp;
return;
}