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

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 close merge delete

Sort by ยป oldest newest most voted

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(mask.at(i) == 1) //So is an inliers
{
numInliers++;
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!

more

1

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

( 2020-05-15 05:42:11 -0500 )edit
1

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

( 2020-05-15 05:47:19 -0500 )edit

Official site

GitHub

Wiki

Documentation