分割出那些有洞的对象
Segment out those objects that have holes in it
我有一张二值图像,里面有圆圈和正方形。
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');
一些圆圈和正方形中有小孔,我必须根据这些小孔生成一个图像,其中只有那些圆圈和正方形中有 holes/missing 个点。我该如何编码?
目的:稍后,在 MATLAB 中使用 regionprops
,我将从这些对象中提取信息,即有多少是圆形和正方形。
可能有更快的方法,但这应该可行:
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.
你应该使用欧拉特性。它是一个拓扑不变量,描述了二维情况下物体中的孔洞数量。您也可以使用 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
输出孔L:
我有一张二值图像,里面有圆圈和正方形。
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');
一些圆圈和正方形中有小孔,我必须根据这些小孔生成一个图像,其中只有那些圆圈和正方形中有 holes/missing 个点。我该如何编码?
目的:稍后,在 MATLAB 中使用 regionprops
,我将从这些对象中提取信息,即有多少是圆形和正方形。
可能有更快的方法,但这应该可行:
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.
你应该使用欧拉特性。它是一个拓扑不变量,描述了二维情况下物体中的孔洞数量。您也可以使用 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
输出孔L: