
Imfindcircles detect multiple time the same circle
1 view (last 30 days)
Show older comments
Hi,
I want to use imfindcircles for an augmented reality project. But I have an issue with the function. First I want to detect the circles on my image (there are just black circles on a white background, image vision2.png) but if I don't put a very high sensitivity it just detects nothing, so that's why I have a sensitivity at 0.99 to be sure that it detects my 8 circles.
But then I need to detect the same 8 circles on a picture of my image (points.png), but when I do the same thing that before it detects false circles and even multiple times the same circle. I tried to reduce the sensibility but below 0.98 it doesn't detect anything.
Does someone knows what I'm doing wrong ?
Here is my code:
% Opening my image and looking for circles
I = imread('vision2.png');
imshow(I)
[centers, radii] = imfindcircles(I,[50 150],'ObjectPolarity','dark','Sensitivity',0.99);
viscircles(centers(1:8,:), radii(1:8),'EdgeColor','b');
m = centers(1:8,:);
% Opening the picture of my image and looking for circles
I2 = imread('points.png');
[centers, radii] = imfindcircles(I2,[50 150],'ObjectPolarity','dark','Sensitivity',0.99)
viscircles(centers(1:8,:), radii(1:8),'EdgeColor','b');
M = centers(1:8,:);
0 Comments
Accepted Answer
Image Analyst
on 21 Dec 2022
Edited: Image Analyst
on 21 Dec 2022
I wouldn't even use imfindcircles. You can find those circles quite easily just using simple thresholding. Then call regionprops to find number, diameter, area, centroid, brightness, or whatever you want. Let me know if you can't figure it out.
% Demo by Image Analyst
% Finds betti numbers for an image which is defined by Wikiepdia at
% "tells us the maximum number of cuts that can be made before separating a surface into two pieces"
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'vision2.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgb2gray(grayImage);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 1, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Binarize the image
threshold = 128;
mask = grayImage <= threshold;
subplot(2, 1, 2);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Find circles
props = regionprops(mask, 'EquivDiameter', 'Centroid')
% Plot circles over image
xy = vertcat(props.Centroid)
diameters = [props.EquivDiameter]
hold on;
viscircles(xy, diameters/2)
% Plot centroids with red crosshairs
plot(xy(:, 1), xy(:, 2), 'r+', 'MarkerSize', 30, 'LineWidth', 2)

More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!