如果我正确理解您的问题,我假设您希望关键点匹配以std::vector<cv::DMatch>
使用 OpenCV 绘制它们cv::drawMatches
或使用一些类似的 OpenCV 函数。由于我最近也在“手动”进行匹配,所以这是我的代码,它绘制了最初包含在 a 中的任意匹配std::vector<std::pair <int, int> > aMatches
并将它们显示在窗口中:
const cv::Mat& pic1 = img_1_var;
const cv::Mat& pic2 = img_2_var;
const std::vector <cv::KeyPoint> &feats1 = img_1_feats;
const std::vector <cv::KeyPoint> &feats2 = img_2_feats;
// you of course can work directly with original objects
// but for drawing you only need const references to
// images & their corresponding extracted feats
std::vector <std::pair <int, int> > aMatches;
// fill aMatches manually - one entry is a pair consisting of
// (index_in_img_1_feats, index_in_img_2_feats)
// the next code draws the matches:
std::vector <cv::DMatch> matches;
matches.reserve((int)aMatches.size());
for (int i=0; i < (int)aMatches.size(); ++i)
matches.push_back(cv::DMatch(aMatches[i].first, aMatches[i].second,
std::numeric_limits<float>::max()));
cv::Mat output;
cv::drawMatches(pic1, feats1, pic2, feats2, matches, output);
cv::namedWindow("Match", 0);
cv::setWindowProperty("Match", CV_WINDOW_FULLSCREEN, 1);
cv::imshow("Match", output);
cv::waitKey();
cv::destroyWindow("Match");
或者,如果您出于比绘图更复杂的目的需要有关匹配的更全面信息,那么您可能还需要将匹配之间的距离设置为适当的值。例如,如果您想使用L2距离计算距离,则应替换以下行:
for (int i=0; i < (int)aMatches.size(); ++i)
matches.push_back(cv::DMatch(aMatches[i].first, aMatches[i].second,
std::numeric_limits<float>::max()));
有了这个(注意,为此还需要对特征描述符向量的引用):
cv::L2<float> cmp;
const std::vector <std::vector <float> > &desc1 = img_1_feats_descriptors;
const std::vector <std::vector <float> > &desc2 = img_2_feats_descriptors;
for (int i=0; i < (int)aMatches.size(); ++i){
float *firstFeat = &desc1[aMatches[i].first];
float *secondFeat = &desc2[aMatches[i].second];
float distance = cmp(firstFeat, secondFeat, firstFeat->size());
matches.push_back(cv::DMatch(aMatches[i].first, aMatches[i].second,
distance));
}
请注意,在最后一个片段中,descX[i]
是 的描述符featsX[i]
,内部向量的每个元素都是描述符向量的一个组成部分。另外,请注意所有描述符向量应该具有相同的维度。