0

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

我认为甚至没有一个匹配是正确的。

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

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


输入图像

在此处输入图像描述

在此处输入图像描述

错误和不确定的结果

在此处输入图像描述

在此处输入图像描述

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

2 回答 2

0

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

        matched1.push_back(keypoints1[first.queryIdx]);
        matched2.push_back(keypoints2[first.trainIdx]);
于 2015-11-09T23:49:47.880 回答
-2

我怎样才能得到在两个图像中找到的匹配的坐标,即第一图像中匹配的坐标和第二图像中匹配的坐标?

于 2020-01-13T14:25:25.803 回答