detecting circle using hough tranform

19 次查看(过去 30 天)
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 个评论
Md
Md 2022-9-18
unfortunately I was instructed to use hough/imfindcircles and this is the only option.
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