Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually make pairwise matching in OpenCV from features key points

Here's my problem. I manually extracted key points features with SURF on multiple images. But I also already know which pair of points are going to match. The thing is, I'm trying to create my matching pairs, but I don't understand how. I tried by looking at the code, but it's a mess.

Right now, I know that the size of the features.descriptors, a matrix, is the same as the number of key points (the other dimension is 1). In the code, to detect matching pairs, it's only using the descriptors, so it's comparing rows (or columns, I'm not sure) or two descriptors matrix and determined if there's anything in common.

But in my case, I already know that there's a match between keypoint i from image 1 and keypoint j from image 2. How do I describe that as a MatchesInfo value. Particularly the element matches of type std::vector< cv::DMatch >.

EDIT: So, for this, I don't need to use any matcher or anything like this. I know which pairs are going together!

like image 305
widgg Avatar asked Mar 15 '12 15:03

widgg


Video Answer


1 Answers

If I understood you're question correctly, I assume that you want the keypoint matches in std::vector<cv::DMatch> for the purpose of drawing them with the OpenCV cv::drawMatches or usage with some similar OpenCV function. Since I was also doing matching "by hand" recently, here's my code that draws up arbitrary matches contained originally in a 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;
    // 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");

Alternatively, if you need fuller information about the matches for purposes more complicated than drawing then you might also want to set the distance between matches to a proper value. E.g. if you want to calculate distances using L2 distance, 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, for this a reference to feature descriptor vectors is also needed):

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 snippet, descX[i] is a descriptor for featsX[i], each element of the inner vector being one component of the descriptor vector. Also, note that all descriptor vectors should have the same dimensionality.

like image 115
penelope Avatar answered Oct 04 '22 14:10

penelope