If I understand that you are asking the right question, I assume that you want the key match to be in std::vector<cv::DMatch> for the purpose of drawing them using OpenCV cv::drawMatches or using with some similar OpenCV function . Since I also performed manual matching recently, here is my code that draws arbitrary matches originally contained in std::vector<std::pair <int, int> > aMatches and displays them in a window:
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;
Alternatively, if you need more complete match information for goals more complex than drawing, you can also set the distance between matches to the correct value. For example. if you want to calculate distances using distance L2, you should replace the following line:
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()));
with this (note, this also needs a link to function descriptor vectors):
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)); }
Note that in the last fragment descX[i] is a descriptor for featsX[i] , each element of the internal vector is one of the components of the descriptor vector. Also note that all descriptor vectors must have the same dimension.
penelope
source share