How can I use imfindcircles function to find circle centers without sorting

I used imfindcircles to find circles in an image, but the circles found are also sorted based on the strgenth of the circles! I do not want to this function to sort the circles, how can I to do this?

 Accepted Answer

Your comment suggests that you do want the results to be sorted, but according to position rather than strength. (Strength means the number of votes the circle gets in the Hough accumulator.)
There is no natural position ordering. You have to decide what you want and implement a sort as required. For example, if you want to sort by y-coordinate, you'd do this:
% example data
A = imread('coins.png');
[centres, radii] = imfindcircles(A, [15 30]);
% sort by y coordinate
[~, index] = sort(centres(:,2)); % 1 instead of 2 would sort by x coord
centres = centres(index, :);
radii = radii(index, :);
% display the ordering
imshow(A);
viscircles(centres, radii);
for c = 1:length(radii)
text(centres(c,1), centres(c,2), sprintf('%2u', c));
end
If your circles are on a kind of grid, it is unlikely that sorting by x or y will give exactly what you want. You probably have to work out which cell of the grid each circle is in, then sort the results accordingly. Here's a simple approach, but you'll need to think through how to make the quantisation step work reliably for your data.
% sort by quantised x and y coordinates
gridstep = 50; % needs to be equal to the grid size
qcentres = gridstep * round(centres/gridstep); % quantised coords
[~, index] = sort(qcentres(:,1)*size(A,1) + qcentres(:,2)); % sort on x then on y
centres = centres(index, :);
radii = radii(index, :);
You can visualise the ordering using the same code as before.

More Answers (2)

I don't see anything in the documentation about it doing any kind of sorting, though it might. Let's say you have a dozen circles of different diameters randomly placed around the image. What would you consider to be an unsorted list? Which circle would you put first and which would you put last .

5 Comments

Do you mean that the first detected circle is put first? I looked over the documention about the function, it underlins that centers detected are sorted based on the strength of the circles. One thing I do not understand is that what does it mean sorting the centers based on the STRENGTH of circles?
Please define "first". And please say what an unsorted list would look like. Anyway, what does it matter? Why do you even care about sorting?
Maybe my question is ambiguous. My question is that if an image consists of circles placed in order regularly, the coordinates of the circles' centers founded should be alse placed in the previous order. For example, if the coordinates of founded centers in the first row represents a circle center's coordinates, and the secont row should represcent a circle next to the first by row or by column, and so on. The reason why I matter the order of circle centers is my image consists of regular circles in order. Anyway, centers' orders found are NOT ordered anymore.
OK so it looks like you want the circles sorted in column-major order. I suggest you look at David's answer. By the way, are you looking at a 96 well plate or microarray?
Thank you for your patience to answer really. I do not look at 96 well plate or microarray. I just do camera calibration with the target consists of circles ordered regularly.

Sign in to comment.

If you mean to say that you'd like imfindcircles to also detect even 'weak' circles, than set the 'Sensitivity' parameter to a high value (close to 1).

Categories

Asked:

on 25 Nov 2014

Commented:

on 28 Nov 2014

Community Treasure Hunt

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

Start Hunting!