% boundary demo.m % (C) 2006 by Yu Hen Hu % created: Dec. 19, 2006 % demonstrate morphological boundary extraction % clear all, close all A0=imread('myshap4.bmp'); imshow(A0); % a heart shape hand drawing title('original image'); pause % A0 contains mostly 1s and the drawing contains 0s, uint8 A1=1-double(A0); % invert black and white B=ones(3); A2=imopen(imclose(A1,B),B); % fill 1 pixel hole and remove sticks A3=imfill(A2,[100 100]); % fill the interior A=double(A2) + double(A3); imshow(A),title('after interior filling using imfill'); pause Ab=A-double(erode(A,B)); imshow(Ab), title('extracted boundary'); clear A1 A2 A3; Ac=Ab; vidx=[[1:20:280] 280]; Ac(vidx,:)=1; Ac(:,vidx)=1; imshow(Ac)