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

7 次查看(过去 30 天)
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 :)

采纳的回答

Simon Chan
Simon Chan 2021-8-23
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 个评论
Shailly  Vaidya
Shailly Vaidya 2021-8-24
编辑:Shailly Vaidya 2021-8-24
@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 2021-8-24
@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!

请先登录,再进行评论。

更多回答(1 个)

Devyani Maladkar
Devyani Maladkar 2021-8-23
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 个评论
Shailly  Vaidya
Shailly Vaidya 2021-8-23
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 2021-8-23
This could be due to the values populated in sorted_centers_x and sorted_centers_y you may want to look into those

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by