detect Circles and squares on the image using regionprops

10 ビュー (過去 30 日間)
Ahmed Emad
Ahmed Emad 2019 年 12 月 6 日
コメント済み: Image Analyst 2019 年 12 月 6 日
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);

採用された回答

Marcel Kreuzberg
Marcel Kreuzberg 2019 年 12 月 6 日
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
  2 件のコメント
Ahmed Emad
Ahmed Emad 2019 年 12 月 6 日
Thank you very much Marcel Kreuzberg but could you please tell me the difference between 'Area' and 'ConvexArea' ? and explain me the circularity equation because i still misunderstand it. Thank you again!
Image Analyst
Image Analyst 2019 年 12 月 6 日
Convex area does not include holes or "bays" in the outer perimeter. Basically think like if you put a rubber band around your shape. The area inside the rubber band is the convex hull area. The regular area would be the actual area of the white pixels, i.e., it does not include holes.
Circularity is 1 for a circle and higher for non-circular things. If the blob is a perfect circle the perimeter squared equals 4 * pi * the area. The more non-circular and tortuous the shape becomes, the perimeter gets larger faster than the area does and so the circularity metric will get larger. The smallest it can theoretically be is 1 for a perfect circle, though I've seen some as low as 0.9 presumably due to quantization error because we have an image compoaed of square blocks (pixels).
For another demo, see my attached shape recognition demos and see some of the links on the left of this page.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDimensionality Reduction and Feature Extraction についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by