Computer Vision
Homework 9
General Edge Detection
December 14, 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 or inverting an image.
4. Conducting histogram equalization.
5. Dynamic morphological operation kernel assignment.
6. Conducting general morphological operations: dilation, erosion, opening, closing and hit-and-miss.
7. Down-sampling the image into smaller sizes.
8. Calculate Yokoi connectivity number and conducts image thinning.
9. Display binary image in ASCII ‘*’ symbols or in Yokoi connectivity numbers.
10. Adding Gaussian noise and salt & pepper to the image.
11. Dynamic assignment of filtering kernel.
12. Conducts box filter and mean filter.
13. General edge detection operators: Roberts, Prewitt, Sobel, Frei and Chen, Kirsch, Robinson, and Nevatia-Babu.
14. Simple hand adjusting zero crossing operation.
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.
Code Fragment for Zero-Crossing
This function conducts zero crossing detection on the input intensity image.
The threshold value depends on the user, which controls it with another threshold assigning window. This function can be found under the ‘Tools’ submenu.
void __fastcall IMAGE::ZeroCrossing( IMAGE *dest ) { int a, b;
dest->sizex = sizex;
dest->sizey = sizey;
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++ )
if( function_zero_crossing( a, b, Form8->TrackBar1->Position ) ) dest->data[b][a] = 255;
MainForm->ProgressBar1->Hide();
}
Code Fragment for Zero-Crossing Decision
This function is used by the zero-crossing function to determine that if its neighborhood pixels are in condition of a true zero crossing or not.
bool __fastcall IMAGE::function_zero_crossing( int x, int y, const int val ) { int u, v;
const int neighbor[8][2] = {
{ 1, 0 }, { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 1 }, { 1, -1 }, { -1, -1 }, { -1, 1 } };
int c;
for( c = 0; c < 8; c++ ) {
u = x + neighbor[c][0];
v = y + neighbor[c][1];
if( u < 0 || v < 0 || u >= sizex || v >= sizey ) continue;
if( ( ( data[y][x] * data[v][u] ) < 0 ) &&
( abs( data[y][x] ) >= val ) && ( abs( data[v][u] ) >= val ) ) return true;
}
return false;
}
Code Fragment for Edge Dectection
The mask used by this function is decided by its caller, which is the option the user selects from the main menu.
void __fastcall IMAGE::EdgeDetecting( char *type, TObject *Sender ) { int a, b, c, dx, dy;
float val;
FILE *fp;
int max;
sprintf( buffer, "%s\\Filters\\%s.flt", cwd, type );
if( ( fp = fopen( buffer, "r+t" ) ) == NULL ) {
ShowMessage( AnsiString( "System Error: Filter " )
+ AnsiString( buffer ) + AnsiString( "doesn't exist!" ) );
return;
}
fscanf( fp, "%d", &zImages );
fscanf( fp, "%d %d", &dx, &dy );
max = 0;
for( a = 0; a < zImages; a++ ) {
Form7->BitBtn3Click( Sender );
for( b = 0; b < dx; b++ ) for ( c = 0; c < dy; c++ ) {
fscanf( fp, "%f", &val );
Form7->StringGrid1->Cells[c+23-dy/2][b+23-dx/2] = FloatToStr( val );
if( max < ( int ) val ) max = ( int ) val;
}
zTempImage[a] = new IMAGE;
CopyImage( zTempImage[a] );
zTempImage[a]->BoxFilter( &box_filter );
}
Form8->TrackBar1->Max = 5 * max * dx * dy;
Form8->TrackBar1->Frequency = Form8->TrackBar1->Max / 10;
Form8->TrackBar1->Position = 2 * max * dx * dy;
Form8->Edit1->Text = IntToStr( Form8->TrackBar1->Position );
fclose( fp );
Form8->ShowModal();
}