How to detect the center of the circles in this picture
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Good morning,
I tried several methods such as findcircles and edge but none of them were satisfactory. I would like to have the coordinates of the centres of each circle in the image above.

Thanks for your help.
4 Kommentare
Antworten (1)
Image Analyst
am 31 Aug. 2021
Try this:
% Demo by Image Analyst.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image. This is a horrible image. NEVER use JPG format for image analysis. Use PNG, TIFF, or BMP instead.
folder = [];
baseFileName = 'circles on floor.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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('Original RGB Image : "%s"\n%d rows by %d columns', baseFileName, rows, columns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
grayImage = rgb2gray(rgbImage);
% Display the histogram of the gray scale image.
subplot(2, 2, 2);
imhist(grayImage);
grid on;
% Create a binary image of the circle.
threshold = 160;
binaryImage = grayImage > threshold;
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
caption = sprintf('Initial Mask Image : %d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get rid of blobs touching the border.
binaryImage = imclearborder(~binaryImage);
subplot(2, 2, 4);
imshow(binaryImage);
% Get convex hull and fill holes
binaryImage = bwconvhull(binaryImage, 'objects');
imshow(binaryImage);
% There is still a blob in the lower left that's a bright spot that is not the paper with the circles on it.
% Try to get the paper, which we'll assume is the largest white blob.
paperMask = imdilate(binaryImage, true(9)); % Merge close together circles
paperMask = bwconvhull(paperMask, 'objects');
paperMask = bwareafilt(paperMask, 1); % Take largest blob only.
imshow(paperMask);
% And it with the all blobs image to get only the spots that are on the paper.
binaryImage = binaryImage & paperMask;
imshow(binaryImage);
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
imshow(coloredLabelsImage);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get the centroids.
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
props = regionprops(labeledImage, 'Centroid', 'Area');
numberOfBlobs = size(props, 1);
allAreas = [props.Area]
xy = vertcat(props.Centroid)
% Plot cross hairs are every centroid.
hold on;
for k = 1 : numberOfBlobs
plot(xy(k, 1), xy(k, 2), 'r+', 'LineWidth', 2);
end
message = sprintf('Done!');
msgbox(message);

3 Kommentare
Image Analyst
am 31 Aug. 2021
Yes I suspected it wouldn't work for all images but it gives you a start. Feel free to adapt the code to get it to be more robust. Sorry, but I don't have the time to go on this journey with you. It could take many hours, days, or weeks to make it fully robust. Perhaps you might want to try Segnet (deep learning) to find the target.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

