some problem about knnMatch

asked 2014-09-23 06:43:27 -0600

hyb gravatar image

updated 2014-09-23 06:55:42 -0600

thdrksdfthmn gravatar image

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;

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?

edit retag flag offensive close merge delete


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.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-09-23 07:00:58 -0600 )edit