knnMatch with k = 2 returns 0 nearest-neighbour with images with 1 keypoint
I am basically extracting many keypoints with SURF from similar images and adding them to the
BFMatcher(NORM_L2)
After I have calculate all descriptors from my keypoints and I have a
Mat descriptors;
vector<Mat> vectDesc;
vectDesc.push_back(descriptors);
I add them to the matcher->add(vectDesc);
Now when I add an image with only 1 keypoint/descriptor and I use knnMatch:
matcher->knnMatch(queryDesc,matches,2);
I get a vector with 0 Nearest-neighbour. If i do:
for(auto i = 0; i <matches.size(); i++) {
cout << "matches size: "<<matches[i].size()<<endl;
I get printed
"matches size: 0"
"matches size: 0"
"matches size: 0"
...
This happens only when I have an image with only one keypoint/descriptor. Before the knnMatch works fine.
What it could be? (Version 2.4.5)
I have tried with SIFT and it happens the same thing
I have tried to check if matcher.getTrainDescriptors();
contains my descriptors and effectively it contains everything. To check this, if i do:
// (Get the size of the descriptors Mat associated to the first training image)
cout << matcher->getTrainDescriptors().at(0).size();
I get: [128 x 32]. This means that descriptors are there but the knnMatches return an empty vector
Also NOTE that If I change .knnMatch
with a simple .match
the matcher returns all DMatches normally! The code fails only with knnMatch