FlannBasedMatcher :: match中的掩码无法正常工作

时间:2017-11-16 20:05:30

标签: c++ opencv image-processing

交叉发布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,我都会得到相同的结果。您可以使用或不使用蒙版来重现它。我错过了某些内容,或者我的2.4.13中的OpenCV有错误吗?任何人都可以告诉我如何使用mask中的FlannBasedMatcher::match吗?我认为这是一个有用的参数..

1 个答案:

答案 0 :(得分:1)

docs:&#34; FlannBasedMatcher不支持屏蔽允许的描述符集匹配,因为flann :: Index不支持这个。&#34;有关测试匹配器是否支持屏蔽的方法,请参阅DescriptorMatcher::isMaskSupported方法。