Lets Learn together... Happy Reading

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

like button Like "IMAGE PROCESSING" page

14 comments:

vishal said... Reply to comment

size(C,1)-(m-1)
size(C,2)-(m-1)
sir can u explain what it does????

Aaron Angel said... Reply to comment

@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.

Unknown said... Reply to comment

Do you have the same code in C++?

Unknown said... Reply to comment

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

Unknown said... Reply to comment

if(In(In1)==1) <- I don't understand it :(

Aaron Angel said... Reply to comment

@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.

Aaron Angel said... Reply to comment

@Manh Cuong Nguyen

This line checks whether the ON pixels in the sliding window overlaps with the structuring elements with value one.

Unknown said... Reply to comment

@Aaron Angel
hello thanx for uploading tutorial but it is still giving an error :(

Aaron Angel said... Reply to comment

@shahab lodhi

Can you share the error message here.

Unknown said... Reply to comment

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.

Unknown said... Reply to comment

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

Unknown said... Reply to comment

my code changes the black background to cyan ... I don't know why

Aaron Angel said... Reply to comment

@Unknown
These morphological operations are performed on binary images. If the input image is grayscale or RGB then convert it into binary image.

Unknown said... Reply to comment

same error

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com