# 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 Erosion without using MATLAB function 'imerode'

In MATLAB, ‘imerode’ is a function used to make the objects thin. MATLAB code without using 'imerode' function and explanation is provided here. The input image is binary.

MATLAB CODE:

A=[1 0 1 1 1; 1 0 1 0 0; 1 1 1 0 0;0 0 1 1 1];
%Structuring element
B=[1 1 0];
%Pad array with ones on both sides
%Intialize the matrix D of size A with zeros
D=false(size(A));
for i=1:size(C,1)
for j=1:size(C,2)-2
In=C(i,j:j+2);
%Find the position of ones in the structuring element
In1=find(B==1);
%Check whether the elements in the window have the value one in the
%same positions of the structuring element
if(In(In1)==1)
D(i,j)=1;
end
end
end
display(D);

Explanation:
1.     Consider a matrix A and a structuring element B.
2.     Initialize a matrix D of size A with zeros.
3.     Construct a window of size B with the elements of matrix A.
4.     Check whether the ones in the structuring element B overlap the ones in the window.
5.     If it overlaps, then update D with one else zero.

Example 2:

figure,imshow(A);
 Original Image

%Structuring element
B=getnhood(strel('disk',11));

m=floor(size(B,1)/2);
n=floor(size(B,2)/2);
%Pad array on all the sides
%Intialize a matrix with size of matrix A
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)=min(min(Temp-B));

end
end
figure,imshow(~D);

 After Erosion

Like "IMAGE PROCESSING" page

### 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