# Ratio check function with findHomography

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
{
public:
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++)
{
if((nmatches[i].size()==1)||...
(abs(nmatches[i][0].distance/nmatches[i][1].distance) <RatioT))
{
knmatches.push_back(nmatches[i]);
}
}
nmatches=knmatches;
}


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

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

( 2013-08-20 16:27:12 -0600 )edit
1

updated..sorry

( 2013-08-21 04:10:50 -0600 )edit

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

( 2013-08-21 08:27:37 -0600 )edit

Sort by ยป oldest newest most voted

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))
{
good_matches.push_back(matches[i]);
}
}


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 );
}

more

-1. Solution off-site.

( 2013-08-20 16:25:37 -0600 )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.

( 2013-08-21 00:58:32 -0600 )edit
1

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.

( 2013-08-21 01:26:10 -0600 )edit
1

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

( 2013-08-21 02:05:04 -0600 )edit
1

+1. Thanks!

( 2013-08-21 08:33:40 -0600 )edit

Official site

GitHub

Wiki

Documentation

## Stats

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

Seen: 1,217 times

Last updated: Aug 21 '13