可以使用bwboundaries找到白色連接的部件,使得每個連接組件具有相應的一組邊界的像素邊界。
%calculate boundries and generate boundry mask
B = bwboundaries(im,'noholes');
boundriesImage = zeros(size(im));
boundriesPixels = cell2mat(B);
boundriesImage(sub2ind(size(im),boundriesPixels(:,1),boundriesPixels(:,2)))=1;
%finds the connected component in the original image and in the boundry
%mask
CC = bwconncomp(im);
CC2 = bwconncomp(boundriesImage);
結果:CC和CC2包含相同數量的連接部件
CC =
Connectivity: 8
ImageSize: [535 1571]
NumObjects: 814
PixelIdxList: {1x814 cell}
CC2 =
Connectivity: 8
ImageSize: [535 1571]
NumObjects: 814
PixelIdxList: {1x814 cell}
而且,每個連接部件CC2的{II}匹配到它的CC {II}如可以通過以下的試驗中可以看出結果:
%tests that for each ii, CC{ii} is contained in CC{i}
CC2MatchesToCC1 = true;
for ii=1:length(CC.PixelIdxList)
if length(intersect(CC2.PixelIdxList{ii},CC.PixelIdxList{ii}))~=length(CC2.PixelIdxList{ii})
CC2MatchesToCC1 = false;
end
end
結果:
CC2MatchesToCC1 =
1