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
C=padarray(A,[0
1],1);
%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:
A=imread('circles.png');
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
C=padarray(A,[m n],1);
%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 |
14 comments:
size(C,1)-(m-1)
size(C,2)-(m-1)
sir can u explain what it does????
@vishal
Depending on the size of the structuring element,the window is adjusted.Here the size of the structuring element is stored in the variable m.
Do you have the same code in C++?
Can you please explain y do we do Temp-B1 before we take the minimum?
I have been getting errors for other images at the line min(min(Temp-B1))
Thanks in advance :)
if(In(In1)==1) <- I don't understand it :(
@Unknown
It is a typo.It should be min(min(Temp-B)). I corrected the code. Kindly check it now. The explanation can be found at the 7th step in the flow chart.
@Manh Cuong Nguyen
This line checks whether the ON pixels in the sliding window overlaps with the structuring elements with value one.
@Aaron Angel
hello thanx for uploading tutorial but it is still giving an error :(
@shahab lodhi
Can you share the error message here.
D(i,j)=min(min(Temp-B))
Error using -
Integers can only be combined with integers of the same class, or scalar doubles.
B is class logical and Temp is class unit8.
I solved this error for my particular problem.
for i=1:size(C,1)-(2*m)
for j=1:size(C,2)-(2*n)
Temp=window(i:i+(2*m),j:j+(2*n));
D(i,j)=min(min(Temp)); %this is where the error occured, now it delivers an inverted image
end
end
D = 1- D; %invert the image to get the result
my code changes the black background to cyan ... I don't know why
@Unknown
These morphological operations are performed on binary images. If the input image is grayscale or RGB then convert it into binary image.
same error
Enjoyed Reading? Share Your Views