Thursday, October 25, 2012

Matlab Code for OPENING - Erosion after Dilation


%opening

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


%%% 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)];
         A1(x,y)=max(a1); %Dilation%
         A2(x,y)=min(a1); % Erosion%
    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)];
         A3(i,j)=max(a11); %Dilation after erosion opening  %
       
    end
end
figure(1)
subplot(3,2,1)
imshow(a)
title('input image');
subplot(3,2,2)
imshow(A3)
title('OPEN');

No comments:

Post a Comment