MATLAB CODE FOR MORPHOLOGICAL OPERATIONS 1) EROSION 2) DILATION 3) OPENING 2) CLOSING % EROSION FUNCTION function ero=erosion(im) %% ero = return name % erosion == fun calling %im=imread('text.png'); se=ones(3,3); [p,q]=size(im); imp=zeros(p+2,q+2); imp(2:p+1,2:q+1)=im; for i=2:p+1 for j=2:q+1 im1=zeros(3,3); w=imp(i-1:i+1,j-1:j+1); im1=se*w; imp(i-1,j-1)= min(im1(:)); end end ero=imp(2:p+1,2:q+1); end % DILATION FUNCTION function dil=dilation(im) %% dil = return name % dilation == fun calling %im=imread('text.png'); se=ones(3,3); [p,q]=size(im); imd=zeros(p+2,q+2); imd(2:p+1,2:q+1)=im; for i=2:p+1 for j=2:q+1 im2=zeros(3,3); w=imd(i-1:i+1,j-1:j+1); im2=se*w; imd(i-1,j-1)= max(im2(:)); end end dil=imd(2:p+1,2:q+1); end % MAIN CODE %im=imread('text.png'); im=imread('blobs.png'); subplot 231 imshow(im); title('ORIGINAL IMAGE'); % erosion ero=erosion(im); subplot 232 imshow(ero); title('EROSION'); % dilation dil=dilation(im); subplot 233 imshow(dil); title('DILATION'); % opening dil2=dilation(ero); subplot 234 imshow(dil2); title('OPENING'); % closing ero2=erosion(dil); subplot 235 imshow(ero2); title('CLOSING');