Select those objects that have holes

I have a binary image in which there are circles and squares.

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');

Some circles and squares have small holes in them, on the basis of which I have to create an image that has only those circles and squares in which there are holes / missing points. How to do it?

PURPOSE: Later, using regionpropsMATLAB, I will extract information from these objects, how many circles and squares.

+4
source share
2 answers

. , . 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:

enter image description here

+2

, , :

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.
+1

Source: https://habr.com/ru/post/1678975/


All Articles