Home | History | Annotate | Download | only in src
      1 /*
      2  * RobustMatcher.h
      3  *
      4  *  Created on: Jun 4, 2014
      5  *      Author: eriba
      6  */
      7 
      8 #ifndef ROBUSTMATCHER_H_
      9 #define ROBUSTMATCHER_H_
     10 
     11 #include <iostream>
     12 
     13 #include <opencv2/core/core.hpp>
     14 #include <opencv2/highgui/highgui.hpp>
     15 #include <opencv2/features2d/features2d.hpp>
     16 
     17 class RobustMatcher {
     18 public:
     19   RobustMatcher() : ratio_(0.8f)
     20   {
     21     // ORB is the default feature
     22     detector_ = cv::ORB::create();
     23     extractor_ = cv::ORB::create();
     24 
     25     // BruteFroce matcher with Norm Hamming is the default matcher
     26     matcher_ = cv::makePtr<cv::BFMatcher>((int)cv::NORM_HAMMING, false);
     27 
     28   }
     29   virtual ~RobustMatcher();
     30 
     31   // Set the feature detector
     32   void setFeatureDetector(const cv::Ptr<cv::FeatureDetector>& detect) {  detector_ = detect; }
     33 
     34   // Set the descriptor extractor
     35   void setDescriptorExtractor(const cv::Ptr<cv::DescriptorExtractor>& desc) { extractor_ = desc; }
     36 
     37   // Set the matcher
     38   void setDescriptorMatcher(const cv::Ptr<cv::DescriptorMatcher>& match) {  matcher_ = match; }
     39 
     40   // Compute the keypoints of an image
     41   void computeKeyPoints( const cv::Mat& image, std::vector<cv::KeyPoint>& keypoints);
     42 
     43   // Compute the descriptors of an image given its keypoints
     44   void computeDescriptors( const cv::Mat& image, std::vector<cv::KeyPoint>& keypoints, cv::Mat& descriptors);
     45 
     46   // Set ratio parameter for the ratio test
     47   void setRatio( float rat) { ratio_ = rat; }
     48 
     49   // Clear matches for which NN ratio is > than threshold
     50   // return the number of removed points
     51   // (corresponding entries being cleared,
     52   // i.e. size will be 0)
     53   int ratioTest(std::vector<std::vector<cv::DMatch> > &matches);
     54 
     55   // Insert symmetrical matches in symMatches vector
     56   void symmetryTest( const std::vector<std::vector<cv::DMatch> >& matches1,
     57                      const std::vector<std::vector<cv::DMatch> >& matches2,
     58                      std::vector<cv::DMatch>& symMatches );
     59 
     60   // Match feature points using ratio and symmetry test
     61   void robustMatch( const cv::Mat& frame, std::vector<cv::DMatch>& good_matches,
     62                       std::vector<cv::KeyPoint>& keypoints_frame,
     63                       const cv::Mat& descriptors_model );
     64 
     65  // Match feature points using ratio test
     66  void fastRobustMatch( const cv::Mat& frame, std::vector<cv::DMatch>& good_matches,
     67                        std::vector<cv::KeyPoint>& keypoints_frame,
     68                        const cv::Mat& descriptors_model );
     69 
     70 private:
     71   // pointer to the feature point detector object
     72   cv::Ptr<cv::FeatureDetector> detector_;
     73   // pointer to the feature descriptor extractor object
     74   cv::Ptr<cv::DescriptorExtractor> extractor_;
     75   // pointer to the matcher object
     76   cv::Ptr<cv::DescriptorMatcher> matcher_;
     77   // max ratio between 1st and 2nd NN
     78   float ratio_;
     79 };
     80 
     81 #endif /* ROBUSTMATCHER_H_ */
     82