finding distance between centroids

Hi
I have a group of blobs of a binary image for which I have calculated the centroids.
I would like to find the distance between each of these centroids and then extract the two centroids which lie in a 40 to 45 range. I've seperated the X and Y cdts into 2 matrices like so:
>> X=centroids(:,1); Y=centroids(:,2); >> X
X =
121.3073
142.9425
168.1000
225.5856
522.9487
>> Y
Y =
207.2626
149.0276
114.7667
297.5586
299.9551
and then I tried computing the euclidean distance using pdist2 but I dont understand what it gives me and I dont see any 40s over here
D = pdist2(X,Y,'euclidean'); >> D
D =
85.9553 27.7203 6.5406 176.2513 178.6479
64.3200 6.0851 28.1759 154.6160 157.0126
39.1626 19.0724 53.3333 129.4586 131.8551
18.3230 76.5580 110.8189 71.9730 74.3695
315.6861 373.9211 408.1821 225.3902 222.9936
Any advice pls?

Risposte (1)

try
D = dist([X,Y]')

7 Commenti

mayfair
mayfair il 12 Apr 2012
Thanks andrei I just tried it and got this:
D = dist([X,Y]')
D =
0 62.1241 103.6583 137.9396 412.1987
62.1241 0 42.5054 169.9745 408.8812
103.6583 42.5054 0 191.6180 400.2654
137.9396 169.9745 191.6180 0 297.3728
412.1987 408.8812 400.2654 297.3728 0
I realised that dist and pdist2 actually computes distance between each X and Y which is not exactly what I want...
I'd like to be able to find the distance between each set of centroid coordinates and ideas on that pls?
Thanks!
mayfair, try hand computing your expected result for a couple and compare with the above.
Example: Distance betwee first two points:
>> ( (X(1)-X(2))^2 + (Y(1)-Y(2))^2)^.5
ans =
62.1240
(Isnt that what Andrei's answer gives?)
mayfair
mayfair il 14 Apr 2012
That's truue Ashish I managed to work it out I found that if you subtract the x and y matrices from its transposed matrix then use the hypot function it works like a charm :)
Rujal
Rujal il 5 Feb 2016
hi mayfair..
My question is same as yours please give me a matlab code to find out distance between set of centroids. Thanks in advance.
Image Analyst
Image Analyst il 5 Feb 2016
Modificato: Image Analyst il 5 Feb 2016
If you have the stats toolbox, you can use pdist2(). If not, just a simple for loop with vectorized distance calculation between that point and all the other points will do it.
Even without the stats toolbox, you don't need a loop:
euclideandistance = hypot(bsxfun(@minus, X, X'), bsxfun(@minus, Y, Y'));
is all that's needed (assuming X and Y are vectors of the same size)
emami.m
emami.m il 11 Mar 2019
Good job Guillaume,
It was a smart and exact solution.

Accedi per commentare.

Richiesto:

il 12 Apr 2012

Commentato:

il 11 Mar 2019

Community Treasure Hunt

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

Start Hunting!

Translated by