以下是正在进行匹配的代码:
// Match
vector<vector<DMatch> > knnMatches;
Mat testDesc;
int knn = 1;
vector<int> bestMatches;
FeatureDetector *detector = new SurfFeatureDetector();
DescriptorExtractor *extractor = new SurfDescriptorExtractor();
DescriptorMatcher *matcher = new FlannBasedMatcher(); // ANN
trainDesc.push_back(desc);
trainImgs.push_back(img_name);
int temp = 0;
bestMatches.push_back(temp);
matcher->knnMatch(testDesc, knnMatches, knn);
// Filter results
for (vector<vector<DMatch> >::const_iterator it = knnMatches.begin();
it != knnMatches.end(); ++it) {
for (vector<DMatch>::const_iterator it2 = it->begin(); it2 != it->end();
++it2) {
++bestMatches[(*it2).imgIdx];
}
}
// Find best match
int bestScore = 0;
int bestIdx = -1;
for (int i = 0; i < bestMatches.size(); ++i) {
if (bestMatches[i] >= bestScore) {
bestScore = bestMatches[i];
cout << "best Score inside if is:" << bestScore << endl;
cout << "best Matches inside if is:" << bestMatches[i] << endl;
bestIdx = i;
}
}
if (debug > 0)
cout << "Best match: " << trainImgs[bestIdx] << " Score: " << bestScore
<< "bestidx: " << bestIdx << endl;
cout << trainImgs[bestIdx] << endl;