How can I calculate the distance between two points?

8 views (last 30 days)
Hello guys...
I have this https://drive.google.com/file/d/0B5c0tsKbCjUdYzFVRGpXLTBrSzg/view?usp=sharing on one side and I would like to use Matlab to identify the two blue points drawn and then I'd like to calculate the distance between this two points. (maybe using Cartesian coordinates)
Your help in this regard is highly appreciated, thank you in advance

Accepted Answer

Image Analyst
Image Analyst on 2 Jun 2015
You can use color segmentation to find the blue spots. Then use regionprops() to get the centroids of them. Then use the Pythagorean theorem or the hypot() function to get the distance. I'm attaching a demo of finding and tracking green regions in a video.
  11 Comments
Rebecca Brandão
Rebecca Brandão on 7 Jun 2015
Yeeey... I got it :))
I already indentify the centroids of the blue spots, but I kind of confuse how can I put this values in a variable to calculate the distance... Here is the code for the centroids:
I = imread('maos.jpg');
Ibw = im2bw(I);
Ibw = imfill(Ibw,'holes');
Ilabel = bwlabel(Ibw);
stat = regionprops(Ilabel,'BoundingBox','Centroid');
imshow(I); hold on;
for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro');
thisBB = stat(x).BoundingBox;
thisCentroid = stat(x).Centroid;
hRect(x)= rectangle('Position', thisBB);
hSpot = plot(thisCentroid(1), thisCentroid(2), 'y+', 'MarkerSize', 10, 'LineWidth', 2);
hText(x) = text(thisBB(1), thisBB(2)-20, strcat('X: ', num2str(round(thisCentroid(1))), ' Y: ', num2str(round(thisCentroid(2)))));
set(hText(x), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
and this is what I want to use to calculate the distance:
function [d] = distPontos(p1,p2)
d = sqrt((p1(1) - p2(2))^2 + (p1(2) - p2(2))^2);
Image Analyst
Image Analyst on 7 Jun 2015
The formula is wrong. Try
d = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);
or else use the hypot() function.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!