How to label the centers of circles according to their position on axes?

6 views (last 30 days)
Hello all.
I have found and masked circles in an image. Now, I wish to label the circles as 1, 2, 3....end (please see the attached image) , and I want the numbers to appear at the center of the circles. The attached image is how I want the output to be.
Tried implementing it using text(). I used the following code which definitely has errors; as it is not giving the desired output.
for i=1:sorted_centers_x % sorted_centers_x is an array with all the X coordinates of the centers of the circles
for j=1:sorted_centers_y % sorted_centers_y is an array with all the Y coordinates of the centers of the circles
if (sorted_centers_y(j) >=0) && (sorted_centers_y(j)<20) % iterating with different range of values. so as to label 1-6 on the circles in the upper row and so on..
text(double(sorted_centers_x(j)),double(sorted_centers_y(i)),num2str(j),'Color','g','FontSize', 14,'FontWeight','bold');
else
text(double(sorted_centers_x(j)),double(sorted_centers_y(i)),num2str(j),'Color','g','FontSize', 14,'FontWeight','bold');
end
end
end
Any help would be appreciated! Thanks in advance :)

Accepted Answer

Simon Chan
Simon Chan on 23 Aug 2021
Try this and still using function text:
clear; clc;
I = ones(400,600);
sorted_centers_x = repmat(50:100:550,1,4);
sorted_centers_y = repelem(50:100:350,1,6);
imshow(I)
text(sorted_centers_x,sorted_centers_y,num2str([1:24]'))
  5 Comments
Shailly  Vaidya
Shailly Vaidya on 24 Aug 2021
Edited: Shailly Vaidya on 24 Aug 2021
@Simon Chan there are 24 circles detected and hence 24 pairs of X and Y coordiantes of centers stored in sorted_centers_x and sorted_centers_y respectively.
Thanks for helping! :)
Shailly  Vaidya
Shailly Vaidya on 24 Aug 2021
@Image Analyst yes sir, am using 96 well plate template. You are right, after a few rounds of testing, I can fix the positions of the numbers on the image.
Thanks a lot for helping!

Sign in to comment.

More Answers (1)

Devyani Maladkar
Devyani Maladkar on 23 Aug 2021
Hello,
It is my understanding that you want to label the masked circle by placing a number at the centre of the circle.
The text function can be used to insert text at the desired position in the current axes. The documentation here can be used to understand more about the text function use.
I have provided the code below for the implementation of labelling the circles with appropriate numbers. I have used sample data to demonstrate that the numbering takes place depending on where the (0,0) reference point is.
In the below code the reference point (0,0) is in the bottom-left corner of the plot, and the circle with maximum y-coordinate and minimum x-coordinate gets labelled 1 (upper-left corner). The labelling proceeds with increasing x-coordinate and decreasing y-coordinate hence the for-loop iteration is on ascending x values and descending y values.
%Sample data
sorted_centers_x=sort([0.1 ,0.2,0.3,0.4,0.5],'ascend')
sorted_centers_y=sort([0.1,0.2,0.3],'descend')
for j=1:numel(sorted_centers_y) % sorted_centers_x is an array with all the X coordinates of the centers of the circles
for i=1:numel(sorted_centers_x) % sorted_centers_y is an array with all the Y coordinates of the centers of the circles
if (sorted_centers_y(j) >=0) && (sorted_centers_y(j)<20) % iterating with different range of values. so as to label 1-6 on the circles in the upper row and so on..
text(double(sorted_centers_x(i)),double(sorted_centers_y(j)),num2str((j-1)*numel(sorted_centers_x)+i),'Color','g','FontSize', 14,'FontWeight','bold');
else
ext(double(sorted_centers_x(i)),double(sorted_centers_y(j)),num2str((j-1)*numel(sorted_centers_x)+i),'Color','r','FontSize', 14,'FontWeight','bold');
end
end
end
  2 Comments
Shailly  Vaidya
Shailly Vaidya on 23 Aug 2021
Hey thanks @Devyani Maladkar! I have altered the code that you provided.
%Sample data
sorted_centers_x=sort(sorted_centers_x,'descend')
sorted_centers_y=sort(sorted_centers_y,'ascend')
for j=1:numel(sorted_centers_y) % sorted_centers_x is an array with all the X coordinates of the centers of the circles
for i=1:numel(sorted_centers_x) % sorted_centers_y is an array with all the Y coordinates of the centers of the circles
if (sorted_centers_y(j) >=0) && (sorted_centers_y(j)<135) % iterating with different range of values. so as to label 1-6 on the circles in the upper row and so on..
text(double(sorted_centers_x(i)),double(sorted_centers_y(j)),num2str((j-1)*numel(sorted_centers_x)+i),'Color','g','FontSize', 14,'FontWeight','bold');
else
text(double(sorted_centers_x(i)),double(sorted_centers_y(j)),num2str((j-1)*numel(sorted_centers_x)+i),'Color','r','FontSize', 14,'FontWeight','bold');
end
end
end
and getting this output. Here, all the numbers are getting printed on top of each other, and hence creating the red-patch over it. How can I resolve this issue?
Devyani Maladkar
Devyani Maladkar on 23 Aug 2021
This could be due to the values populated in sorted_centers_x and sorted_centers_y you may want to look into those

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!