Skip to content

Instantly share code, notes, and snippets.

@panovr
Created March 29, 2014 04:14
Show Gist options
  • Save panovr/9848256 to your computer and use it in GitHub Desktop.
Save panovr/9848256 to your computer and use it in GitHub Desktop.
FindMatches
void FindMatches(const Mat &img1, const Mat &img2, vector<MatchPair> &ret_matches)
{
Mat grey1, grey2;
cvtColor(img1, grey1, CV_BGR2GRAY);
cvtColor(img2, grey2, CV_BGR2GRAY);
// Detecting keypoints
Ptr<FeatureDetector> detector = FeatureDetector::create("SURF");
Ptr<DescriptorExtractor> descriptorExtractor = DescriptorExtractor::create("SURF");
Ptr<DescriptorMatcher> descriptorMatcher = DescriptorMatcher::create("FlannBased");
vector<KeyPoint> keypoints1, keypoints2;
detector->detect(grey1, keypoints1);
detector->detect(grey2, keypoints2);
// Computing descriptors
Mat descriptors1, descriptors2;
descriptorExtractor->compute(grey1, keypoints1, descriptors1);
descriptorExtractor->compute(grey2, keypoints2, descriptors2);
// Matching descriptors
vector<DMatch> matches;
descriptorMatcher->match(descriptors1, descriptors2, matches);
ret_matches.clear();
for (size_t i = 0; i < matches.size(); ++i)
{
int i1 = matches[i].queryIdx;
int i2 = matches[i].trainIdx;
const KeyPoint &kp1 = keypoints1[i1];
const KeyPoint &kp2 = keypoints2[i2];
// This is ad-hoc, adjust to suit your need
if (matches[i].distance < 0.20)
{
ret_matches.push_back(MatchPair(kp1.pt, kp2.pt));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment