Another possibility is to use the BWBOUNDARIES function, which:
monitors the external borders of objects, as well as the boundaries of the hole inside these objects
This information is contained in the fourth issue of A , an adjacency matrix that represents dependencies on parent and child holes.
%# read binary image bw = imread('SUvif.png'); %# find all boundaries [B,L,N,A] = bwboundaries(bw, 8, 'holes'); %# exclude inner holes [r,~] = find(A(:,N+1:end)); %# find inner boundaries that enclose stuff [rr,~] = find(A(:,r)); %# stuff they enclose idx = setdiff(1:numel(B), [r(:);rr(:)]); %# exclude both bw2 = ismember(L,idx); %# filled image %# compare results subplot(311), imshow(bw), title('original') subplot(312), imshow( imfill(bw,'holes') ), title('imfill') subplot(313), imshow(bw2), title('bwboundaries')

Amro
source share