Hello, I have a binary image with one object. How can I calculate the center of this object, to mark it in my image and to write the coordinates in the corner of this image, like this: object: X, Y
I used this code to calculate the center of mass:
s = regionprops(bw, 'centroid');
centroids = cat(1, s.Centroid);
imshow('MY_IMAGE.jpg')
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
but how can i plot this coordinates on my image in the left corner?
Thank you for help

 채택된 답변

David Sanchez
David Sanchez 2013년 9월 18일

0 개 추천

use text command:
your_string = strcat( num2str(centroids(:,1)),'-', num2str(centroids(:,2)));
text(10,10,your_string)
change the position by changing the "10,10" values

댓글 수: 5

Felix
Felix 2013년 9월 18일
Thank you! How can I change the background and the color of the text?
Look up Text properties in the help. You will see 'BackgroundColor' and 'Color':
text(x, y, yourString, 'Color', [1, ,5, .2], 'BackgroundColor', [.1, .3, .2]);
Felix
Felix 2013년 9월 18일
편집: Felix 2013년 9월 18일
Thank you it's works, another question, how can i write in this way:
Object: X,Y(coordinates)
now it gives me only a coordinates and can i also write a coordinates without a numbers after '.'? only integers
Use sprintf to create your string, and cast your numbers to integers or use %.0f:
textString = sprintf('Object: %d, %d (coordinates)', int32(X), int32(Y));
text(x,y,textString);
Felix
Felix 2013년 9월 18일
thnx

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2013년 9월 18일

0 개 추천

ndgrid() of 1:width and 1:height, do a logical indexing of those according to the binary image, calculate the mean() of what is left in each of the two coordinate matrices. Result will be centre of mass along each of the two coordinates.
Mauldy Putra
Mauldy Putra 2016년 12월 16일

0 개 추천

i want to ask, in your code there is bw i want to know what is bw stand for? cause i try use your code it's said "Undefined function or variable 'bw'."
thank you

댓글 수: 1

Walter Roberson
Walter Roberson 2016년 12월 16일
bw is the binary image whose center of mass is to be found.

댓글을 달려면 로그인하십시오.

태그

아직 태그를 입력하지 않았습니다.

질문:

2013년 9월 18일

댓글:

2016년 12월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by