Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DIP-LAB#13: MORPHOLOGICAL OPERATIONS

OBJECTIVE:
The objective of this lab is to implement morphological operations on images.

INTRODUCTION:
Morphology is a branch of biology that deals with form and structure of animal and plant. In
image processing, we use mathematical morphology to extract image components which are
useful in representation and description of region shape such as Boundaries, Skeletons,
Convex hull, Thinning, Pruning etc.

THE STRUCTURING ELEMENT:

The structuring element is a small matrix of pixels akin to the mask used in filtering
operations you studied in an earlier experiment. The dimension of this matrix specifies the
size of the structuring element and spatial organization of pixel value specifies the shape of
the structural element. The size and shape of the structuring element decides the effect of
operation on the objects in the image.
THEORETICAL CONSIDERATIONS:

The two principal morphological operations are dilation and erosion. Dilation allows objects
to expand, thus potentially filling in small holes and connecting disjoint objects. Erosion
shrinks objects by etching away (eroding) their boundaries. These operations can be
customized for an application by the proper selection of the structuring element, which
determines exactly how the objects will be dilated or eroded.

1. THE DILATION:
The dilation process is performed by laying the structuring element B on the image A and
sliding it across the image in a manner similar to convolution (will be presented in a next
laboratory). The difference is in the operation performed.
It is best described in a sequence of steps:
 If the origin of the structuring element coincides with a 'background' pixel in the image,
there is no change; move to the next pixel
 If the origin of the structuring element coincides with an 'object' pixel in the image, make
(label) all pixels from the image covered by the structuring element as ‘object’ pixels.
NOTATION:

The structuring element can have any shape. Typical shapes are presented below:

2. THE EROSION:
The erosion process is similar to dilation, but we turn pixels to 'background', not 'object'. As
before, slide the structuring element across the image and then follow these steps:
 If the origin of the structuring element coincides with a 'background' pixel in the image,
there is no change; move to the next pixel.
 If the origin of the structuring element coincides with an 'object' pixel in the image, and
any of the 'object' pixels in the structuring element extend beyond the 'object' pixels in the
image, then change the current 'object' pixel in the image (above you have positioned the
structuring element center) to a 'background' pixel.

NOTATION:
AΘB
In below Fig the only remaining pixels are those that coincide to the origin of the structuring
element where the entire structuring element was contained in the existing object.

TASK#01:
Write down a program to implement Erosion, note down its effect on binary image.
Compare your results by using builtin function ‘imerode’.
MATLAB CODE:
% Erosion
clc
clear all
close all
I=imread('text.png');
subplot(221)
imshow(I)
title('Original Image')
%Structuring element
B=[ 1 1 1];
%Pad array with ones on both sides
C=padarray(I,[0,1]);
D=false(size(I));
for i=1:size(I,1)
for j=1:(size(I,2)-2)
L=C(i,j:j+2);
%Find the position of ones in the structuring element
k=find(B==1);
if (L(k)==1);
D(i,j)=1;
end
end
end
subplot(222)
imshow(D)
title('Erosion Image')
% By built-in function
E=imerode(I,B);
subplot(223)
imshow(E)
title('Erosion by Built-in Function')
OUTPUT:
COMMENT:
From the above output it has been seen that Erosion is performed on images manually as well
as by built-in function, the results are same in both cases.

TASK#02: Write down a program to implement Dilation, note down its effect on
binary image. Compare your results by using builtin function ‘imdilate’.

MATLAB CODE:
clc
close all
clear all
Img=imread( 'text.png' );
A=im2bw(Img);
subplot(221)
imshow(Img)
title('Original Image')
%Structuring element
B=[1 1 1 1 1 1 1;];
C=padarray(Img,[0 3]);
D=false(size(Img));
for i=1:size(C,1)
for j=1:size(C,2)-6
D(i,j)=sum(B&C(i,j:j+6));
end
end
subplot(222)
imshow(D);
title('Dilated Image')
% By built-in function
BW1 = imdilate(Img,B);
subplot(223)
imshow(BW1)
title('Dilation by Built-in Function')
OUTPUT:
COMMENT:
From the above output it has been seen that Dilation is performed on images manually as well
as by built-in function, the results are same in both cases.

TASK#03:
Write down a program to implement Closing, note down its effect on binary image.
Compare your results by using builtin function ‘imclose’. (Note: Closing of set A by
set B is achieved by first dilating the set A by B, then eroding the resulting set by B.)

MATLAB CODE:
clc
clear all
close all
A=imread( 'text.png' );
A=im2bw(A);
subplot(221)
imshow(A)
title('original Image')
%Structuring element
B=[1 1 1 1 1 1 1;];
C=padarray(A,[0 3]);
D=false(size(A));
for i=1:size(C,1)
for j=1:size(C,2)-6
D(i,j)=sum(B&C(i,j:j+6));
end
end
subplot(222)
imshow(D);
title('Dilated Image')
D=im2bw(D);
%Pad array with ones on both sides
C1=padarray(D,[0,1],1);
D1=false(size(D));
for ii=1:size(C1,1)
for jj=1:size(C1,2)-6
L=C1(ii,jj:jj+6);
%Find the position of ones in the structuring element
k=find(B==1);
if (L(k)==1)
D1(ii,jj)=1;
end
end
end
subplot(223)
imshow(D1)
title('Closing')
% By builtin function
E=imclose(D1,B);
subplot(224)
imshow(E)
title('Closing by Built-in Function')
OUTPUT:

COMMENT:
From the above output it has been seen that Closing is performed on images manually as well
as by built-in function, the results are same in both cases.

TASK#04:
Write down a program to implement Opening, note down its effect on binary image.
Compare your results by using builtin function ‘imopen’. (Note: Opening of set A by
set B is achieved by first eroding the set A by B, then dilating the resulting set by B.)

MATLAB CODE:
clc
clear all
close all
A=imread( 'text.png' );
A=im2bw(A);
subplot(221)
imshow(A)
title('Original Image')
%Structuring element
B=[1 1 1];
%Pad array with ones on both sides
C=padarray(A,[0,1],1);
D=false(size(A));
for i=1:size(C,1)
for j=1:size(C,2)-6
L=C(i,j:j+6);
%Find the position of ones in the structuring element
k=find(B==1);
if (L(k)==1)
D(i,j)=1;
end
end
end
subplot(222)
imshow(D)
title('Erode Image')
D=im2bw(D);
C1=padarray(D,[0 3]);
D1=false(size(D));
for i=1:size(C1,1)
for j=1:size(C1,2)-6
D1(i,j)=sum(B&C1(i,j:j+2));
end
end
subplot(223)
imshow(D1);
title('Opening')
% By builtin function
E=imopen(D1,B);
subplot(224)
imshow(E)
title('Opening by Built-in Function')
OUTPUT:
COMMENT:
From the above output it has been seen that Opening is performed on images manually as
well as by built-in function, the results are same in both cases.

You might also like