![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173041/image.jpeg)
find distance of points to centre of object?
17 views (last 30 days)
Show older comments
hi, how can find the distance of a,b and c to the centre of object (red point) in a binary image. I've found the the centre, but finding distances of a,b,c looks difficult to me. thank you.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/156018/image.jpeg)
0 Comments
Accepted Answer
Image Analyst
on 27 Feb 2016
Edited: Image Analyst
on 15 Dec 2018
You can get all boundary points using bwboundaries():
boundaries = bwboundaries(binaryImage);
x = bwboundaries{1}(:, 2);
y = bwboundaries{1}(:, 1);
Now you can get distances from the center point to all points on the boundary like this:
distances = sqrt((x-xCentroid).^2 + (y-yCentroid).^2);
It's not really clear where your points are, especially point C. To get A and B, you could use findpeaks() to find them
[peakDistances, peakIndexes] = findpeaks(distances);
That requires the Signal Processing Toolbox.
You'd need to somehow better define where C is to get its distance from the "distances" array. Perhaps you can just negate distances and find the two peaks for C and the point directly across the banana from it, and take the highest peak.
[valleyDistances, valleyIndexes] = findpeaks(-distances);
cDistance = max(abs(valeyDistances));
Attached is a full demo, that produces this picture. Since it is possible this is your homework, I'll leave it to you to find point "b" - it's similar to how I found point "a" but there are a few different ways.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173041/image.jpeg)
2 Comments
Evelynn Tan
on 15 Dec 2018
Edited: Image Analyst
on 15 Dec 2018
Hi, I know this is posted by others, and it was asked long ttime ago, but I would like to ask, I've got this by modifying your demo code. However, can I know what is it mean by the x-axis? as i know the y-axis would be the pixel value of the measurements, and also what is it mean by the few lines on the graph as there are only three lines with different colors in the binary image. (I have already changed all the MinPeakProminence until li can get a line in the binary image while also the minimum value of lines in the graph.)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/199095/image.jpeg)
Image Analyst
on 15 Dec 2018
For the images, the y axis is the row and the x axis is the column. For the line plot, the y axis is the distance from the centroid, and the x axis is the index of the distances array.
Post your image in a new question along with your code if you still have questions.
More Answers (1)
John BG
on 27 Feb 2016
Abo
1.- acquire image
A=imread('the triangulation of the banana.jpg')
imshow(A)
[size_y size_x s3]=size(A)
A(A<120)=0;A(A>120)=255 % clean image a bit
A2=A(:,:,1)+A(:,:,2)+A(:,:,3) % carry on with binary map, no need for RGB 3 layers
2.- scan columns and find point a with:
i=1
all_black=1
while all_black
L=A2(:,i)
if (max(L)==255)
all_black=0;
end
i=i+1
end
if you did just
find(L==255)
ans =
184.00
185.00
188.00
189.00
190.00
191.00
there are actually 6 pixels right on the left tip, so let's take the midpoint
b=[i-1 floor(mean(find(L==255)))]
3.- repeat now scanning rows to find point a:
j=1
all_black=1
while all_black
L=A2(j,:)
if (max(L)==255)
all_black=0;
end
j=j+1
end
a=[floor(mean(find(L==255))) j-1]
4.- repeat to find c:
k=size_y
all_black=1
while all_black
L=A2(k,:)
if (max(L)==255)
all_black=0;
end
k=k-1
end
c=[floor(mean(find(L==255))) k+1]
5.- the 4th point of interest, not to be confused with point c, the center K:
K=[231 234] % however you find it, i just used the marker to get the coordinates
since you named vertices 'a' 'b' 'c' and the central point is centre K the common way to name segments is aK bK cK
norm(K-a)
ans = 259.36
norm(b-K)
ans = 191.85
norm(c-K)
ans = 59.54
If you find this answer of any help solving this question, please click on the thumbs-up vote link,
thanks in advance
John
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!