我正在尝试创建一个 C++ 程序,与一个输入图像相比,列表中有很多图像。我得到了整个工作,程序正在创建DMatch
匹配项。
现在我正在尝试确定与源图像进行比较的图像列表中的哪一个是最佳匹配。我首先尝试通过比较图像之间有多少匹配来做到这一点,但问题是当生成的图像有很多关键点时;他们也往往有很多比赛,至少在我的节目中。
那么如何确定图像数组中的哪一个与源图像最匹配?我正在使用此循环来确定匹配项,但它并没有真正起作用:
vector< vector<DMatch> > filteredMatches;
vector<int> goodIds;
Ptr<DescriptorMatcher> matcher(new BFMatcher(NORM_HAMMING, false));
printf("bad matches: ");
for(size_t i = 0; i < images.size();i++){
vector<DMatch> matches, good_matches;
matcher->clear();
matcher->match(images[i], tex_des, matches);
if(matches.size() < 8){
printf("F%d,", (int)i + 1);
continue;
}
double min_dist = 100;
for(size_t j = 0; j < matches.size(); j++ ){
double dist = matches[j].distance;
if( dist < min_dist )
min_dist = dist;
}
if(min_dist > 50.0){
printf("D%d,", (int)i + 1);
continue;
}
double good_dist = min_dist * 3;
for(size_t j = 0; j < matches.size(); j++ ){
if(matches[j].distance < good_dist)
good_matches.push_back(matches[j]);
}
size_t size = good_matches.size();
if(size < 8){
printf("M%d,", (int)i + 1);
continue;
}
vector<Point2f> srcPoints(size);
vector<Point2f> dstPoints(size);
for(size_t j = 0; j < size; j++){
srcPoints[j] = destination[good_matches[j].trainIdx].pt;
dstPoints[j] = keyPoints[i][good_matches[j].queryIdx].pt;
}
vector<unsigned char> inliersMask(srcPoints.size());
Mat H = findHomography(srcPoints, dstPoints, CV_FM_RANSAC, 3.0, inliersMask);
vector<DMatch> inliers;
for(size_t j = 0; j < inliersMask.size(); j++){
if(inliersMask[j]){
inliers.push_back(good_matches[j]);
}
}
if(inliers.size() < 4){
printf("S%d,", (int)i + 1);
continue;
}
filteredMatches.push_back(inliers);
goodIds.push_back((int)i);
H.release();
}
printf(" good matches: ");
int best = -1;
int amount = 0;
for(size_t i = 0; i < filteredMatches.size(); i++){
int size = (int)filteredMatches.at(i).size();
if(size < 8) continue;
printf("%d,", goodIds[i] + 1);
if(amount < size){
amount = size;
best = i;
}
}
if(best >= 0) printf(" best match on image: %d, keypoints: %d, ", goodIds[best] + 1, amount);
如果有人能指出我必须使用的功能或逻辑,我将不胜感激!