Computer Vision
Homework 5
Gray Morphology
November 17, 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.
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 Dilation
This method conducts the basic gray dilation morphological operation.
void __fastcall IMAGE::GrayDilation( GMORPHOLOGY *m ) { int a, b, c ,d;
IMAGE *temp = new IMAGE;
int tmp;
int max;
GMORPHOLOGY *x = new GMORPHOLOGY;
for( b = 0; b < 45; b++ ) for( a = 0; a < 45; a++ ) {
x->mask[b][a] = m->mask[a][b];
x->selected[b][a] = m->selected[a][b];
x->color[b][a] = m->color[a][b];
}
GetGrayMorphMaskBoundary( x );
for( a = 0; a < sizex; a++ )
temp->data[a][b] = 0;
MainForm->ProgressBar1->Show();
for( MainForm->ProgressBar1->Position = b = 0; b < sizey;
b++, MainForm->ProgressBar1->Position = b ) for( a = 0; a < sizex; a++ )
{
for( max = 0, d = x->min_y+22; d <= x->max_y+22; d++ ) for( c = x->min_x+22; c <= x->max_x+22; c++ ) {
if( b+d-22 >= sizey || b+d-22 < 0 ||
a+c-22 >= sizex || a+c-22 < 0 ) continue;
if( x->selected[d][c] ) {
tmp = data[b+d-22][a+c-22] + x->mask[d][c];
if( max < tmp ) max = tmp;
} }
if( max < 0 ) max = 0;
else if( max > 255 ) max = 255;
temp->data[b][a] = ( char ) max;
}
MainForm->ProgressBar1->Hide();
for( b = 0; b < sizey; b++ ) for( a = 0; a < sizex; a++ ) data[b][a] = temp->data[b][a];
delete temp;
delete x;
return;
}
Code Fragment for Erosion
This method conducts the basic erosion morphological operation.
void __fastcall IMAGE::GrayErosion( GMORPHOLOGY *m ) { int a, b, c ,d;
IMAGE *temp = new IMAGE;
int tmp;
int min;
GetGrayMorphMaskBoundary( m );
for( a = 0; a < sizex; a++ ) for( b = 0; b < sizey; b++ ) temp->data[a][b] = 0;
MainForm->ProgressBar1->Show();
for( MainForm->ProgressBar1->Position = b = 0; b < sizey;
b++, MainForm->ProgressBar1->Position = b ) for( a = 0; a < sizex; a++ )
{
for( min = 255, 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->selected[d][c] ) {
tmp = data[b+d-22][a+c-22] - m->mask[d][c];
if( min > tmp ) min = tmp;
} }
if( min < 0 ) min = 0;
else if( min > 255 ) min = 255;
temp->data[b][a] = ( char ) min;
}
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 Locking Morph Kernel
This method will find out the size of the kernel for morphing. This can reduce computational time, since 45 by 45 kernel may not be used most of the time.
void __fastcall IMAGE::GetGrayMorphMaskBoundary( GMORPHOLOGY *m ) { int a, b;
m->max_x = 0;
m->max_y = 0;
m->min_x = 45;
m->min_y = 45;
for( b = 0; b < 45; b++ ) for( a = 0; a < 45; a++ ) if( m->selected[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 Opening
The opening operation can be done by first applying the erosion operation and then followed by dilation operation.
void __fastcall TMainForm::Opening2Click(TObject *Sender) { TMDIChild *MDI;
MDI = ( TMDIChild * )MainForm->ActiveMDIChild;
MDI->image->GrayErosion( &gray_ero );
MDI->image->GrayDilation( &gray_dil );
MDI->ShowImage();
}
Code Fragment for Closing
The closing operation can be done by first applying the dilation operation and then followed by erosion operation.
void __fastcall TMainForm::Closing2Click(TObject *Sender) { TMDIChild *MDI;
MDI = ( TMDIChild * )MainForm->ActiveMDIChild;
MDI->image->GrayDilation( &gray_dil );
MDI->image->GrayErosion( &gray_ero );
MDI->ShowImage();
}