IMAGE PROCESSING

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Image Dilation without using 'imdilate' function

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
%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:
 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
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);

 Dilated Image

Example 3:(Method 2)

A=im2bw(A);
%Structuring element
B=[1 1 1 1 1 1 1;];
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

Like "IMAGE PROCESSING" page

vishal said...

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));

AMAL IT said...

how can get link explain bwareaopen in same way.

Sam Sung said...

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

Theo said...

how about for grey level images?

Ali Arslan said...

thanks man. you are number one