From 19cd710438cb89440ad2856c89ac02430105d845 Mon Sep 17 00:00:00 2001
From: asantamaria <asantamaria@iri.upc.edu>
Date: Thu, 17 May 2018 16:20:56 +0200
Subject: [PATCH] Add ransacTest function and parameters (.cpp, .h, .yamls)

---
 src/examples/yaml/ACTIVESEARCH.yaml           |  3 ++
 src/examples/yaml/BRUTEFORCE.yaml             |  4 ++
 src/examples/yaml/BRUTEFORCE_HAMMING.yaml     |  3 ++
 src/examples/yaml/BRUTEFORCE_HAMMING_2.yaml   |  3 ++
 src/examples/yaml/BRUTEFORCE_L1.yaml          |  3 ++
 src/examples/yaml/FLANNBASED.yaml             |  5 +-
 src/examples/yaml/TRACKFEATURES.yaml          |  8 ++-
 .../matcher_bruteforce_load_yaml.cpp          |  5 ++
 src/matchers/matcher_base.cpp                 | 27 ++++------
 src/matchers/matcher_base.h                   | 13 +++--
 src/test/data/ACTIVESEARCH.yaml               |  3 ++
 src/test/data/BRUTEFORCE.yaml                 |  3 ++
 src/test/data/BRUTEFORCE_HAMMING.yaml         |  3 ++
 src/test/data/BRUTEFORCE_HAMMING_2.yaml       |  3 ++
 src/test/data/BRUTEFORCE_L1.yaml              |  5 +-
 src/test/data/FLANNBASED.yaml                 |  5 +-
 src/test/data/TRACKFEATURES.yaml              |  8 ++-
 src/test/data/base_classes.yaml               |  3 ++
 src/test/gtest_matcher_base.cpp               | 54 +++++++++++++++++++
 19 files changed, 133 insertions(+), 28 deletions(-)

diff --git a/src/examples/yaml/ACTIVESEARCH.yaml b/src/examples/yaml/ACTIVESEARCH.yaml
index 1f6a692..7dcc3e5 100644
--- a/src/examples/yaml/ACTIVESEARCH.yaml
+++ b/src/examples/yaml/ACTIVESEARCH.yaml
@@ -27,6 +27,9 @@ matcher:
   type: "FLANNBASED"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
   min normalized score: 0.85 
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
     
 algorithm:
   type: "ACTIVESEARCH"   
diff --git a/src/examples/yaml/BRUTEFORCE.yaml b/src/examples/yaml/BRUTEFORCE.yaml
index 5edfa46..65715f3 100644
--- a/src/examples/yaml/BRUTEFORCE.yaml
+++ b/src/examples/yaml/BRUTEFORCE.yaml
@@ -3,3 +3,7 @@ sensor:
   
 matcher:
   type: "BRUTEFORCE"
+  match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3 
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability 
\ No newline at end of file
diff --git a/src/examples/yaml/BRUTEFORCE_HAMMING.yaml b/src/examples/yaml/BRUTEFORCE_HAMMING.yaml
index 42e63bc..53f9729 100644
--- a/src/examples/yaml/BRUTEFORCE_HAMMING.yaml
+++ b/src/examples/yaml/BRUTEFORCE_HAMMING.yaml
@@ -4,3 +4,6 @@ sensor:
 matcher:
   type: "BRUTEFORCE_HAMMING"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/examples/yaml/BRUTEFORCE_HAMMING_2.yaml b/src/examples/yaml/BRUTEFORCE_HAMMING_2.yaml
index fb95ed6..e43ae7a 100644
--- a/src/examples/yaml/BRUTEFORCE_HAMMING_2.yaml
+++ b/src/examples/yaml/BRUTEFORCE_HAMMING_2.yaml
@@ -4,3 +4,6 @@ sensor:
 matcher:
   type: "BRUTEFORCE_HAMMING_2"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/examples/yaml/BRUTEFORCE_L1.yaml b/src/examples/yaml/BRUTEFORCE_L1.yaml
index 73ebe11..e9bd4af 100644
--- a/src/examples/yaml/BRUTEFORCE_L1.yaml
+++ b/src/examples/yaml/BRUTEFORCE_L1.yaml
@@ -4,3 +4,6 @@ sensor:
 matcher:
   type: "BRUTEFORCE_L1"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/examples/yaml/FLANNBASED.yaml b/src/examples/yaml/FLANNBASED.yaml
index 67900be..e7d046a 100644
--- a/src/examples/yaml/FLANNBASED.yaml
+++ b/src/examples/yaml/FLANNBASED.yaml
@@ -3,4 +3,7 @@ sensor:
   
 matcher:
   type: "FLANNBASED"
-  match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
\ No newline at end of file
+  match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/examples/yaml/TRACKFEATURES.yaml b/src/examples/yaml/TRACKFEATURES.yaml
index 378bf04..bee58a1 100644
--- a/src/examples/yaml/TRACKFEATURES.yaml
+++ b/src/examples/yaml/TRACKFEATURES.yaml
@@ -24,10 +24,16 @@ matcher:
   type: "FLANNBASED"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
   min normalized score: 0.85  
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
 
 algorithm:
   type: "TRACKFEATURES"
   draw results: true
   min features to track: 10
   max new features: 100
-  min response new features: 80
\ No newline at end of file
+  min response new features: 80
+  target roi:
+    width: 20
+    height: 20
\ No newline at end of file
diff --git a/src/matchers/bruteforce/matcher_bruteforce_load_yaml.cpp b/src/matchers/bruteforce/matcher_bruteforce_load_yaml.cpp
index 3dd4bd1..7142209 100644
--- a/src/matchers/bruteforce/matcher_bruteforce_load_yaml.cpp
+++ b/src/matchers/bruteforce/matcher_bruteforce_load_yaml.cpp
@@ -27,6 +27,11 @@ static ParamsBasePtr createParamsBRUTEFORCEMatcher(const std::string & _filename
                 params_ptr->match_type  = d_yaml["match type"].as<int>();
             if (d_yaml["min normalized score"])
                 params_ptr->min_norm_score = d_yaml["min normalized score"].as<double>();
+            Node filters = d_yaml["filtering"];
+            if (filters["ransac epipolar distance"])
+                params_ptr->ransac_epipolar_distance = filters["ransac epipolar distance"].as<double>();
+            if (filters["ransac confidence prob"])
+                params_ptr->ransac_confidence_prob = filters["ransac confidence prob"].as<double>();
         }else
         {
             std::cerr << "Bad configuration file. Wrong type " << d_yaml["type"].as<string>() << std::endl;
diff --git a/src/matchers/matcher_base.cpp b/src/matchers/matcher_base.cpp
index 0edd244..f7eafd1 100644
--- a/src/matchers/matcher_base.cpp
+++ b/src/matchers/matcher_base.cpp
@@ -180,22 +180,15 @@ void MatcherBase::filterByDistance(const int& _max_pixel_dist, const float& _img
     	std::cerr << "[" << name_ << "]: Wrong input type in filterByDistance method." << std::endl;
 }
 
-void MatcherBase::ransacTest(const DMatchVector& _raw_matches,
-                             const KeyPointVector& _raw_kps1,
+void MatcherBase::ransacTest(const KeyPointVector& _raw_kps1,
                              const KeyPointVector& _raw_kps2,
-                             DMatchVector& _inlier_matches,
+                             const DMatchVector& _raw_matches,
                              KeyPointVector& _inlier_kps1,
-                             KeyPointVector& _inlier_kps2)
+                             KeyPointVector& _inlier_kps2,
+                             DMatchVector& _inlier_matches)
 {
-    // To be set outside
-    Scalar distance = 3.0;// distance to epipolar line
-    Scalar confidence = 0.99; // confidence probability
-
-
     // Convert keypoints into Point2f
     PointVector raw_pts1, raw_pts2;
-
-    std::vector<cv::Point2f> raw_pts1, raw_pts2;
     for (std::vector<cv::DMatch>::const_iterator it= _raw_matches.begin(); it!= _raw_matches.end(); ++it)
     {
         raw_pts1.push_back(cv::Point2f(_raw_kps1[it->queryIdx].pt.x, _raw_kps1[it->queryIdx].pt.y));
@@ -205,11 +198,11 @@ void MatcherBase::ransacTest(const DMatchVector& _raw_matches,
     // Compute F matrix using RANSAC
     std::vector<uchar> inliers(raw_pts1.size(),0);
     cv::Mat fundemental= cv::findFundamentalMat(raw_pts1,
-                                                raw_pts2, // matching points
-                                                inliers,      // match status (inlier ou outlier)
-                                                CV_FM_RANSAC, // RANSAC method
-                                                distance,     // distance to epipolar line
-                                                confidence);  // confidence probability
+                                                raw_pts2,                                   // matching points
+                                                inliers,                                    // match status (inlier ou outlier)
+                                                CV_FM_RANSAC,                               // RANSAC method
+                                                params_base_ptr_->ransac_epipolar_distance, // distance to epipolar line
+                                                params_base_ptr_->ransac_confidence_prob);  // confidence probability
 
     // extract the surviving (inliers) matches
     std::vector<uchar>::const_iterator itIn = inliers.begin();
@@ -224,8 +217,6 @@ void MatcherBase::ransacTest(const DMatchVector& _raw_matches,
             _inlier_kps2.push_back(_raw_kps1[itM->trainIdx]);
         }
     }
-
-    std::cout << "Number of matched points (after cleaning): " << _inlier_matches.size() << std::endl;
 }
 
 
diff --git a/src/matchers/matcher_base.h b/src/matchers/matcher_base.h
index 39c7350..73a05b6 100644
--- a/src/matchers/matcher_base.h
+++ b/src/matchers/matcher_base.h
@@ -36,8 +36,11 @@ 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
+
         MatcherParamsBase(const std::string& _type): type(_type){}
         virtual ~MatcherParamsBase(){}
 };
@@ -88,12 +91,12 @@ class MatcherBase : public VUBase, public std::enable_shared_from_this<MatcherBa
                      cv::DMatch& _match);
 
         // Identify good matches using RANSAC
-        void ransacTest(const DMatchVector& _raw_matches,
-                        const KeyPointVector& _raw_kps1,
+        void ransacTest(const KeyPointVector& _raw_kps1,
                         const KeyPointVector& _raw_kps2,
-                        DMatchVector& _inlier_matches,
+                        const DMatchVector& _raw_matches,
                         KeyPointVector& _inlier_kps1,
-                        KeyPointVector& _inlier_kps2);
+                        KeyPointVector& _inlier_kps2,
+                        DMatchVector& _inlier_matches);
 
         void filterByDistance(const int& _max_pixel_dist,
                               const float& _percentage,
diff --git a/src/test/data/ACTIVESEARCH.yaml b/src/test/data/ACTIVESEARCH.yaml
index 04333b0..9fab948 100644
--- a/src/test/data/ACTIVESEARCH.yaml
+++ b/src/test/data/ACTIVESEARCH.yaml
@@ -27,6 +27,9 @@ matcher:
   type: "FLANNBASED"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
   min normalized score: 0.85 
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
       
 algorithm:
   type: "ACTIVESEARCH"   
diff --git a/src/test/data/BRUTEFORCE.yaml b/src/test/data/BRUTEFORCE.yaml
index 3af86a3..4ee5f19 100644
--- a/src/test/data/BRUTEFORCE.yaml
+++ b/src/test/data/BRUTEFORCE.yaml
@@ -3,3 +3,6 @@ sensor:
 matcher:
   type: "BRUTEFORCE"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/test/data/BRUTEFORCE_HAMMING.yaml b/src/test/data/BRUTEFORCE_HAMMING.yaml
index 081dd31..248752a 100644
--- a/src/test/data/BRUTEFORCE_HAMMING.yaml
+++ b/src/test/data/BRUTEFORCE_HAMMING.yaml
@@ -3,3 +3,6 @@ sensor:
 matcher:
   type: "BRUTEFORCE_HAMMING"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/test/data/BRUTEFORCE_HAMMING_2.yaml b/src/test/data/BRUTEFORCE_HAMMING_2.yaml
index 0da3073..d415f81 100644
--- a/src/test/data/BRUTEFORCE_HAMMING_2.yaml
+++ b/src/test/data/BRUTEFORCE_HAMMING_2.yaml
@@ -3,3 +3,6 @@ sensor:
 matcher:
   type: "BRUTEFORCE_HAMMING_2"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/test/data/BRUTEFORCE_L1.yaml b/src/test/data/BRUTEFORCE_L1.yaml
index 128fcf4..23d7c08 100644
--- a/src/test/data/BRUTEFORCE_L1.yaml
+++ b/src/test/data/BRUTEFORCE_L1.yaml
@@ -2,4 +2,7 @@ sensor:
   type: "USB_CAM"
 matcher:
   type: "BRUTEFORCE_L1"
-  match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3 
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/test/data/FLANNBASED.yaml b/src/test/data/FLANNBASED.yaml
index 67900be..e7d046a 100644
--- a/src/test/data/FLANNBASED.yaml
+++ b/src/test/data/FLANNBASED.yaml
@@ -3,4 +3,7 @@ sensor:
   
 matcher:
   type: "FLANNBASED"
-  match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
\ No newline at end of file
+  match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
\ No newline at end of file
diff --git a/src/test/data/TRACKFEATURES.yaml b/src/test/data/TRACKFEATURES.yaml
index 43d5a68..38150db 100644
--- a/src/test/data/TRACKFEATURES.yaml
+++ b/src/test/data/TRACKFEATURES.yaml
@@ -24,10 +24,16 @@ matcher:
   type: "FLANNBASED"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
   min normalized score: 0.85     
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
       
 algorithm:
   type: "TRACKFEATURES"
   draw results: true
   min features to track: 10
   max new features: 100
-  min response new features: 80
\ No newline at end of file
+  min response new features: 80
+  target roi:
+    width: 20
+    height: 20  
\ No newline at end of file
diff --git a/src/test/data/base_classes.yaml b/src/test/data/base_classes.yaml
index 9f8fabd..5fedf9f 100644
--- a/src/test/data/base_classes.yaml
+++ b/src/test/data/base_classes.yaml
@@ -27,6 +27,9 @@ matcher:
   type: "BRUTEFORCE"
   match type: 1     #  Match type. MATCH = 1, KNNMATCH = 2, RADIUSMATCH = 3
   min normalized score: 0.85
+  filtering:
+    ransac epipolar distance: 3  # Distance to epipolar [pixels]
+    ransac confidence prob: 0.97 # Confidence probability
       
 algorithm:
   type: "OPTFLOWPYRLK" 
diff --git a/src/test/gtest_matcher_base.cpp b/src/test/gtest_matcher_base.cpp
index 72329fd..f630872 100644
--- a/src/test/gtest_matcher_base.cpp
+++ b/src/test/gtest_matcher_base.cpp
@@ -48,6 +48,60 @@ TEST(MatcherBase, getParams)
     ASSERT_EQ(params->match_type,1);
 }
 
+TEST(MatcherBase, ransacTest)
+{
+    cv::Mat image1 = cv::imread(filename1, CV_LOAD_IMAGE_GRAYSCALE);
+    ASSERT_TRUE(image1.data)<< "Failed to load image " << filename1 << std::endl;
+    cv::Mat image2 = cv::imread(filename2, CV_LOAD_IMAGE_GRAYSCALE);
+    ASSERT_TRUE(image1.data)<< "Failed to load image " << filename2 << std::endl;
+
+    // Define detector
+    std::string det_name = vision_utils::readYamlType(yaml_file_params, "detector");
+    ASSERT_TRUE(det_name.compare("ORB")==0);
+    vision_utils::DetectorBasePtr det_ptr = vision_utils::setupDetector(det_name, det_name + " detector", yaml_file_params);
+    det_ptr = std::static_pointer_cast<vision_utils::DetectorORB>(det_ptr);
+    ASSERT_TRUE(det_ptr!=NULL);
+
+    // Define descriptor
+    std::string des_name = vision_utils::readYamlType(yaml_file_params, "descriptor");
+    ASSERT_TRUE(des_name.compare("ORB")==0);
+    vision_utils::DescriptorBasePtr des_ptr = vision_utils::setupDescriptor(des_name, des_name + " descriptor", yaml_file_params);
+    des_ptr = std::static_pointer_cast<vision_utils::DescriptorORB>(des_ptr);
+    ASSERT_TRUE(des_ptr!=NULL);
+
+    // Image 1
+    KeyPointVector kpts1 = det_ptr->detect(image1);
+    cv::Mat desc1 = des_ptr->getDescriptor(image1,kpts1);
+    ASSERT_TRUE(kpts1.size() == desc1.rows);
+
+    // Image 2
+    KeyPointVector kpts2 = det_ptr->detect(image2);
+    cv::Mat desc2 = des_ptr->getDescriptor(image2,kpts2);
+    ASSERT_TRUE(kpts2.size() == desc2.rows);
+
+    // Define matcher
+    vision_utils::MatcherParamsBasePtr params_ptr = std::make_shared<vision_utils::MatcherParamsBase>(matcher_name);
+    vision_utils::MatcherBasePtr mat_ptr;
+
+    // MATCH type
+    params_ptr->match_type = vision_utils::MATCH;
+    mat_ptr = vision_utils::setupMatcher(matcher_name, matcher_name, params_ptr);
+    params_ptr = mat_ptr->getParams();
+    ASSERT_EQ(params_ptr->match_type,1);
+
+    // match
+    DMatchVector matches;
+    mat_ptr->match(desc1,desc2,des_ptr->getSize(),matches);
+    ASSERT_TRUE(matches.size()-75>0);
+
+    KeyPointVector inlier_kpts1;
+    KeyPointVector inlier_kpts2;
+    DMatchVector inlier_matches;
+    mat_ptr->ransacTest(kpts1, kpts2, matches,
+                        inlier_kpts1, inlier_kpts2, inlier_matches);
+    ASSERT_TRUE(inlier_matches.size()<23);
+}
+
 int main(int argc, char **argv)
 {
   testing::InitGoogleTest(&argc, argv);
-- 
GitLab