MATLAB: Detect Circles and squares on the image using regionprops

Image Processing ToolboxMATLABmorphologysegmantationshape recognition

Now , i have image called "test.png" and my question is how to detect circles with & without holes and display each of them in single figure and here is my code but desn't work with me , so need help
I = imread('test.png');
imshow(I);
I = rgb2gray(I);
I = im2bw(I,0.01);
[L ,num] = bwlabel(I);
stats1 = regionprops (L,'EulerNumber' , 'Area' , 'Perimeter' , 'BoundingBox');
sum = 0;
for R=1:num
% this way desn't work but maybe it just need a little changes
% x = uint8 (stats1(R).BoundingBox(1));
% y = uint8 (stats1(R).BoundingBox(2));
% if (I(x,y) == 0)
% sum = sum + 1;
% bb = stats1(R).BoundingBox;
% rectangle('position',bb,'edgecolor','r','linewidth',1.3);
% end
circularity = (stats1(R).Perimeter .^ 2) ./ (4 * pi * stats1(R).Area);
if (circularity >= 1.1)
sum = sum + 1;
bb = stats1(R).BoundingBox;
rectangle('position',bb,'edgecolor','r','linewidth',1.3);
end
end
disp(sum);

Best Answer

try this
I = imread('test.png');
imshow(I);
I = rgb2gray(I);
I = im2bw(I,0.01);
[L ,num] = bwlabel(I);
stats1 = regionprops (L,'EulerNumber' , 'Area' ,'ConvexArea', 'Perimeter' , 'BoundingBox');
sum1 = 0;
sum2 = 0;
for R=1:num
circularity = (stats1(R).Perimeter .^ 2) ./ (4 * pi * stats1(R).ConvexArea);
if (circularity < 1) %Circles
if stats1(R).EulerNumber == 1
sum1 = sum1 + 1;
bb = stats1(R).BoundingBox;
rectangle('position',bb,'edgecolor','r','linewidth',1.3);
end
if stats1(R).EulerNumber < 1
sum2 = sum2 + 1;
bb = stats1(R).BoundingBox;
rectangle('position',bb,'edgecolor','y','linewidth',1.3);
end
end
end
disp(sum1); %circle without holes
disp
(sum2); %circle with holes