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?
Wrong & non-deterministic results
#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;
}



