BFMatcher crosscheck
Hey guys,
Ive got a problem with the crosscheck feature in the BFMatcher. Im trying to use this on android, but somehow the matcher seems to do crap. It produces lots of good results, but sometimes it returns queryIdx' and trainIdx' that are far away from the number of Matches. Basically I declare the matcher to use crosscheck and I also use knnMatch with k=1. After they got matched, I log the indices. Lots of them are correct, but not all.
BFMatcher matcher(NORM_HAMMING,true);
matcher.knnMatch(queryDescriptors, trainDescriptors, matches, 1);
for (unsigned int i=0; i<matches.size(); i++){
LOGE("query Idx:%d, trainIdx:%d", matches[i][0].queryIdx, matches[i][0].trainIdx);
/* circle(*frame,queryPoints[matches[i][0].queryIdx].pt, 1, Scalar(255,255,0,255));
line(*frame,queryPoints[matches[i][0].queryIdx].pt, trainPoints[matches[i][0].trainIdx].pt, Scalar(255,255,255,255));
circle(*frame,trainPoints[matches[i][0].trainIdx].pt, 1, Scalar(255,0,0,255));*/
}
edit:
Ok, I solved it on my own. I debugged the code on android and in the end its pretty obvious. The problem is that the matcher creates an entry in std::vector<std::vector<dmatch>> matches, but if the match is invalid, it does not fill this entry. So obviously this entry is empty and if you access it, there is crap in it. So a simple check if that entry is empty solves everything.
for (unsigned int i=0; i<matches.size(); i++){
if (!matches[i].empty())
LOGE("query Idx:%d, trainIdx:%d", matches[i][0].queryIdx, matches[i][0].trainIdx);
}
Some of the log
(13765): query Idx:27, trainIdx:18
(13765): query Idx:1075809540, trainIdx:1075809540
(13765): query Idx:1, trainIdx:1514815912
(13765): query Idx:7, trainIdx:49
(13765): query Idx:31, trainIdx:7
(13765): query Idx:104, trainIdx:18
When I run your code at first I've got this error:
error: ‘struct cv::DMatch’ has no member named ‘empty’
but then I've noticed you wrote that you use std::vector<std::vector<dmatch>> matchesSo from what you wrote here, you have a set of matches that you check only the first match!! (match[i][0]) that why you never go deep to the knn garbage I've solved it by looking at the distance as I wrote in my answer.