P3d0r
P3d0r

Reputation: 1099

BFMatcher knnMatch

I try to implement knnMatch on BFMatcher as follows:

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

And receive the following error:

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> >&’

Can anybody explain this error?

Upvotes: 4

Views: 22325

Answers (2)

Harsh Wardhan
Harsh Wardhan

Reputation: 2148

Your arguments for BFMatcher are not correct. When you set crossCheck as true, you can have only one match per keypoint. Whereas, for knnMatch you need to have more than one match. So your code should be like:

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

Upvotes: 4

berak
berak

Reputation: 39796

please have another look at the docs

while the ordinary match function has a vector<DMatch> as result,

knnMatch (say loud : k-nearest-neighbours !) results in several(k) vectors, thus you need a :

vector< vector< DMatch > > matches

for the result

Upvotes: 9

Related Questions