1

I am trying to do feature matching between 2 perspectives of the same image using DAISY and the FlannBasedMatcher.

I don't think there is even a single match that is correct.

Note: I also get different results each time I run the program but I think this is expected behaviour as explained here: FlannBasedMatcher returning different results

So what am I doing wrong? Why are these matches so bad?


Input Images

enter image description here

enter image description here

Wrong & non-deterministic results

enter image description here

enter image description here

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>

#include <iostream>
#include <vector>
#include <stdio.h>

using namespace cv;
using std::vector;

const float nn_match_ratio = 0.7f;      // Nearest neighbor matching ratio
const float keypoint_diameter = 15.0f;

int main(int argc, char ** argv){

    // Load images
    Mat img1 = imread(argv[1]);
    Mat img2 = imread(argv[2]);

    vector<KeyPoint> keypoints1, keypoints2;

    // Add every pixel to the list of keypoints for each image
    for (float xx = keypoint_diameter; xx < img1.size().width - keypoint_diameter; xx++) {
        for (float yy = keypoint_diameter; yy < img1.size().height - keypoint_diameter; yy++) {
            keypoints1.push_back(KeyPoint(xx, yy, keypoint_diameter));
            keypoints2.push_back(KeyPoint(xx, yy, keypoint_diameter));
        }
    }

    Mat desc1, desc2;

    Ptr<cv::xfeatures2d::DAISY> descriptor_extractor = cv::xfeatures2d::DAISY::create();

    // Compute DAISY descriptors for both images 
    descriptor_extractor->compute(img1, keypoints1, desc1);
    descriptor_extractor->compute(img2, keypoints2, desc2);

    vector <vector<DMatch>> matches;

    // For each descriptor in image1, find 2 closest matched in image2 (note: couldn't get BF matcher to work here at all)
    FlannBasedMatcher flannmatcher;
    flannmatcher.add(desc1);
    flannmatcher.train();
    flannmatcher.knnMatch(desc2, matches, 2);


    // ignore matches with high ambiguity -- i.e. second closest match not much worse than first
    // push all remaining matches back into DMatch Vector "good_matches" so we can draw them using DrawMatches
    int                 num_good = 0;
    vector<KeyPoint>    matched1, matched2; 
    vector<DMatch>      good_matches;

    for (int i = 0; i < matches.size(); i++) {
        DMatch first  = matches[i][0];
        DMatch second = matches[i][1];

        if (first.distance < nn_match_ratio * second.distance) {
            matched1.push_back(keypoints1[first.queryIdx]);
            matched2.push_back(keypoints2[first.trainIdx]);
            good_matches.push_back(DMatch(num_good, num_good, 0));
            num_good++;
        }
    }

    Mat res;
    drawMatches(img1, matched1, img2, matched2, good_matches, res);
    imwrite("_res.png", res);

    return 0;
}
Community
  • 1
  • 1
GroovyDotCom
  • 1,304
  • 2
  • 15
  • 29
  • Hard to tell, but I'd suggest just trying with another descriptor, just to see if you get better results ? – kebs Nov 09 '15 at 21:56
  • I tried SURF instead of DAISY and getting weird results there as well - including matches between bright areas and dark areas. At least with SURF, it is the same results each time. Thanks for the direction though, I'll see if I can reproduce the openCV SURF example and work from there. – GroovyDotCom Nov 09 '15 at 23:22

2 Answers2

0

Sorry. I found my bug. I have the Indexes reversed in the lines that read:

        matched1.push_back(keypoints1[first.queryIdx]);
        matched2.push_back(keypoints2[first.trainIdx]);
GroovyDotCom
  • 1,304
  • 2
  • 15
  • 29
-2

how can i get the coordinates of the matches find in the two images,that is the coordinates of the matches in the first image and the coordinates of the matches in the second?

elia
  • 1
  • 1