How can I calculate the distance between two points?

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

 Risposta accettata

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 Commenti

Hey Image Analyst... thanks for your help :) I'm looking your code, and there is a part that you use:
hThresholds = [0.24, 0.44]; sThresholds = [0.8, 1.0]; vThresholds = [20, 125];
How can I find the HSV values for the color that I want to find?
P.S: The RGB for the color that i'm looking for is 52,41,49.. how can i convert for the hsv ???
I look at the histograms for those channels to see where the values for the color I'm interested in lie. If you know for a fact that segmentation in RGB color space will do the job for you, and know what the values are, then you can do it in RGB color space instead of HSV or LAB. I often use Color Inspector 3D to examine the 3D gamut in different color spaces to see the shape of it and decide which color space will be easiest to segment it in. Usually HSV color space is best but not always.
You can use impixelinfo() if you want to see the HSV values "live" in a status label as you interactively move the mouse around in the image.
Ok .... I was trying to use HSV values that I found using impixelinfo (), but this method can not find a satisfactory result because there are many points that are detected but not the points that I really want... How could I use the RGB values instead HSV values ?
I'm trying this...
a=imread('mao3.png');
figure(1),title('original image'),imshow(a);
b=rgb2gray(a);
red = imsubtract(a(:,:,1), b)%1 for red,2 for green ,3 for blue
red = medfilt2(red, [4 4]); % Filter out the noise by using median filter
red = im2bw(red,0.17); % Convert the image into binary image with the red objects as white
red1= bwareaopen(red,300); % removes from a binary image all connected components (red) that have fewer than 300 pixels, producing another binary image, red1.
figure(3),title('red color'),imshow(red1);
And this work so good to find the red object in the image... but, Do you know how can i find other colors with this method?
It really depends on that shape of the gamut in 3D color space. You can use different colorspaces but the best one to use depends on the shape of the gamut - in other words, it depends on your image: the colors of what you want and the colors of the stuff you don't want. I can't make a general suggestion unless I see the actual image. Why don't you try the 3 methods in my File Exchange? Adapt them and see what works best. If you can't find a good one, post your image and I'll see what I can do.
Hmm... I see. My image's link is this: https://drive.google.com/file/d/0B5c0tsKbCjUdYzFVRGpXLTBrSzg/view
I tried your "Simple color detection by hue" tutorial, and works (like i said, i just want the blue spots) but part of the background shows up on the final results too...
The background must be close to the same color. If the background blobs are smaller, you can use bwareaopen() to get rid of them. Otherwise, try to use a more vivid blue color, like paper dot stickers or something.
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);
The formula is wrong. Try
d = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);
or else use the hypot() function.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by