Index in position 1 exceeds array bounds

Hi, I am running the Circular Hough Transform code (from Matlab) with different image inputted. However there is an error popping out mentioned that "Index in position 1 exceeds array bounds". I don know what is the reason behind it. Please help me on this !
A = imread('circle.png');
imshow(A);
[centers, radii, metric] = imfindcircles(A,[15 30]);
centersStrong5 = centers(1:5,:);
radiiStrong5 = radii(1:5);
metricStrong5 = metric(1:5);
viscircles(centersStrong5, radiiStrong5,'EdgeColor','b');

2 Comments

What line is causing the error?
Sharing the error message is helpful but you should share the entire copy-pasted error message and indicate which line is causing the error.
"Index in position 1 exceeds array bounds" is quite informative. It means you're asking for values that don't exist. Here's an example:
a = [1 2 3 4 5]
a(6) % But there is only 5 values! Error!
Hi,
I think I pasted the whole error that pop out on my command window. Only "Index in position 1 exceeds array bounds" is shown which put me in a confuse situation.
But in this error, which line is represented as position 1 ?

Sign in to comment.

 Accepted Answer

If imfindcircles finds less than 5 circles you are going to error.
A = imread('circle.png');
imshow(A);
[centers, radii, metric] = imfindcircles(A,[15 30]);
a=max(5,length(centers));
centersStrong5 = centers(1:a,:);
radiiStrong5 = radii(1:a);
metricStrong5 = metric(1:a);
viscircles(centersStrong5, radiiStrong5,'EdgeColor','b');

5 Comments

Thanks for your prompt response.
Just to make it clear, if i would like to make the code know that any conditions below 5 circles detected are acceptable, which line changes should i make it ?
In another words, which line of this code is causing the issue that provides the condition that imfindcircles finds less than 5 circles will pop out the error.
a=max(5,length(centers));
Suppose that 3 centers are found, then max(5,3) is 5. If 27 centers were found, max(5,27) would be 27. Therefore your max() call will not return a value less than 5, and will return all of the length if there is more.
centersStrong5 = centers(1:a,:);
and there you try to index at least 5 even if there were only 3.
If you want at most 5 but fewer if that is all there is, then use min() instead of max()
Yes, my mistake. Should have said,
a=min(5,length(centers));
I keep getting caught myself in thinking that the operation "at least so-many" should be a min() operation, because you want a minimum of so-many.
Yea. You solve that issue for me.
But sadly, when I try it with other image it doens't seems like working even though there is no error. I changed the sensitivy and radius, but the output is the same. No circle detected.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!