交叉发布here

我注意到FlannBasedMatcher::match函数有一个参数mask,因此我尝试使用以下代码:

#include<opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;
using namespace std;

int main() {
    Mat rightImg = imread("right.jpg", 0);
    Mat leanImg = imread("lean.jpg", 0);
    if (!rightImg.data || !leanImg.data) {
        cout << "Fail to read your image. Please check your path.\n";
        return -1;
    }
    resize(leanImg, leanImg, rightImg.size());
    int minHessian = 400;
    SurfFeatureDetector detector(minHessian);
    vector<KeyPoint> keypoints_right, keypoints_lean;

    detector.detect(rightImg, keypoints_right);
    detector.detect(leanImg, keypoints_lean);

    Mat med_right, med_lean;
    drawKeypoints(rightImg, keypoints_right, med_right);
    drawKeypoints(leanImg, keypoints_lean, med_lean);

    SurfDescriptorExtractor extractor;
    Mat descriptors_right, descriptors_lean;
    extractor.compute(rightImg, keypoints_right, descriptors_right);
    extractor.compute(leanImg, keypoints_lean, descriptors_lean);

    FlannBasedMatcher matcher;
    vector< DMatch > matches;

    Mat mask(descriptors_right.rows, descriptors_lean.rows, CV_8UC1, Scalar(0));

    Mat target(rightImg.size(), CV_8UC1, Scalar(255));
    ellipse(target, Point(rightImg.cols / 2, rightImg.rows / 2), Size(rightImg.cols / 2, rightImg.rows / 2), 0, 0, 360, Scalar(0), CV_FILLED);

    for (int i = 0; i < mask.rows; i++) {
        uchar* pixrow = mask.ptr<uchar>(i);
        for (int j = 0; j < mask.cols; j++) {
            if (target.at<uchar>(keypoints_right[i].pt) == 255)
                pixrow[j] = 255;
        }
    }

    matcher.match(descriptors_right, descriptors_lean, matches/*, mask*/);//use it or not to test

    Mat img_matches;
    drawMatches(rightImg, keypoints_right, leanImg, keypoints_lean,
        matches, img_matches, Scalar::all(-1), Scalar::all(-1),
        vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
    return 0;
    }

这是我的right.jpglean.jpg我不在乎right.jpg中心的那些点。所以我为此做了一个面具。 但我注意到,无论是否在函数mask中使用FlannBasedMatcher::match,我都会得到完全相同的结果。您可以使用蒙版,也可以不使用蒙版。我是否错过了某些内容,或者OpenCV的2.4.13中存在错误?谁能告诉我如何在mask中使用FlannBasedMatcher::match吗?我认为这是一个有用的参数。

最佳答案

来自docs:“FlannBasedMatcher不支持屏蔽描述符集的允许匹配,因为flann::Index不支持此功能。”有关测试匹配器是否支持屏蔽的方法,请参见DescriptorMatcher::isMaskSupported方法。

07-24 21:31