How to detect the center of the circles in this picture

5 Ansichten (letzte 30 Tage)
Claire Pottier
Claire Pottier am 31 Aug. 2021
Kommentiert: Claire Pottier am 1 Sep. 2021
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
Claire Pottier
Claire Pottier am 31 Aug. 2021
Bearbeitet: Claire Pottier am 31 Aug. 2021
Hi,
For the moment I am using kmeans to determine the center of a cluster generated with that code:
%Image treatment
[edges_prewitt2, edge_threshold] = edge(rgb2gray(im), 'Roberts');
%% We can maipulate the value of edge_threshold
sensitivity = edge_threshold + 0.06;
edges_prewitt2 = edge(rgb2gray(im), 'Sobel', sensitivity);
imshow(edges_prewitt2);
title(sprintf('Sensitivity: %.03f', sensitivity));
%% Treatment Peter's algo (Find the center of the target)
BW_black = edges_prewitt2;
BW_white = imcomplement(BW_black);
%Find bounding box, area and centroid for black and white pictures
s_white = regionprops(BW_white,'basic');
s_black = regionprops(BW_black,'basic');
%Center
centroids_w = cat(1,s_white.Centroid);
centroids_b = cat(1,s_black.Centroid);
all_centroids = cat(1,centroids_w,centroids_b);
for x = 1:length(all_centroids)
for y = 1:length(all_centroids)
deltas(x,y)= norm(all_centroids(x,1:2) - all_centroids(y,1:2));
end
end
deltas = deltas<0.75;
targets= sum(deltas,2)>1;
centroids=all_centroids(targets,:);
[rows, columns, numberOfColorBands] = size(BW_white);
for i = 1:length(centroids)
hold on;
plot(centroids(i, 1),centroids(i, 2), '+r', 'MarkerSize', 30, 'LineWidth', 2);
end
try
[idx,C,sumd,D] = kmeans(centres,5);
catch
fprintf('Threshold too low \n');
C = centroids;
end
f = figure;
imshow(edges_prewitt2);
for i = 1:length(C)
hold on;
plot(C(i, 1),C(i, 2), '+r', 'MarkerSize', 30, 'LineWidth', 2);
end
hold off;
With this code I cannot detect the center of all the circles.
I tried another method, similar to that one but with changing the constrast of the picture.
What do you mean by ovals?
Claire Pottier
Claire Pottier am 31 Aug. 2021
Hi Matt J,
I want the centers of the ellipses.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
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
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.
Claire Pottier
Claire Pottier am 1 Sep. 2021
orry if my answer was rude. I didn't expect you to find the solution for me.
Thanks for your help. I will take a closer look at your script and also take a look at Segnet.
Thanks for your time and help.

Melden Sie sich an, um zu kommentieren.

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!

Translated by