Assignment4 Report

advertisement
SFTW453
Digital Image Processing
Assignment 4
Morphological Image Processing
D-A7-2819-0
Cao Han
Abstract:
(a) Write a computer program capable of performing binary dilation and erosion with
an arbitrary structuring element of size 3 x 3 that can be specified by the user.
(b) Write a computer program for performing set intersection, differencing, and
complementation.
(c) Implement morphological boundary extraction as in Eq. (9.5-1), and reproduce the
results in Fig. 9.14.
Technical Discussion
The definition and formula for erosion:
The definition and formula for dilation:
We consider a binary image, we need the user specified the arbitrary structure element,
B, and just build an image and for each point, we do a comparison with the 3 x 3
matrix the user gave using erosion and dilation separately to realize the goal.
a. We can consider the whole images’ set processing into each pixel, we may
compare each pixel of the two pictures, for intersection if both are same we keep it,
otherwise we throw both; for differencing, we using the differences between each
pixels and generate the whole image; for complementation, we just make all pixel be
the opposite (black is 0, opposite white is 255).
b. We use the function we get above to realize the equation which is
c.
Discussion of Result:
Obviously, from the result we may see that dilation will enlarge the shape of a binary
image and erosion will generally make the original shape smaller than before.
The intersection, difference and complementation of two images just like which in
mathematics. We just need to consider every small pixel of certain images to get these
3 results.
Using Eq. (9.5-1), we may get the edge of a certain shape of an image.
Results:
There is something wrong with my computer’s display so that the white is appearance
as red, but the functions of my program are ok. You may see the results below:
Original
After Dilation
After Erosion
Intersection (intersection of Image before Erosion and after Erosion)
Difference (also the reproduce of Figure 9.14)
Complementation
Appendix:
#include "CImg.h"
#include <iostream>
using namespace cimg_library;
using namespace std;
int se[3][3];
CImg<unsigned char>
intersection(CImg<unsigned char> img1, CImg<unsigned char> img2)
{
CImg<unsigned char> image(img1.dimx(), img1.dimy());
for ( int x = 0; x < img1.dimx(); x++ )
for ( int y = 0; y < img1.dimy(); y++ )
{
if(img1(x,y)<img2(x,y))
image(x,y)=img1(x,y);
else image(x,y)=img2(x,y);
}
image.save_convert("intersection.jpg");
return image;
}
CImg<unsigned char>
complementation (CImg<unsigned char> img)
{
CImg<unsigned char> image(img.dimx(),img.dimy());
for ( int x = 0; x < img.dimx(); x++ )
for ( int y = 0; y < img.dimy(); y++ )
{
image(x,y)=255-img(x,y);
}
image.save_convert("complementation.jpg");
return image;
}
CImg<unsigned char>
difference(CImg <unsigned char> img1,CImg<unsigned char> img2)
{
CImg<unsigned char> image(img1.dimx(),img1.dimy());
for ( int x = 0; x < img1.dimx(); x++ )
for ( int y = 0; y < img1.dimy(); y++ )
{
if(img1(x,y)==255)
image(x,y)=img1(x,y)-img2(x,y);
else image(x,y)=0;
}
image.save_convert("difference.jpg");
return image;
}
CImg<unsigned char>
dilation(CImg<unsigned char> img)
{
int x,y;
CImg<unsigned char> image(img.dimx(),img.dimy(),1,3,0);
for(x = 0; x < img.dimx(); x++){
for(y = 0; y < img.dimy(); y++){
if(img(x, y, 0, 0) == se[1][1]){
if(x < 1){
if(y < 1){
image(x, y, 0, 0) = se[1][1];
image(x + 1, y, 0, 0) = se[1][2];
image(x, y + 1, 0, 0) = se[2][1];
} else{
image(x, y - 1, 0, 0) = se[0][1];
image(x + 1, y - 1, 0, 0) = se[0][2];
image(x, y, 0, 0) = se[1][1];
image(x + 1, y, 0, 0) = se[1][2];
image(x, y + 1, 0, 0) = se[2][1];
image(x + 1, y + 1, 0, 0) = se[2][2];
}
} else if(y < 1){
image(x - 1, y, 0, 0) = se[1][0];
image(x, y, 0, 0) = se[1][1];
image(x + 1, y, 0, 0) = se[1][2];
image(x - 1, y + 1, 0, 0) = se[2][0];
image(x, y + 1, 0, 0) = se[2][1];
image(x + 1, y + 1, 0, 0) = se[2][2];
}else if(x == img.dimx() - 1){
if(y == img.dimy() - 1){
image(x - 1, y - 1, 0, 0) = se[0][0];
image(x, y - 1, 0, 0) = se[0][1];
image(x - 1, y, 0, 0) = se[1][0];
image(x, y, 0, 0) = se[1][1];
} else{
image(x - 1, y - 1, 0, 0) = se[0][0];
image(x, y - 1, 0, 0) = se[0][1];
image(x - 1, y, 0, 0) = se[1][0];
image(x, y, 0, 0) = se[1][1];
image(x - 1, y + 1, 0, 0) = se[2][0];
image(x, y + 1, 0, 0) = se[2][1];
}
}else if(y == img.dimy() - 1){
image(x - 1, y - 1, 0, 0) = se[0][0];
image(x, y - 1, 0, 0) = se[0][1];
image(x + 1, y - 1, 0, 0) = se[0][2];
image(x - 1, y, 0, 0) = se[1][0];
image(x, y, 0, 0) = se[1][1];
image(x + 1, y, 0, 0) = se[1][2];
image(x, y + 1, 0, 0) = se[2][1];
}else{
image(x - 1, y - 1, 0, 0) = se[0][0];
image(x, y - 1, 0, 0) = se[0][1];
image(x + 1, y - 1, 0, 0) = se[0][2];
image(x - 1, y, 0, 0) = se[1][0];
image(x, y, 0, 0) = se[1][1];
image(x + 1, y, 0, 0) = se[1][2];
image(x - 1, y + 1, 0, 0) = se[2][0];
image(x, y + 1) = se[2][1];
image(x + 1, y + 1, 0, 0) = se[2][2];
}
} else
image(x,y,0,0) = img(x,y,0,0);
}
}
image.save_convert("dilation.jpg");
return image;
}
CImg<unsigned char>
erosion(CImg<unsigned char> img)
{
CImg<unsigned char> img0(img.dimx()+1,img.dimy()+1,1,3,0);
int x,y,i,j;
int judge;
img0(0, 0, 0, 0) = 0;
img0(1, 0, 0, 0) = 0;
img0(0, 1, 0, 0) = 0;
img0(1, 1, 0, 0) = 0;
for(x = 1; x < img.dimx() + 1; x++)
for(y = 1; y < img.dimy() + 1; y++)
img0(x, y, 0, 0) = img(x - 1, y - 1, 0, 0);
img0.save_convert("temp.jpg");
CImg<> img1("temp.jpg");
for(x = 1; x < img.dimx() + 1; x++){
for(y = 1; y < img.dimy() + 1; y++){
if(img0(x, y, 0, 0) == se[1][1] ){
judge = 0;
for(i = -1; i <= 1; i++)
for(j = -1; j <= 1; j++)
if(img0(x + i, y + j, 0, 0) == se[i + 1][j + 1])
judge++;
if(judge != 9){
for(i = -1; i <= 1; i++)
for(j = -1; j <= 1; j++){
if(se[1][1] == 255)
img1(x, y, 0, 0) = 0;
else
img1(x, y, 0, 0) = 255;
}
}
}else{
if(se[1][1] == 255)
img1(x, y, 0, 0) = 0;
else
img1(x, y, 0, 0) = 255;
}
}
}
img1.save_convert("erosion.jpg");
return img0;
}
CImg<unsigned char>
boundary_extraction(CImg<unsigned char> img)
{
CImg<unsigned char> img0(img.dimx(),img.dimy()),
temp(img.dimx(),img.dimy());
temp=erosion(img);
img0=difference(img,temp);
img0.save_convert("boundary_extraction.jpg");
return img0;
}
int main()
{
CImg<unsigned char> img0("human.jpg");
CImg<unsigned char> img2(img0.dimx(),img0.dimy());
CImg<unsigned char> img3(img0.dimx(),img0.dimy());
int i,k,num,temp;
cout<<"input the values for structuring element line by line:"<<endl;
for (i=0;i<3;i++)
for (k=0;k<3;k++)
{
cout<<"input the value 0 or 1 in ["
<<i+1
<<"]"
<<"["
<<k+1
<<"]:";
cin>>num;
se[i][k]=num;
}
for (i=0;i<3;i++)
for (k=0;k<3;k++)
{
temp=se[i][k];
se[i][k]=se[2-i][2-k];
se[2-i][2-k]=temp;
}
for(i = 0; i < 3; i++)
for(k = 0; k < 3; k++)
if(se[i][k] == 1)
se[i][k] = 255;
// dilation of original picture
img3=dilation(img0);
// erosion of original picture
img2=erosion(img0);
//intersection of dilation picture and original picture
img2=intersection(img0,img3);
//complementation of original picture
img2=complementation(img0);
//difference of original picture
img2=difference(img0,img0);
//boudary extraction of original picture
img2=boundary_extraction(img0);
return 0;
}
Download