OpenCV和DAISY描述符

时间:2015-11-09 21:39:59

标签: opencv feature-detection feature-extraction flannbasedmatcher

我正在尝试使用DAISY和FlannBasedMatcher在同一图像的两个透视图之间进行特征匹配。

我认为甚至没有一场比赛是正确的。

注意:每次运行程序时我也会得到不同的结果,但我认为这是预期的行为,如下所述:FlannBasedMatcher returning different results

那么我做错了什么?为什么这些比赛如此糟糕?

输入图像

enter image description here

enter image description here

错误&非确定性结果

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

2 个答案:

答案 0 :(得分:0)

对不起。我发现了我的错误。我在以下行中反转了索引:

        matched1.push_back(keypoints1[first.queryIdx]);
        matched2.push_back(keypoints2[first.trainIdx]);

答案 1 :(得分:-2)

如何获取在两个图像中找到的匹配项的坐标,即第一张图像中的匹配项坐标和第二张图像中的匹配项坐标?