some problem about knnMatch
Hi guys, I am a frensh man in opencv developers, I read the book “Mastering OpenCV with Practical Computer Vision Projects”, in the three character, I find a problem, the book use ORB
as detector, FREAK
as DescriptorExtractor
, and the BFMatcher
as matcher. When I call the knnMatch
function, the program pop a bug. Here is my code:
cv::Mat patternImg = cv::imread("starry_night_small.jpg", 0);//starry_night
cv::Mat testImg = cv::imread("starry_night_test.jpg", 0);
std::vector<cv::KeyPoint> queryKeypoints;
std::vector<cv::KeyPoint> trainKeypoints;
std::vector<std::vector<cv::DMatch>> matches;
cv::Mat queryDescriptor;
cv::Mat trainDescriptor;
cv::ORB detector(1000);
cv::FREAK extractor(false,false);
cv::BFMatcher matcher(cv::NORM_HAMMING, true);
detector.detect(patternImg, trainKeypoints);
detector.detect(testImg, queryKeypoints);
extractor.compute(patternImg, trainKeypoints, trainDescriptor);
extractor.compute(testImg, queryKeypoints, queryDescriptor);
matcher.knnMatch(queryDescriptor, trainDescriptor, matches, 1);
cv::imshow("pattern", patternImg);
cout << "matches size-->" << matches.size() << endl;
cv::waitKey(0);
but when I run the program, which crashes and shows the error:
OpenCV Error: Assertion failed (K == 1 && update == 0 && mask.empty()) in cv::batchDistance, file ..\..\..\sources\modules\core\src\stat.cpp, line 2500
can you help me how can I fix this?
Reading the assertion message, it seems that your K is 1 (last parameter in
knnMatch
) and you have no mask. Try to read the function's documentation.