finding x-y coordinate of object with specific distance

hello,
I have a binary image with five objects in it, I've found the distances between each pair, I gonna find the x-y coordinates of the two objects which have the special distances (for example the biggest distances), how can I do that? Thank you for your help in advance.

 Accepted Answer

With PairWiseDistanceArray being your 5 x 5 distance array where the index (I,J) is the distance between object #I and object #J, then:
[maxdistance, maxidx] = max(PairWiseDistanceArray(:));
[object1idx, object2idx] = ind2sub(size(PairWiseDistanceArray),maxidx);
The maximum distance was maxdistance and it was between object #object1idx and object #object2idx

11 Comments

thank you for your suggestion, is it possible to find objects which the distance between them is (55)?
No. That never happens on the coordinate plane if you are using Euclidean distance. 55 is an integer, and the only integer distances less than 100 that can occur are: 5, 13, 17, 25, 29, 37, 41, 53, 61, 65, 73, 85, 89, 97. See https://en.wikipedia.org/wiki/Pythagorean_triple
All non-integral distances that occur between integer coordinates are irrational numbers.
ok, thank you. how can i find pairs with distances less than 100, with the code you proposed?
With PairWiseDistanceArray being your 5 x 5 distance array where the index (I,J) is the distance between object #I and object #J, then:
[r, c] = find(PairWiseDistanceArray < 100);
mask = r < c;
obj1 = r(mask);
obj2 = c(mask);
Now obj1 and obj2 are vectors with obj1(K) being less than distance 100 from obj2(K), and duplicates have been eliminated.
Walter, the centroids are rarely exact integer coordinates. They're almost always located in between pixel centers.
The poster did not say how the coordinates were found. Centroids were not mentioned. One of my mental questions when I first read the topic was "Oh? How exactly is the distance between blobs being determined when blobs are often irregularly shaped?". To get any further in the question I had to assume that some method of assigning the coordinates had been arbitrarily determined.
Coordinates determined by centroids of a finite set of integral points are always rational values, and rational values have the same restrictions on pythagorean triples: there are a few but every other distance is irrational.
I used 'nchoosek' and distance functions for finding distances between different pairs, with the code Walter Roberson suggested, i can not find the x and y coordinates of centres which have special distances (here less than 100 ).!!
Regarding the code, I Want to find 1) distance between each two centres 2) find distances less than 100, 3) find x-y coordinates of centres with distances less than 100:
co=[ centrex, centreY]; % x-y coordinate of centres
paircentre = nchoosek(1:size(centrex, 2), 2);
distcentre= sqrt(sum((centrex (:, paircentre(:, 1)) - centrex (:, paircentre(:, 2))) .^ 2, 1)+ sum((centreY (:, paircentre(:, 1)) - centreY (:, paircentre(:, 2))) .^ 2, 1)); % distance between pairs
cd1= distcentre <100;
[tr, tc] = find(cd1);
mask = tr < tc;
obj1 = tr(mask);
obj2 = tc(mask);
xp1 = min(obj2); % x-coordinate of centre of object 1
xp2 = max(obj2);% x-coordinate of centre of object 2
the code does not give the best result, how can I find y-coordinates as well?
Thank you
obj1 and obj2 are not the x and y coordinates, they are the object number.
xp1 = centrex(obj1);
xp2 = centrex(obj2);
yp1 = centreY(obj1);
yp2 = centreY(obj2);

Sign in to comment.

More Answers (0)

Categories

Asked:

on 18 Nov 2015

Commented:

on 21 Nov 2015

Community Treasure Hunt

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

Start Hunting!