Thursday, October 25, 2012

Matlab code for Closing - Erosion after Dilation


%closeing

clear all
clc
a=imread('circbw.tif');
p=size(a);

%%% using in built MATLAB function %%%

s=strel('square',3);
d1=imdilate(a,s);
d2=imerode(a,s);

%%% Writing program
w=[1 1 1;1 1 1;1 1 1];
for x=2:1:p(1)-1
    for y=2:1:p(2)-1
         a1=[w(1)*a(x-1,y-1) w(2)*a(x-1,y) w(3)*a(x-1,y+1) w(4)*a(x,y-1) w(5)*a(x,y) w(6)*a(x,y+1) w(7)*a(x+1,y-1) w(8)*a(x+1,y) w(9)*a(x+1,y+1)];
         A2(x,y)=max(a1); %Dilation%
       
    end
end
q=size(A2)
for i=2:1:q(1)-1
    for j=2:1:q(2)-1
        a11=[w(1)*A2(i-1,j-1) w(2)*A2(i-1,j) w(3)*A2(i-1,j+1) w(4)*A2(i,j-1) w(5)*A2(i,j) w(6)*A2(i,j+1) w(7)*A2(i+1,j-1) w(8)*A2(i+1,j) w(9)*A2(i+1,j+1)];
         A1(i,j)=min(a11); %Erosion after dilation closing%
       
    end
end
figure(1)
subplot(3,2,1)
imshow(a)
title('input image');
subplot(3,2,2)
imshow(A1)
title('Close');

No comments:

Post a Comment