In MATLAB, ‘imdilate’is the function that dilates
the image using a structuring element. Let’s learn how this function works
using some examples and codes.

MATLAB CODE:

Example 1:

A=[1 0 0 0 1; 1 0 1 0
0; 1 1 1 0 0;0 0 1 1 1];

%Structuring
element

B=[1 0 0; 0 1 0; 0 0
1];

%Pad zeros on
all the sides

C=padarray(A,[1 1]);

%Intialize a
matrix of matrix size A with zeros

D=false(size(A));

for
i=1:size(C,1)-2

for
j=1:size(C,2)-2

%Perform
logical AND operation

D(i,j)=sum(sum(B&C(i:i+2,j:j+2)));

end

end

display(D);

Example 2:

A=imread('text.png');

Original Image |

A=im2bw(A);

%Structuring element

B2=getnhood(strel('line',7,90));

m=floor(size(B2,1)/2);

n=floor(size(B2,2)/2);

%Pad array on
all the sides

C=padarray(A,[m n]);

D=false(size(A));

for
i=1:size(C,1)-(2*m)

for
j=1:size(C,2)-(2*n)

Temp=C(i:i+(2*m),j:j+(2*n));

D(i,j)=max(max(Temp&B2));

end

end

figure,imshow(D);

Example 3:(Method 2)

A=imread('text.png');

A=im2bw(A);

%Structuring element

%Structuring element

B=[1 1 1 1 1 1 1;];

C=padarray(A,[0 3]);

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

figure,imshow(D);

Dilated image |

## 5 comments:

sir,this error is coming....plz help..thnx in advance..

??? Assignment has more non-singleton rhs dimensions than non-singleton

subscripts

Error in ==> dil at 10

D(i,j)=sum(B&C(i:i+6,j));

how can get link explain bwareaopen in same way.

Do you have the c++ code for example 2?

how about for grey level images?

thanks man. you are number one

## Enjoyed Reading? Share Your Views