%fig917.m - Figure 9.17 Gonzales and Woods' book % connected component extraction example % (C) 2003 by Yu Hen Hu % created: 12/2/2003 clear all A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; figure(1),clf,colormap('gray') subplot(131),imagesc(A), x=zeros(size(A)); x(7,4)=1; B=ones(3); k=0; converged=0; while converged==0, k=k+1; subplot(132),imagesc(x), xnew=double(and(dilate(x,B),A)); if sum(sum(xnew-x))==0, converged=1; end disp(['iteration # ' int2str(k)]); x=xnew; pause(1) end subplot(133),imagesc(x),title('extracted connected component')