Ask Your Question

Find average distance between 2 matched keypoints (after using findHomography() mask) c++

asked 2020-05-14 06:36:11 -0500

albeh95 gravatar image

updated 2020-05-14 21:12:58 -0500

supra56 gravatar image

I'm trying to calculate the average distance along x and y axis between 2 matched keypoints. I followed the following steps:

  1. I calculated keypoints thanks to the ORB class (vector keypoint_1, keypoint2;)
  2. I matched keypoints thanks to BFMatcher.match()
  3. I obtained a better result thanks to the min_dist method
  4. I got a mask between 2 images using findHomography(img_1, img_2, RANSAC, 3, mask)

Now that I have the mask I just made a for loop, that evaluate every point in the mask that are equal to 1: when this happens I'm supposed to calculate the (average) distance along x and y axis of this matched points, but I'm stuck and don't know how to do that. Thanks everyone for the help!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-05-15 02:55:39 -0500

HYPEREGO gravatar image

updated 2020-05-15 05:46:49 -0500

What you're doing is completely fine. You just need to answer to this: which distance you would like to use? There are tons of distances, Euclidean, Mahalanobis, Manatthan distance. You have to choose it. If you choose, for example, a Sum of Squared Distance (SSD), it will be (x-x')^2+(y-y')^2, where (x,y) are the keypoints location in the first image and (x',y') are the matched keypoints in the second image.

If you want to find the average distance along x and y axis, simply defined as you did, then you have to sum all the distance x-x' in a variable and y-y' in another variable and do the average. Like this

int sumXdist = 0;
int sumYdist = 0;
int numInliers=0;

for(size_t i = 0; i < matches.size(); i++)
    if( == 1) //So is an inliers
        cv::DMatch first = matches[i][0];
        sumXdist += fabs(keypoints1[first.queryIdx].pt.x - keypoints2[first.trainIdx].pt.x);
        sumYdist += fabs(keypoints1[first.queryIdx].pt.y - keypoints2[first.trainIdx].pt.y);
double avgAlongX = (double) sumXdist/numInliers;
double avgAlongY = (double) sumYdist/numInliers;

This code is just an example in C++ but I hope you get it!

edit flag offensive delete link more



Thank you so much! This is very clear and helpful :)

albeh95 gravatar imagealbeh95 ( 2020-05-15 05:42:11 -0500 )edit

If you don't mind please mark my reply as correct answer to help who's looking at the same thing. Thank you!

HYPEREGO gravatar imageHYPEREGO ( 2020-05-15 05:47:19 -0500 )edit

Question Tools

1 follower


Asked: 2020-05-14 06:36:11 -0500

Seen: 211 times

Last updated: May 15 '20