Calculating the coordinates for the center of a circle in an image


<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/KxJEJ.jpg" data-original="https://i.stack.imgur.com/KxJEJ.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Say I have this image and I want to get the center of each circle in (X , Y).

Is there an algorithm to do so in MatLab??


Here is a result using cross-correlation with artificial circle as a filter. The result is [row, column] from upper left corner:

>> disp(centers) 483 1347 460 662 478 1001 451 290

There is no detailed comments, please ask it required.

im = rgb2gray(im2double(imread('D:/temp/circles.jpg'))); r = 117; % define radius of circles thres_factor = 0.9; % see usage %% [x, y] = meshgrid(-r : r); f = sqrt(x .^ 2 + y .^ 2) >= r; %% im = im - mean(im(:)); im = im / std(im(:)); f = f - mean(f(:)); f = f / std(f(:)) / numel(f); imf_orig = imfilter(im, f, 'replicate'); %% search local maximas imf = imf_orig; [n_idx, m_idx] = meshgrid(1 : size(imf, 2), 1 : size(imf, 1)); threshold = thres_factor * max(imf(:)); centers = []; % this is the result while true if max(imf(:)) < threshold break; end [m, n] = find(imf == max(imf(:)), 1, 'first'); centers = cat(1, centers, [m, n]); % now set this area to NaN to skip it in the next iteration idx_nan = sqrt((n_idx - n) .^ 2 + (m_idx - m) .^ 2) <= r; imf(idx_nan) = nan; end

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/pESPs.png" data-original="https://i.stack.imgur.com/pESPs.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />


That's possible with one call to <a href="http://www.mathworks.nl/help/images/ref/regionprops.html" rel="nofollow">regionprops</a>:

img = imread('KxJEJ.jpg'); % read the image imgbw = ~im2bw(img,graythresh(img)); % convert to grayscale stats = regionprops(bwlabel(imgbw), 'centroid','area'); % call regionprops to find centroid and area of all connected objects area = [stats.Area]; % extract area centre = cat(1,stats.Centroid); % extract centroids centre = centre(area>10,:); % filter out dust specks in the image

Now centre contains an Nx2 array: first column is x-position, second column is y-position of the centres:

centre = 289.82 451.73 661.41 461.21 1000.8 478.01 1346.7 482.98


I recall doing that at the university, <strong>many</strong> years ago!

What we did was to apply a threshold and make everything black and white. Then, we blobbed out the white area (non-circle) so it spread onto the circles.

When they started to disappear, we had the coordinates.

You could also pick two points at the circumference, find the exact middle of the line between them and then draw a perpendicular line through that point. If the middle of the new line <strong>is</strong> the center of the circle.


  • How do I keep the title bar from causing rendering issues in Metal?
  • lwjgl 3 , glUniformMatrix4 causes jre to crash
  • Firebase Crashlytics DSYM file
  • Using DevOps pipelines to create distribution bundles of applications
  • How to test if particular function has argument of given name?
  • Power for big numbers modulo m in C
  • How to list a specific directories in the SVN pre-commit Hook
  • Possible to fill a table cell with a bg color?
  • Spring boot preloading data from database in bean
  • Tensorflow import error: module 'imp' has no attribute 'find_module'
  • Unpacking / extracting resource from another JAR file
  • Mounting a gcePersistentDisk kubernetes volume is very slow
  • How to get optim working with matrix multiplication inside the function to be maximized in R
  • PDFBOX : U+000A ('controlLF') is not available in this font Helvetica encoding: WinAnsiEnc
  • WrapPanel: Trying to make the ItemWidth equal to the max width of any one element
  • Can't install Pillow on centos
  • how can i access values from strings.xml in kotlin android
  • Creating a Mutex throws a DirectoryNotFoundException
  • Django's admin is missing css, images, etc - unable to properly set up static files on shared h
  • How to pass data to a UIViewController from a UITableViewRowAction in Swift?
  • Display Django Code from a Django template
  • ORDER BY not behaving as expected
  • Decompress string in java from compressed string in C#
  • Fixing corrupt encoding (with Python)
  • Unicode File IO in Codename One
  • Anaconda + Apache + mod_wsgi + Ubuntu
  • Sorting Custom Listview Items Using Spinner Android
  • Using django-multiupload within a ModelForm
  • Threads and Concurrent Modification Exception working with a list
  • In metro, get all inherited classes of an (abstract) class?
  • Django REST framework - HyperlinkedRelatedField with additional parameter
  • How to use Typescript with libraries like Ampersand.js that parse configs to build prototypes
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • How to use FirstOrDefault inside Include