detecting circle using hough tranform

3 ビュー (過去 30 日間)
Md
Md 2022 年 9 月 18 日
コメント済み: Image Analyst 2022 年 9 月 18 日
I am trying to detect circle using hough transform using the image above. My code is
RGB = imread('img1.jpg');
imshow(RGB);
Rmin = 60; Rmax = 100;
[center, radius] = imfindcircles(RGB, [Rmin Rmax], 'Sensitivity', 0.9)
viscircles(center, radius);
hold on;
plot(center(:,1), center(:,2), 'yx', 'LineWidth', 2);
hold off;
It's showing error saying
"Index in position 2 exceeds array bounds.
Error in hough (line 7)
plot(center(:,1), center(:,2), 'yx', 'LineWidth', 2);"
Can I get help to fix this? Thanks in advance.

回答 (3 件)

Simon Chan
Simon Chan 2022 年 9 月 18 日
The range of radius is too small and the sensitivity may not be good enough.
I choose radius from 100 to 150 pixels and sensitivity 0.98 as follows:
RGB=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1128355/image.png');
imshow(RGB);
Rmin = 100; Rmax = 150;
[center, radius] = imfindcircles(RGB, [Rmin Rmax], 'Sensitivity', 0.98)
center = 2×2
815.0664 742.2900 434.8299 613.4308
radius = 2×1
143.3625 127.1976
viscircles(center, radius);
hold on;
plot(center(:,1), center(:,2), 'yx', 'LineWidth', 2);
hold off;

Image Analyst
Image Analyst 2022 年 9 月 18 日
center is probably empty. To correct, try changimg some of the input parameters. But why use hough/imfindcircles? For the image you showed, you can easily find the circles using the ColorThresholder (which does thresholding) or simply thresholding the green channel.
  2 件のコメント
Image Analyst
Image Analyst 2022 年 9 月 18 日
Oh, you didn't say it was your homework.
I've now tagged it as homework.
So what did you think of @Simon Chan's answer? It seems to work, following along the lines of your code.

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


Matt J
Matt J 2022 年 9 月 18 日
編集済み: Matt J 2022 年 9 月 18 日
I find that imfindcircles is often hard to parameter-tune. Here's an alternative solution using this circle-fitting tool,
A=imread('Image.png');
BW=bwareaopen(A(:,:,2)>150,500);
bd=bwboundaries(bwconvhull(BW,'objects'));
clear center radius;
imshow(A);hold on
for i=numel(bd):-1:1
xy=fliplr(bd{i})';
fitobj=circularFit(xy);
center{i}=fitobj.center;
radius{i}=fitobj.radius;
fitobj.showfit(LineWidth=4);
end; hold off

Community Treasure Hunt

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

Start Hunting!

Translated by