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. 
相關問題