Ask Your Question

Ratio check function with findHomography

asked 2013-08-20 13:07:42 -0500

matteo gravatar image

updated 2013-08-21 08:33:00 -0500

SR gravatar image

i need to acquire a perspective matrix from two images taken by the same camera after its movement in a 3D space...I'm taking inspiration from this OpenCV's example about the use of findHomography. I would like to substitute the "filter for the good_matches there suggested with the "ratio check function":

class RatioCheck
    void filter(vector<vector<DMatch>>& nmatches, double RatioT);

void RatioCheck::filter(std::vector<vector<DMatch> > &nmatches, double RatioT=0.8)
    vector<vector<DMatch>> knmatches;
    for(int i=0; i<nmatches.size(); i++)
            (abs(nmatches[i][0].distance/nmatches[i][1].distance) <RatioT))

In principle i have to give to the function a vector<vector<DMatch> > ..but actually i have a vector<DMatch>... does anybody know how?

edit retag flag offensive close merge delete


-1. Relevant part of the question off-site. Provide a minimal example.

SR gravatar imageSR ( 2013-08-20 16:27:12 -0500 )edit


matteo gravatar imagematteo ( 2013-08-21 04:10:50 -0500 )edit

+1 Thanks. (seems that I cannot undo my downvote, so I upvoted your comment)

SR gravatar imageSR ( 2013-08-21 08:27:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-08-20 13:36:35 -0500

Moster gravatar image

updated 2013-08-21 02:03:09 -0500

You need to replace the matching part with this. Its important that you use knnMatch with 2 best matches, so that you can actually do the ratioTest (which compares the match distance between the first and second best match).

std::vector<vector< DMatch >> matches;
matcher.knnMatch( descriptors_object, descriptors_scene, matches, 2 );

std::vector<vector<DMatch>> good_matches
double RatioT = 0.75;
//-- ratio Test
for(int i=0; i<matches.size(); i++)
   if((matches[i].size()==1)||(abs(matches[i][0].distance/matches[i][1].distance) < RatioT))

The creation of the Point2f vectors obj and scene also needs to be adjusted since you have a vector of vectors.

for( int i = 0; i < good_matches.size(); i++ )
//-- Get the keypoints from the good matches
    obj.push_back( keypoints_object[ good_matches[i][0].queryIdx ].pt );
    scene.push_back( keypoints_scene[ good_matches[i][0].trainIdx ].pt );
edit flag offensive delete link more


-1. Solution off-site.

SR gravatar imageSR ( 2013-08-20 16:25:37 -0500 )edit

Why isnt it allowed to be offsite? I didnt want to post the whole code here, since its huge and he hasnt been too specific.

Moster gravatar imageMoster ( 2013-08-21 00:58:32 -0500 )edit

Because a specific question should be asked where the OP narrows it down to the actual issue. Posting the whole code is not really helpful as one has to scan the whole code. And also, the answer should be reachable for everyone. External links might be dead soon.

SR gravatar imageSR ( 2013-08-21 01:26:10 -0500 )edit

Ok, I edited it, so that its not leading to an offsite.

Moster gravatar imageMoster ( 2013-08-21 02:05:04 -0500 )edit

+1. Thanks!

SR gravatar imageSR ( 2013-08-21 08:33:40 -0500 )edit

Question Tools


Asked: 2013-08-20 13:07:42 -0500

Seen: 1,111 times

Last updated: Aug 21 '13