How to pair two set of coordinates together based on their Euclidean distance?

Hi! I got two sets of pixels as my feature points, one is red and another is blue, I want to use Euclidean distance between two features as the metric: if the distance is below a threshold, make the two features as a pair of correspondence. The threshold could be 1 or 2 (pixels). I want to count the number of correspondence pairs. Thank you so much if you can help meT
The image file is the "image.mat", the red pixel coordinates is in "red_pixel_coordinate.mat", and the blue pixel coordinates is in "blue_pixel_coordinate.mat".

Answers (1)

The cost matrix can be obtained using pdist2.

6 Comments

Thank you very much for the answer! But I'm sorry I don't understand how can this two functions be applied to my problem, could you please give me an example? Thank you!
pdist2 gives you the distances of each red point from each blue point.
After this, you can sort out the elements of this matrix with values less than 1 (or 2) pixel distance.
Example:
red=load('red_pixel_coordinate.mat').red_coordinates';
blue=load('blue_pixel_coordinate.mat').blue_coordinates';
Cost=pdist2(red,blue);
Cost(Cost>2)=inf; %don't match for distance>2
M=matchpairs(Cost,1e5);
Hi thank you very much, M returns a 39by2 array, but what does that mean, still little confused;) thank you
From the the doc:
M — Matches
matrix
Matches, returned as a matrix. M is a p-by-2 matrix, where M(i,1) and M(i,2) are the row and column indices of a matched pair in the cost matrix. The rows of M are sorted with the second column in ascending order.
  • Each row and column can be matched a single time only, so each M(i,1) value and each M(i,2) value is unique.
  • M contains p matches, and p is less than or equal to the maximum number of matches min(size(Cost)).
  • The cost of the matches in M is sum([Cost(M(1,1),M(1,2)), Cost(M(2,1),M(2,2)), ..., Cost(M(p,1),M(p,2))]).
Hi Matt, problem solved, thank you so much so much.

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

on 3 Oct 2022

Commented:

on 6 Oct 2022

Community Treasure Hunt

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

Start Hunting!