From c362a071b24436699580908b57d00af29d435df8 Mon Sep 17 00:00:00 2001
From: asantamaria <asantamaria@iri.upc.edu>
Date: Thu, 31 May 2018 17:26:43 +0200
Subject: [PATCH] FIX matcher to use other matchers than flannbased

---
 src/matchers/matcher_base.cpp | 64 +++++++++++++++++++++--------------
 src/matchers/matcher_base.h   | 34 ++++++++++---------
 2 files changed, 56 insertions(+), 42 deletions(-)

diff --git a/src/matchers/matcher_base.cpp b/src/matchers/matcher_base.cpp
index 15290d5..3a186e6 100644
--- a/src/matchers/matcher_base.cpp
+++ b/src/matchers/matcher_base.cpp
@@ -7,7 +7,9 @@
 
 namespace vision_utils {
 
-MatcherBase::MatcherBase(const std::string& _type)
+MatcherBase::MatcherBase(const std::string& _type) :
+        match_type_(MATCH),
+        matcher_type_(_type)
 {
     params_base_ptr_ = std::make_shared<MatcherParamsBase>(_type);
 }
@@ -40,41 +42,51 @@ Scalar MatcherBase::match(const cv::Mat& _desc1,
 
 std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1,
                                        const cv::Mat& _desc2,
-                                       const int& desc_size_bytes,
-                                       DMatchVector& matches,
+                                       const int&     _desc_size_bytes,
+                                       DMatchVector&  _matches,
                                        cv::InputArray _mask)
 {
+    clock_t tStart = clock();
+
     std::vector<Scalar> normalized_scores;
 
-    if (params_base_ptr_->match_type==MATCH)
+    if (!_desc1.empty() && !_desc2.empty())
     {
-        clock_t tStart = clock();
-        if (!_desc1.empty() && !_desc2.empty())
+        if (params_base_ptr_->match_type==MATCH)
         {
             // The following line is needed because flann by default is built with .
             // This conversion is required when using with hamming descriptors outputs
             // like BRIEF, ORB, FREAK, AKAZE etc
-            if (_desc1.type()!=CV_32F)
+            if (_desc1.type()!=CV_32F && matcher_type_.compare("FLANNBASED")==0)
                 matcher_ = new cv::FlannBasedMatcher(new cv::flann::LshIndexParams(LSH_TABLE_NUM, LSH_KEY_SIZE, LSH_MULTI_PROBE_LEVEL));
 
-            matcher_->match( _desc1, _desc2, matches, _mask);
+            matcher_->match( _desc1, _desc2, _matches, _mask);
 
             // here we get only the best match score
-            for (auto m : matches)
-                normalized_scores.push_back(1.0 - m.distance/(desc_size_bytes*8));
+            for (auto m : _matches)
+                normalized_scores.push_back(1.0 - m.distance/(_desc_size_bytes*8));
         }
-        comp_time_ = (double)(clock() - tStart) / CLOCKS_PER_SEC;
+        else if (params_base_ptr_->match_type == KNNMATCH || params_base_ptr_->match_type == RADIUSMATCH)
+        {
+            std::vector<DMatchVector> matches_vec;
+            std::vector<Scalar> normalized_scores_vec = match( _desc1, _desc2, _desc_size_bytes, matches_vec, _mask);
+            for (auto match_candidates : matches_vec)
+                _matches.push_back(match_candidates[0]);
+        }
+        else
+            std::cerr << "[" << name_ << "]:Wrong match type or output object." << std::endl;
     }
-    else
-        std::cerr << "[" << name_ << "]:Wrong match type or output object." << std::endl;
+
+    comp_time_ = (double)(clock() - tStart) / CLOCKS_PER_SEC;
+
     return normalized_scores;
 }
 
-std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1,
-                                       const cv::Mat& _desc2,
-                                       const int& desc_size_bytes,
-                                       std::vector< DMatchVector >& matches,
-                                       cv::InputArray _mask)
+std::vector<Scalar> MatcherBase::match(const cv::Mat&               _desc1,
+                                       const cv::Mat&               _desc2,
+                                       const int&                   _desc_size_bytes,
+                                       std::vector< DMatchVector >& _matches,
+                                       cv::InputArray               _mask)
 {
     std::vector<Scalar> normalized_scores;
 
@@ -86,14 +98,14 @@ std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1,
             // The following line is needed because flann by default is built with .
             // This conversion is required when using with hamming descriptors outputs
             // like BRIEF, ORB, FREAK, AKAZE etc
-            if (_desc1.type()!=CV_32F)
+            if (_desc1.type()!=CV_32F && matcher_type_.compare("FLANNBASED")==0)
                 matcher_ = new cv::FlannBasedMatcher(new cv::flann::LshIndexParams(LSH_TABLE_NUM, LSH_KEY_SIZE, LSH_MULTI_PROBE_LEVEL));
-            matcher_->knnMatch(_desc1, _desc2, matches, 2);
+            matcher_->knnMatch(_desc1, _desc2, _matches, 2, _mask);
 
             // here we get only the best match score
-            for (auto m : matches)
+            for (auto m : _matches)
                 if (m.size()>0)
-                    normalized_scores.push_back(1.0 - m[0].distance/(desc_size_bytes*8));
+                    normalized_scores.push_back(1.0 - m[0].distance/(_desc_size_bytes*8));
                 else
                     normalized_scores.push_back(0.0);
         }
@@ -107,13 +119,13 @@ std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1,
             // The following line is needed because flann by default is built with .
             // This conversion is required when using with hamming descriptors outputs
             // like BRIEF, ORB, FREAK, AKAZE etc
-            if (_desc1.type()!=CV_32F)
+            if (_desc1.type()!=CV_32F && matcher_type_.compare("FLANNBASED")==0)
                 matcher_ = new cv::FlannBasedMatcher(new cv::flann::LshIndexParams(LSH_TABLE_NUM, LSH_KEY_SIZE, LSH_MULTI_PROBE_LEVEL));
-            matcher_->radiusMatch(_desc1, _desc2, matches, 2, _mask);
+            matcher_->radiusMatch(_desc1, _desc2, _matches, 20.0, _mask);
 
-            for (auto m : matches)
+            for (auto m : _matches)
                 if (m.size()>0)
-                    normalized_scores.push_back(1.0 - m[0].distance/(desc_size_bytes*8));
+                    normalized_scores.push_back(1.0 - m[0].distance/(_desc_size_bytes*8));
                 else
                     normalized_scores.push_back(0.0);
         }
diff --git a/src/matchers/matcher_base.h b/src/matchers/matcher_base.h
index 9df1412..27d2a0b 100644
--- a/src/matchers/matcher_base.h
+++ b/src/matchers/matcher_base.h
@@ -9,8 +9,8 @@ typedef cv::Ptr<cv::DescriptorMatcher> FeatureMatcherPtr;
 // The following definitions are required because flann by default is built with .
 // This conversion is required when using with hamming descriptors outputs
 // like BRIEF, ORB, FREAK, AKAZE etc
-#define LSH_TABLE_NUM 20			// The number of hash tables to use (between 10 and 30 usually).
-#define LSH_KEY_SIZE 15				// The size of the hash key in bits (between 10 and 20 usually)
+#define LSH_TABLE_NUM 30			// The number of hash tables to use (between 10 and 30 usually).
+#define LSH_KEY_SIZE 20				// The size of the hash key in bits (between 10 and 20 usually)
 #define LSH_MULTI_PROBE_LEVEL 2		// The number of bits to shift to check for neighboring buckets (0 is regular LSH, 2 is recommended)
 
 namespace vision_utils
@@ -25,8 +25,8 @@ VU_PTR_TYPEDEFS(MatcherBase);
 VU_STRUCT_PTR_TYPEDEFS(MatcherParamsBase);
 
 enum MATCH_TYPE{
-    MATCH = 1,
-    KNNMATCH = 2,
+    MATCH       = 1,
+    KNNMATCH    = 2,
     RADIUSMATCH = 3
 };
 
@@ -36,7 +36,7 @@ enum MATCH_TYPE{
 struct MatcherParamsBase: public ParamsBase
 {
         std::string type;
-        int match_type = MATCH; 		 //< Type of Match. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+        int match_type = MATCH; 	     //< Type of Match. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
         double min_norm_score;    		 //< [-1..0]: awful match; 1: perfect match; out of [-1,1]: error
         Scalar ransac_epipolar_distance; //< distance to epipolar line
         Scalar ransac_confidence_prob;   //< confidence probability
@@ -73,22 +73,22 @@ class MatcherBase : public VUBase, public std::enable_shared_from_this<MatcherBa
 
         void setParams(const MatcherParamsBasePtr _params);
 
-        std::vector<Scalar> match(const cv::Mat& _desc1,
-                                  const cv::Mat& _desc2,
-                                  const int& desc_size_bytes,
-                                  DMatchVector& matches,
-                                  cv::InputArray _mask=cv::noArray());
+        Scalar match(const cv::Mat& _desc1,
+                     const cv::Mat& _desc2,
+                     const int&     _desc_size_bytes,
+                     cv::DMatch&    _match);
 
         std::vector<Scalar> match(const cv::Mat& _desc1,
                                   const cv::Mat& _desc2,
-                                  const int& desc_size_bytes,
-                                  std::vector< DMatchVector >& matches,
+                                  const int&     _desc_size_bytes,
+                                  DMatchVector&  _matches,
                                   cv::InputArray _mask=cv::noArray());
 
-        Scalar match(const cv::Mat& _desc1,
-                     const cv::Mat& _desc2,
-                     const int& desc_size_bytes,
-                     cv::DMatch& _match);
+        std::vector<Scalar> match(const cv::Mat&               _desc1,
+                                  const cv::Mat&               _desc2,
+                                  const int&                   _desc_size_bytes,
+                                  std::vector< DMatchVector >& _matches,
+                                  cv::InputArray               _mask=cv::noArray());
 
         std::vector<Scalar> robustMatch(const KeyPointVector& _raw_kps1,
                                         const KeyPointVector& _raw_kps2,
@@ -167,6 +167,8 @@ class MatcherBase : public VUBase, public std::enable_shared_from_this<MatcherBa
 
         int match_type_;
 
+        std::string matcher_type_; //< Type of Matcher.
+
         FeatureMatcherPtr matcher_;
 
         MatcherParamsBasePtr params_base_ptr_;
-- 
GitLab