BFMatcher knnMatch

时间:2015-02-25 21:23:36

标签: c++ opencv brute-force

我尝试在BFMatcher上实现knnMatch,如下所示:

BFMatcher matcher(NORM_L2, true);
vector<DMatch> matches;
//matcher.match(descriptors1, descriptors2, matches);
matcher.knnMatch(descriptors1, descriptors2, matches, 2);

并收到以下错误:

fiducialMain.cpp: In function ‘void fiducialCalc(cv::Mat, cv::Mat, cv::Mat&, cv::Mat&, int&)’:
fiducialMain.cpp:98:56: error: no matching function for call to ‘cv::BFMatcher::knnMatch(cv::Mat&, cv::Mat&, std::vector<cv::DMatch>&, int)’
  matcher.knnMatch(descriptors1, descriptors2, matches,2);
                                                        ^
fiducialMain.cpp:98:56: note: candidates are:
In file included from fiducialMain.cpp:15:0:
/usr/local/include/opencv2/features2d/features2d.hpp:1116:18: note: void cv::DescriptorMatcher::knnMatch(const cv::Mat&, const cv::Mat&, std::vector<std::vector<cv::DMatch> >&, int, const cv::Mat&, bool) const
     CV_WRAP void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
                  ^
/usr/local/include/opencv2/features2d/features2d.hpp:1116:18: note:   no known conversion for argument 3 from ‘std::vector<cv::DMatch>’ to ‘std::vector<std::vector<cv::DMatch> >&’
/usr/local/include/opencv2/features2d/features2d.hpp:1130:18: note: void cv::DescriptorMatcher::knnMatch(const cv::Mat&, std::vector<std::vector<cv::DMatch> >&, int, const std::vector<cv::Mat>&, bool)
     CV_WRAP void knnMatch( const Mat& queryDescriptors, CV_OUT vector<vector<DMatch> >& matches, int k,
                  ^
/usr/local/include/opencv2/features2d/features2d.hpp:1130:18: note:   no known conversion for argument 2 from ‘cv::Mat’ to ‘std::vector<std::vector<cv::DMatch> >&’

有人可以解释这个错误吗?

2 个答案:

答案 0 :(得分:6)

请再看一下the docs

而普通匹配函数的结果为vector<DMatch>

knnMatch(大声说:k-nearest-neighbors!)导致几个(k)向量,因此你需要:

矢量&lt;矢量&lt; DMatch&gt; &GT;匹配

结果

答案 1 :(得分:2)

BFMatcher 的论据不正确。将 crossCheck 设置为 true 时,每个关键点只能有一个匹配项。然而,对于 knnMatch ,您需要有多个匹配项。所以你的代码应该是:

BFMatcher matcher(NORM_L2);
std::vector<vector<DMatch> > matches;
matcher.knnMatch(descriptors1, descriptors2, matches,2);

std::vector<DMatch> match1;
std::vector<DMatch> match2;

for(int i=0; i<matches.size(); i++)
{
    match1.push_back(matches[i][0]);
    match2.push_back(matches[i][1]);
}

Mat img_matches1, img_matches2;
drawMatches(img1, kp1, img2, kp2, match1, img_matches1);
drawMatches(img1, kp1, img2, kp2, match2, img_matches2);