分割出那些有洞的对象

时间:2017-06-11 07:06:43

标签: matlab image-processing image-segmentation image-morphology

我有一个二进制图像,里面有圆圈和正方形。

enter image description here

imA = imread('blocks1.png');

A = im2bw(imA);

figure,imshow(A);title('Input Image - Blocks');
imBinInv = ~A;
figure(2); imshow(imBinInv); title('Inverted Binarized Original Image');

有些圆形和正方形有小孔,基于此,我必须生成一个图像,其中只有那些圆圈和正方形中有孔/缺失点。我该怎么编码呢?

目的:稍后,在MATLAB中使用regionprops,我将从这些对象中提取信息,有多少是圆形和正方形。

2 个答案:

答案 0 :(得分:2)

你应该使用欧拉特征。它是拓扑不变量,用于描述2D情况下对象中的孔数量。您也可以使用regionprops计算它:

STATS = regionprops(L, 'EulerNumber');

任何没有孔的单个物体的欧拉特性为1,任何一个具有1个孔的单个物体的欧拉特性为0,两个孔 - > -1等等所以你可以用EC< 1.计算速度也很快。

imA = imread('blocks1.png');

A = logical(imA);
L = bwlabel(A); %just for visualizing, you can call regionprops straight on A

STATS = regionprops(L, 'EulerNumber');

holeIndices = find( [STATS.EulerNumber] < 1 ); 

holeL = false(size(A));
for i = holeIndices
    holeL( L == i ) = true;
end

输出孔:

enter image description here

答案 1 :(得分:1)

可能有更快的方法,但这应该有效:

Afilled = imfill(A,'holes'); % fill holes
L = bwlabel(Afilled); % label each connected component
holes = Afilled - A; % get only holes
componentLabels = unique(nonzeros(L.*holes)); % get labels of components which have at least one hole
A = A.*L; % label original image
A(~ismember(A,componentLabels)) = 0; % delete all components which have no hole
A(A~=0)=1; % turn back from labels to binary - since you are later continuing with regionprops you maybe don't need this step.