drawMatches for vector<vector<DMatch> > good_matches

asked 2013-12-17 11:16:08 -0500

matteo gravatar image

i need to detect the matchings between 2 images. I use the following code:

int MatchFinder(Mat img_1, Mat img_2, Mat& A )
{
vector<KeyPoint> keypoints_1;
vector<KeyPoint> keypoints_2;

Mat img_matches;
vector <Point2f> point1;
vector <Point2f> point2;

int minHessian = 400;

SiftFeatureDetector detector( minHessian );

detector.detect( img_1, keypoints_1 );
detector.detect( img_2, keypoints_2 );

//-- Step 2: Calculate descriptors (feature vectors)

SiftDescriptorExtractor extractor;

Mat descriptors_1, descriptors_2;

extractor.compute( img_1, keypoints_1, descriptors_1 );
extractor.compute( img_2, keypoints_2, descriptors_2 );


//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;

if ( descriptors_1.empty() )
    cvError(0,"MatchFinder","1st descriptor empty",__FILE__,__LINE__);


if ( descriptors_2.empty() )
    cvError(0,"MatchFinder","2nd descriptor empty",__FILE__,__LINE__);

////////////////////////////// -- Ratio check
std::vector<vector< DMatch > > matches;
matcher.knnMatch( descriptors_1, descriptors_2, matches, 2 );

std::vector<vector<DMatch> > good_matches;

double RatioT = 0.75;
//-- ratio Test
for(unsigned 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]);
   }
}


 //-- Draw only "good" matches   //-- Show detected matches

// VISUALIZATION !?!?!

/////////////////////////////////// -- ratio end 1
if(good_matches.size()<4)
    {
    //cout << "less than 4 matching points!   "  <<endl;
    point1.clear();
    point2.clear();
    keypoints_1.clear();
    keypoints_2.clear();
    good_matches.clear();
    img_matches.release();

    return 1;
    }


else
{

    for(unsigned int i = 0; i < good_matches.size(); i++ )
    {
        //-- Get the keypoints from the good matches
        point1.push_back( keypoints_1[ good_matches[i][0].queryIdx ].pt );
        point2.push_back( keypoints_2[ good_matches[i][0].trainIdx ].pt );
    }
      return 0;
   }

   }

I would like now to visualize the matching between the 2 images as if i used drawMatches (actually i cannot use this function: i have a vector<vector<dmatch> > good_matches instead of vector<dmatch> good_matches).

Any suggestion?

edit retag flag offensive close merge delete

Comments

You can implement loop for vector<vector<> > and call drawMatches several times for each vector<dmatch> inside global vector<vector<> >.

Alexander Smorkalov gravatar imageAlexander Smorkalov ( 2013-12-18 01:27:21 -0500 )edit