分割出其中有孔的对象 [英] Segment out those objects that have holes in it
问题描述
我有一个二进制图像,其中有圆圈和正方形.
I have a binary image, that has circles and squares 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');
某些圆形和正方形在其中具有小孔,因此,我必须生成一个图像,该图像仅包含其中具有孔/缺失点的那些圆形和正方形.我该如何编码?
Some circles and squares have small holes in them based on which, I have to generate an image which has only those circles and squares that have holes/missing point in them. How can I code that?
目的:稍后,在MATLAB中使用regionprops
,我将从这些对象中提取出多少个圆和正方形的信息.
PURPOSE: Later on, using regionprops
in MATLAB, I will extract the information that from those objects, how many are circles and squares.
推荐答案
您应该使用Euler特性.这是一个拓扑不变量,它描述了2D情况下对象中的孔数量.您也可以使用regionprops来计算它:
You should use the Euler characteristic. It's a topological invariant which describes the amount of holes in a object in the 2D case. You can calculate it using regionprops too:
STATS = regionprops(L, 'EulerNumber');
任何不带孔的单个对象的欧拉特征为1,任何带1个孔的单个对象的欧拉特征为0,两个孔-> -1等.因此,您可以用EC <; 1.计算也非常快.
Any single object with no holes will have an Euler characteristic of 1, any single object with 1 hole will have an Euler characteristic of 0, two holes -> -1 etc. So you can segment out all the objects with EC < 1. It's pretty fast to calculate too.
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:
这篇关于分割出其中有孔的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!