4

我有一个二进制图像,它有圆形和正方形。分割那些有洞的对象

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

您应该使用欧拉特征。这是一个拓扑不变量,它描述了2D情况下物体的孔数量。可以使用regionprops太计算它:

STATS = regionprops(L, 'EulerNumber'); 

与没有孔的任何单个对象将具有为1的欧拉特性,用1个孔任何单个对象将具有0,两个孔欧拉特性 - > -1等。所以你可以用EC <来分割出所有的对象。它的计算速度也相当快。

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 

输出holeL:

enter image description here

+0

太谢谢你了! (竖起大拇指) – sana

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. 
相关问题