detecting circle using hough tranform

9 views (last 30 days)
Md
Md on 18 Sep 2022
Commented: Image Analyst on 18 Sep 2022
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.

Answers (3)

Simon Chan
Simon Chan on 18 Sep 2022
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 on 18 Sep 2022
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 Comments
Md
Md on 18 Sep 2022
unfortunately I was instructed to use hough/imfindcircles and this is the only option.
Image Analyst
Image Analyst on 18 Sep 2022
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.

Sign in to comment.


Matt J
Matt J on 18 Sep 2022
Edited: Matt J on 18 Sep 2022
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!