Skip to content

Instantly share code, notes, and snippets.

@YoshiRi
Created August 31, 2017 22:46
Show Gist options
  • Save YoshiRi/e62d771ba38058dba29617091b68d6f2 to your computer and use it in GitHub Desktop.
Save YoshiRi/e62d771ba38058dba29617091b68d6f2 to your computer and use it in GitHub Desktop.
Normal ORB matcher in cpp and choosing good pair using stereo constraint
// Look at https://github.com/vonzhou/opencv/blob/master/image-search/orb.cpp
std::vector<cv::DMatch> ratio_test(std::vector< std::vector<cv::DMatch> > matches12, double ratio, std::vector<cv::KeyPoint> kpl, std::vector<cv::KeyPoint> kpr){
std::vector<cv::DMatch> good_matches;
for(int i = 0; i < matches12.size(); i++){
if(abs(kpl[matches12[i][0].queryIdx].pt.y - kpr[matches12[i][0].trainIdx].pt.y ) < 1){ // in the same line
if(matches12[i][0].distance < ratio * matches12[i][1].distance){
good_matches.push_back(matches12[i][0]);
}
}
}
return good_matches;
}
void show_keymatching(const cv::Mat left,const cv::Mat right)
{
cv::Ptr<cv::FeatureDetector> detector = cv::ORB::create();
std::vector<cv::KeyPoint> kpl,kpr;
detector->detect(left, kpl);
detector->detect(right, kpr);
cv::Ptr<cv::DescriptorExtractor> extractor = cv::ORB::create();
cv::Mat desl,desr;
extractor->compute(left, kpl, desl);
extractor->compute(right, kpr, desr);
std::vector< std::vector<cv::DMatch> > matches12, matches21;
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
matcher->knnMatch( desl, desr, matches12, 2 );
//matcher->knnMatch( desr, desl, matches21, 2 );
std::vector<cv::DMatch> good_matches1, good_matches2;
double ratio = 0.8;
good_matches1 = ratio_test(matches12, ratio, kpl, kpr);
//good_matches2 = ratio_test(matches21, ratio);
cv::Mat img_matches;
cv::drawMatches( left, kpl, right, kpr,
good_matches1, img_matches, cv::Scalar::all(-1), cv::Scalar::all(-1),
std::vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
imshow( "Good Matches", img_matches );
cv::waitKey(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment