MATLAB Answers

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

1 ビュー (過去 30 日間)
Shailly  Vaidya
Shailly Vaidya 2021 年 8 月 23 日
編集済み: Shailly Vaidya 2021 年 8 月 24 日
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 日
@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 件のコメント
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

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by