MATLAB: Hi, Good day. I want ask about when I using this code why keep pop up “Index exceeds matrix dimension”? Thank You.

index exceeds matrix dimension

A = imread('coins.png');
imshow(A)
[centers, radii, metric] = imfindcircles(A,[50 100]);
centersStrong2 = centers(1:2,:);
radiiStrong2 = radii(1:2);
metricStrongs = metric(1:2);
viscircles (centersStrong2, radiiStrong2,'EdgeColor','g');

Best Answer

In the image there is no circles with radius r pixels in the range [50 100]
Hence when you run the code with [50 100] radius range, it reflects error
Index exceeds matrix dimensions.
Error in Mat_answer_july19 (line 7)
centersStrong2 = centers(1:2,:);
As you see whos centers
>> whos centers
Name Size Bytes Class Attributes
centers 0x0 0 double
There is no circle on that range, so it reflects 0, which having 0x0 size, but you try to acess
centersStrong2 = centers(1:2,:);
Rows 1 to 2 and all columns, there is no rows and column data in centers
Solution:
If you change the radius pixel range
A=imread('coins.png');
imshow(A)
[centers, radii, metric] = imfindcircles(A,[20 30]);
%...........................................changed here
centersStrong2 = centers(1:2,:);
radiiStrong2 = radii(1:2);
metricStrongs = metric(1:2);
viscircles (centersStrong2, radiiStrong2,'EdgeColor','g');
If you change the image having cricles with radius r pixels in the range [50 100], it will perfectly works.
Hope it helps!