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  double threshold {20.0};
80  double px_error{1e-9};
81  double min_feat_dist {1.0};
82  bool down_sample {false};
83  double down_sample_height {480.0};
84  double down_sample_width {640.0};
85  bool is_cam_extrinsic{false};
87 
93 
101  std::vector<cv::KeyPoint> GridFeatures(
102  std::vector<cv::KeyPoint> key_points,
103  unsigned int rows,
104  unsigned int cols);
105 
113  void Track(double time, int frame_id, const cv::Mat & img_in, cv::Mat & img_out);
114 
115 
120  void RatioTest(std::vector<std::vector<cv::DMatch>> & matches);
121 
128  void SymmetryTest(
129  std::vector<std::vector<cv::DMatch>> & matches_forward,
130  std::vector<std::vector<cv::DMatch>> & matches_backward,
131  std::vector<cv::DMatch> & matches_out);
132 
139  void RANSAC(
140  std::vector<cv::DMatch> & matches_in,
141  std::vector<cv::KeyPoint> & curr_key_points,
142  std::vector<cv::DMatch> & matches_out
143  );
144 
151  void DistanceTest(
152  std::vector<cv::DMatch> & matches_in,
153  std::vector<cv::KeyPoint> & curr_key_points,
154  std::vector<cv::DMatch> & matches_out
155  );
156 
157 protected:
159 
160 private:
161  cv::Ptr<cv::FeatureDetector> InitFeatureDetector(Detector detector, double threshold);
162  cv::Ptr<cv::DescriptorExtractor> InitDescriptorExtractor(Descriptor extractor, double threshold);
163  cv::Ptr<cv::DescriptorMatcher> InitDescriptorMatcher(Matcher matcher);
164 
165  cv::Ptr<cv::FeatureDetector> m_feature_detector;
166  cv::Ptr<cv::DescriptorExtractor> m_descriptor_extractor;
167  cv::Ptr<cv::DescriptorMatcher> m_descriptor_matcher;
168 
169  int m_prev_frame_id;
170  double m_prev_frame_time;
171  cv::Mat m_prev_descriptors;
172  std::vector<cv::KeyPoint> m_prev_key_points;
173 
174  std::map<unsigned int, std::vector<FeaturePoint>> m_feature_points_map;
175 
176  unsigned int GenerateFeatureID();
177 
178  double m_px_error;
179  bool m_down_sample;
180  double m_down_sample_height;
181  double m_down_sample_width;
182  double m_knn_ratio{0.7};
183 };
184 
185 #endif // TRACKERS__FEATURE_TRACKER_HPP_
FeatureTracker Class.
Definition: feature_tracker.hpp:69
void RANSAC(std::vector< cv::DMatch > &matches_in, std::vector< cv::KeyPoint > &curr_key_points, std::vector< cv::DMatch > &matches_out)
Perform RANSAC filtering test given matches and key points.
Definition: feature_tracker.cpp:309
void DistanceTest(std::vector< cv::DMatch > &matches_in, std::vector< cv::KeyPoint > &curr_key_points, std::vector< cv::DMatch > &matches_out)
Perform distance test given matches and key points.
Definition: feature_tracker.cpp:340
MsckfUpdater m_msckf_updater
MSCKF updater object.
Definition: feature_tracker.hpp:158
std::vector< cv::KeyPoint > GridFeatures(std::vector< cv::KeyPoint > key_points, unsigned int rows, unsigned int cols)
Down sample features to grid.
Definition: feature_tracker.cpp:124
FeatureTracker::Parameters Parameters
Feature Tracker Initialization parameters structure.
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:284
void Track(double time, int frame_id, const cv::Mat &img_in, cv::Mat &img_out)
Perform track on new image frame.
Definition: feature_tracker.cpp:170
void RatioTest(std::vector< std::vector< cv::DMatch >> &matches)
Perform ratio test on a set of matches.
Definition: feature_tracker.cpp:274
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
double 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
double down_sample_width
Down-sampled width to use for tracking.
Definition: feature_tracker.hpp:84
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
double threshold
Threshold.
Definition: feature_tracker.hpp:79
Detector detector
Detector.
Definition: feature_tracker.hpp:76
Matcher matcher
Matcher.
Definition: feature_tracker.hpp:78
Tracker Initialization parameters structure.
Definition: tracker.hpp:37