Clear Filters
Clear Filters

filling Index issues imfindcircles

13 views (last 30 days)
Damon
Damon on 20 Jul 2024 at 17:13
Commented: Damon on 21 Jul 2024 at 14:21
Im having issue where i think the index is looking for instances of detected circles but it either is not filling the index or maybe there isnt enough to fill the array but im having issue getting this to work. its from the help documentation so i expected it to just work but im still having trouble and thus am having trouble understanding how to use imfindcircles. I tried using edge(canny) and using a binary image to see if that would make it easier to find circles but im still hvaing issues.
A = imread('coins.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');

Accepted Answer

LeoAiE
LeoAiE on 20 Jul 2024 at 19:16
Hi,
starts by reading the image and converting it to grayscale if it is not already. To enhance the visibility of features, adjusts the image contrast and applies Canny edge detection. Circle detection is then performed in two passes with different radius ranges ([15 30] and [30 50]) to capture circles of varying sizes. The detected circles from both passes are combined and sorted by their strength metric to prioritize the most confidently detected circles. Finally, displays the original image and overlays the strongest detected circles, ensuring that a specified number of circles are shown, thereby improving the overall detection accuracy for all the coins in the image.
% Read the image
A = imread('coins.png');
% Check if the image is already grayscale
if size(A, 3) == 3
% Convert to grayscale if it is an RGB image
grayA = im2gray(A);
else
% Use the image as is if it's already grayscale
grayA = A;
end
% Adjust contrast
adjustedA = imadjust(grayA);
% Apply edge detection
edgesA = edge(adjustedA, 'canny');
% First pass of circle detection
[centers1, radii1, metric1] = imfindcircles(edgesA, [15 30], 'Sensitivity', 0.9);
% Second pass of circle detection with a different range
[centers2, radii2, metric2] = imfindcircles(edgesA, [30 50], 'Sensitivity', 0.9);
% Combine results from both passes
centers = [centers1; centers2];
radii = [radii1; radii2];
metric = [metric1; metric2];
% Sort circles based on metric
[~, sortedIndices] = sort(metric, 'descend');
centers = centers(sortedIndices, :);
radii = radii(sortedIndices);
metric = metric(sortedIndices);
% Check if enough circles are found
numCircles = min(10, length(radii)); % Adjust the number of circles to display as needed
% Display the original image
imshow(A);
hold on;
% Display the strongest circles
if numCircles > 0
centersStrong = centers(1:numCircles, :);
radiiStrong = radii(1:numCircles);
metricStrong = metric(1:numCircles);
viscircles(centersStrong, radiiStrong, 'EdgeColor', 'b');
else
disp('No circles detected.');
end
hold off;

More Answers (1)

Image Analyst
Image Analyst on 21 Jul 2024 at 0:13
I don't know what this means: "not filling the index or maybe there isnt enough to fill the array". The demo and your code does exactly what it says - it finds all the circles and displays the "strongest" 5 circles only.
A = imread('coins.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');
If you want to display all circles, just display them all, not just 5 of them:
A = imread('coins.png');
imshow(A)
[centers, radii, metric] = imfindcircles(A,[15 30]);
viscircles(centers, radii,'EdgeColor','b');
So I'm not really sure what you're asking (because I couldn't understand that phrase I mentioned at the beginning.) Please explain more clearly.
For a more complete demo, see my Image Segmentation Tutorial in my File Exchange:
It's a generic, general purpose demo of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters.
  1 Comment
Damon
Damon on 21 Jul 2024 at 14:21
Strange when i run it the error i get is:
Index in position 1 exceeds array bounds.
Error in TestingScript (line 4)
centersStrong5 = centers(1:5,:);

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!