ekf_cal  0.4.0
A Kalman filter-based sensor calibration package
feature_tracker.hpp
1 // Copyright 2023 Jacob Hartzer
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
16 #ifndef TRACKERS__FEATURE_TRACKER_HPP_
17 #define TRACKERS__FEATURE_TRACKER_HPP_
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <opencv2/features2d.hpp>
25 #include <opencv2/opencv.hpp>
26 
27 #include "ekf/ekf.hpp"
28 #include "ekf/types.hpp"
29 #include "ekf/update/msckf_updater.hpp"
30 #include "infrastructure/debug_logger.hpp"
31 #include "trackers/tracker.hpp"
32 
36 enum class Detector
37 {
38  BRISK,
39  FAST,
40  GFTT,
41  MSER,
42  ORB,
43  SIFT,
44 };
45 
49 enum class Descriptor
50 {
51  ORB,
52  SIFT
53 };
54 
58 enum class Matcher
59 {
60  BRUTE_FORCE,
61  FLANN
62 };
63 
68 class FeatureTracker : public Tracker
69 {
70 public:
74  typedef struct Parameters : public Tracker::Parameters
75  {
76  Detector detector {Detector::FAST};
77  Descriptor descriptor {Descriptor::ORB};
78  Matcher matcher {Matcher::FLANN};
79  int threshold {20};
80  double px_error{1e-9};
81  double min_feat_dist {1.0};
82  bool down_sample {false};
83  int down_sample_height {480};
84  int down_sample_width {640};
85  bool is_cam_extrinsic{false};
87 
93 
101  static std::vector<cv::KeyPoint> GridFeatures(
102  std::vector<cv::KeyPoint> & key_points,
103  int rows,
104  int cols
105  );
106 
114  void Track(
115  double time,
116  unsigned int frame_id,
117  const cv::Mat & img_in,
118  cv::Mat & img_out
119  );
120 
125  void RatioTest(std::vector<std::vector<cv::DMatch>> & matches) const;
126 
133  static void SymmetryTest(
134  std::vector<std::vector<cv::DMatch>> & matches_forward,
135  std::vector<std::vector<cv::DMatch>> & matches_backward,
136  std::vector<cv::DMatch> & matches_out
137  );
138 
145  void RANSAC(
146  std::vector<cv::DMatch> & matches_in,
147  std::vector<cv::KeyPoint> & curr_key_points,
148  std::vector<cv::DMatch> & matches_out
149  ) const;
150 
157  void DistanceTest(
158  std::vector<cv::DMatch> & matches_in,
159  std::vector<cv::KeyPoint> & curr_key_points,
160  std::vector<cv::DMatch> & matches_out
161  ) const;
162 
163 protected:
165 
166 private:
167  static cv::Ptr<cv::FeatureDetector> InitFeatureDetector(
168  Detector detector,
169  int threshold);
170  static cv::Ptr<cv::DescriptorExtractor> InitDescriptorExtractor(
171  Descriptor extractor,
172  int threshold);
173  static cv::Ptr<cv::DescriptorMatcher> InitDescriptorMatcher(Matcher matcher);
174 
175  cv::Ptr<cv::FeatureDetector> m_feature_detector;
176  cv::Ptr<cv::DescriptorExtractor> m_descriptor_extractor;
177  cv::Ptr<cv::DescriptorMatcher> m_descriptor_matcher;
178 
179  unsigned int m_prev_frame_id;
180  double m_prev_frame_time;
181  cv::Mat m_prev_descriptors;
182  std::vector<cv::KeyPoint> m_prev_key_points;
183 
184  std::map<unsigned int, std::vector<FeaturePoint>> m_feature_points_map;
185 
186  static int GenerateFeatureID();
187 
188  double m_px_error;
189  bool m_down_sample;
190  int m_down_sample_height;
191  int m_down_sample_width;
192  double m_knn_ratio{0.7};
193 };
194 
195 #endif // TRACKERS__FEATURE_TRACKER_HPP_
FeatureTracker Class.
Definition: feature_tracker.hpp:69
static std::vector< cv::KeyPoint > GridFeatures(std::vector< cv::KeyPoint > &key_points, int rows, int cols)
Down sample features to grid.
Definition: feature_tracker.cpp:120
void DistanceTest(std::vector< cv::DMatch > &matches_in, std::vector< cv::KeyPoint > &curr_key_points, std::vector< cv::DMatch > &matches_out) const
Perform distance test given matches and key points.
Definition: feature_tracker.cpp:352
void RatioTest(std::vector< std::vector< cv::DMatch >> &matches) const
Perform ratio test on a set of matches.
Definition: feature_tracker.cpp:279
MsckfUpdater m_msckf_updater
MSCKF updater object.
Definition: feature_tracker.hpp:164
void RANSAC(std::vector< cv::DMatch > &matches_in, std::vector< cv::KeyPoint > &curr_key_points, std::vector< cv::DMatch > &matches_out) const
Perform RANSAC filtering test given matches and key points.
Definition: feature_tracker.cpp:318
FeatureTracker::Parameters Parameters
Feature Tracker Initialization parameters structure.
static void SymmetryTest(std::vector< std::vector< cv::DMatch >> &matches_forward, std::vector< std::vector< cv::DMatch >> &matches_backward, std::vector< cv::DMatch > &matches_out)
Perform symmetry test given forward and backward matches.
Definition: feature_tracker.cpp:292
void Track(double time, unsigned int frame_id, const cv::Mat &img_in, cv::Mat &img_out)
Perform track on new image frame.
Definition: feature_tracker.cpp:166
FeatureTracker(FeatureTracker::Parameters params)
FeatureTracker sensor constructor.
Definition: feature_tracker.cpp:35
EKF Updater Class for MSCKF Camera Measurements.
Definition: msckf_updater.hpp:34
Tracker Class.
Definition: tracker.hpp:31
Feature Tracker Initialization parameters structure.
Definition: feature_tracker.hpp:75
bool is_cam_extrinsic
Flag for extrinsic camera calibration.
Definition: feature_tracker.hpp:85
int down_sample_height
Down-sampled height to use for tracking.
Definition: feature_tracker.hpp:83
Descriptor descriptor
Descriptor.
Definition: feature_tracker.hpp:77
double px_error
Pixel error standard deviation.
Definition: feature_tracker.hpp:80
bool down_sample
Flag to perform down-sampling.
Definition: feature_tracker.hpp:82
double min_feat_dist
Minimum feature distance to consider.
Definition: feature_tracker.hpp:81
int down_sample_width
Down-sampled width to use for tracking.
Definition: feature_tracker.hpp:84
Detector detector
Detector.
Definition: feature_tracker.hpp:76
Matcher matcher
Matcher.
Definition: feature_tracker.hpp:78
int threshold
Threshold.
Definition: feature_tracker.hpp:79
Tracker Initialization parameters structure.
Definition: tracker.hpp:37