MTF
FeatureTracker.h
1 #ifndef MTF_FEATURE_TRACKER_H
2 #define MTF_FEATURE_TRACKER_H
3 
4 #include "FeatureBase.h"
5 
6 #ifndef DISABLE_FLANN
7 #include "FLANNParams.h"
8 #endif
9 #include "FLANNCVParams.h"
10 
11 #if CV_MAJOR_VERSION < 3
12 #include "opencv2/features2d/features2d.hpp"
13 #else
14 #include "opencv2/features2d.hpp"
15 #endif
16 
17 #include <memory>
18 #include <vector>
19 #include <boost/any.hpp>
20 
21 _MTF_BEGIN_NAMESPACE
22 
23 #ifndef FEAT_DISABLE_NONFREE
24 struct SIFT{
25  int n_features;
26  int n_octave_layers;
27  double contrast_thresh;
28  double edge_thresh;
29  double sigma;
30  SIFT(const vector<boost::any> &params,
31  std::string _type = "detector");
32  void create(cv::Ptr<cv::FeatureDetector> &ptr);
33 #if CV_MAJOR_VERSION < 3
34  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
35 #endif
36 };
37 struct SURF{
38  double hessian_threshold;
39  int n_octaves;
40  int n_octave_layers;
41  bool extended;
42  bool upright;
43  SURF(const vector<boost::any> &params,
44  std::string _type = "detector");
45  void create(cv::Ptr<cv::FeatureDetector> &ptr);
46 #if CV_MAJOR_VERSION < 3
47  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
48 #endif
49 };
50 #endif
51 
52 struct BRISK{
53  int thresh;
54  int octaves;
55  float pattern_scale;
56  BRISK(const vector<boost::any> &params,
57  std::string _type = "detector");
58  void create(cv::Ptr<cv::FeatureDetector> &ptr);
59 #if CV_MAJOR_VERSION < 3
60  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
61 #endif
62 };
63 struct ORB{
64  int n_features;
65  float scale_factor;
66  int n_levels;
67  int edge_threshold;
68  int first_level;
69  int WTA_K;
70  int score_type;
71  int patch_size;
72  int fast_threshold;
73  ORB(const vector<boost::any> &params,
74  std::string _type = "detector");
75  void create(cv::Ptr<cv::FeatureDetector> &ptr);
76 #if CV_MAJOR_VERSION < 3
77  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
78 #endif
79 };
80 struct FAST{
81  int threshold;
82  bool non_max_suppression;
83  int type;
84  FAST(const vector<boost::any> &params);
85  void create(cv::Ptr<cv::FeatureDetector> &ptr);
86 };
87 struct MSER{
88  int delta;
89  int min_area;
90  int max_area;
91  double max_variation;
92  double min_diversity;
93  int max_evolution;
94  double area_threshold;
95  double min_margin;
96  int edge_blur_size;
97  MSER(const vector<boost::any> &params);
98  void create(cv::Ptr<cv::FeatureDetector> &ptr);
99 };
100 struct GFTT{
101  int max_corners;
102  double quality_level;
103  double min_distance;
104  int block_size;
105  bool use_harris_detector;
106  double k;
107  GFTT(const vector<boost::any> &params);
108  void create(cv::Ptr<cv::FeatureDetector> &ptr);
109 };
110 #if CV_MAJOR_VERSION >= 3
111 struct AGAST{
112  int threshold;
113  bool non_max_suppression;
114  int type;
115  AGAST(const vector<boost::any> &params);
116  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
117 };
118 #ifndef FEAT_DISABLE_NONFREE
119 struct BRIEF{
121  int bytes = 32;
122  bool use_orientation = false;
123  BRIEF(const vector<boost::any> &params);
124  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
125 };
126 struct FREAK{
127  bool orientation_normalized;
128  bool scale_normalized;
129  float pattern_scale;
130  int n_octaves;
131  FREAK(const vector<boost::any> &params);
132  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
133 };
134 struct LUCID{
135  int kernel;
136  int blur_kernel;
137  LUCID(const vector<boost::any> &params);
138  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
139 };
140 struct LATCH{
141  int bytes;
142  bool rotation_invariance;
143  int half_ssd_size;
144  LATCH(const vector<boost::any> &params);
145  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
146 };
147 struct DAISY{
148  typedef vector<float> vectorf;
149  float radius;
150  int q_radius;
151  int q_theta;
152  int q_hist;
153  int norm;
154  vectorf H;
155  bool interpolation;
156  bool use_orientation;
157  DAISY(const vector<boost::any> &params);
158  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
159 };
160 struct VGG{
161  int desc;
162  float isigma;
163  bool img_normalize;
164  bool use_scale_orientation;
165  float scale_factor;
166  bool dsc_normalize;
167  VGG(const vector<boost::any> &params);
168  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
169 };
170 struct BoostDesc{
171  int desc;
172  bool use_scale_orientation;
173  float scale_factor;
174  BoostDesc(const vector<boost::any> &params);
175  void create(cv::Ptr<cv::DescriptorExtractor> &ptr);
176 };
178 struct Star{
179  int max_size;
180  int response_threshold;
181  int line_threshold_projected;
182  int line_threshold_binarized;
183  int suppress_nonmax_size;
184  Star(const vector<boost::any> &params);
185  void create(cv::Ptr<cv::FeatureDetector> &ptr);
186 };
187 struct MSD{
188  int patch_radius = 3;
189  int search_area_radius = 5;
190  int nms_radius = 5;
191  int nms_scale_radius = 0;
192  float th_saliency = 250.0f;
193  int kNN = 4;
194  float scale_factor = 1.25f;
195  int n_scales = -1;
196  bool compute_orientation = false;
197  MSD(const vector<boost::any> &params);
198  void create(cv::Ptr<cv::FeatureDetector> &ptr);
199 };
200 #endif
201 #endif
203  enum class DetectorType {
204  NONE = -1,
206  ORB, BRISK,
208  SIFT, SURF,
210  FAST, MSER, GFTT,
212  AGAST,
214  Star, MSD
215  };
216  enum class DescriptorType{
218  ORB, BRISK,
220  SIFT, SURF,
222  BRIEF, FREAK, LUCID, LATCH, DAISY,
223  VGG, BoostDesc
224  };
225 
226  typedef std::vector<boost::any> DetectorParamsType;
227  typedef std::vector<boost::any> DescriptorParamsType;
228 
229  DetectorType detector_type;
230  DescriptorType descriptor_type;
231 
232  DetectorParamsType detector;
233  DescriptorParamsType descriptor;
234 
235  int grid_size_x, grid_size_y;
236  int search_window_x, search_window_y;
237  bool init_at_each_frame;
238 
239  bool rebuild_index;
240  double max_dist_ratio;
241 
242  int min_matches;
243 
246  double epsilon;
247  bool enable_pyr;
248 
249  bool use_cv_flann;
250 
251  bool uchar_input;
252 
257 
258  bool debug_mode;
259 
261  DetectorType _detector_type,
262  DescriptorType _descriptor_type,
263  const DetectorParamsType &_detector,
264  const DescriptorParamsType &_descriptor,
265  int _grid_size_x, int _grid_size_y,
266  int _search_win_x, int _search_win_y,
267  bool _init_at_each_frame, bool _rebuild_index,
268  int _max_iters, double _epsilon, bool _enable_pyr,
269  bool _use_cv_flann,
270  double _max_dist_ratio, int _min_matches, bool _uchar_input,
271  bool _show_keypoints, bool _show_matches, bool _debug_mode);
272  FeatureTrackerParams(const FeatureTrackerParams *params = nullptr);
273 
274  int getResX() const{ return grid_size_x; }
275  int getResY() const{ return grid_size_y; }
276 
277  std::string toString(DetectorType _detector_type);
278  std::string toString(DescriptorType _descriptor_type);
279 };
280 template<class SSM>
282 
283 public:
287  typedef typename SSM::ParamType SSMParams;
288  typedef typename SSM::EstimatorParams EstimatorParams;
289 #ifndef DISABLE_FLANN
290  typedef FLANNParams::IdxType IdxType;
291  typedef FLANNParams::SearchType SearchType;
292  typedef flann::Index<flann::L2<float> > flannIdxT;
293  typedef flann::Matrix<float> flannMatT;
294  typedef flann::Matrix<int> flannResultT;
295  typedef std::shared_ptr<flannMatT> FlannMatPtr;
296  typedef std::shared_ptr<flannIdxT> FlannIdxPtr;
297 #endif
298 
299  typedef cv::Ptr<cv::FeatureDetector> DetectorPtr;
300  typedef cv::Ptr<cv::DescriptorExtractor> DescriptorPtr;
301 
303  const ParamType *grid_params = nullptr,
304 #ifndef DISABLE_FLANN
305  const FLANNParams *_flann_params = nullptr,
306 #endif
307  const FLANNCVParams *_flanncv_params = nullptr,
308  const EstimatorParams *_est_params = nullptr,
309  const SSMParams *ssm_params = nullptr
310  );
311  ~FeatureTracker(){}
312 
313  void initialize(const cv::Mat &corners) override;
314  void update() override;
315  void setImage(const cv::Mat &img) override;
316  void setRegion(const cv::Mat& corners) override;
317  const uchar* getPixMask() override{ return pix_mask.data(); }
318  int getResX() override{ return params.grid_size_x; }
319  int getResY() override{ return params.grid_size_y; }
320  const cv::Mat& getRegion() override{ return cv_corners_mat; }
321  int inputType() const override{
322  return CV_8UC3;
323  //return params.descriptor_type==DescriptorType::LUCID ? CV_8UC3:
324  // params.uchar_input ? CV_8UC1 : CV_32FC1;
325  }
326  bool detect(const cv::Mat &mask, cv::Mat &obj_location) override;
327 
328  using FeatureBase::initialize;
329  using FeatureBase::update;
330  using FeatureBase::setRegion;
331 
332  SSM& getSSM() { return ssm; }
333 
334  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
335 
336 private:
337  SSM ssm;
338  ParamType params;
339 #ifndef DISABLE_FLANN
340  FLANNParams flann_params;
341 #endif
342  FLANNCVParams flanncv_params;
343  EstimatorParams est_params;
344  cv::Ptr<cv::FlannBasedMatcher> matcher;
345  DetectorPtr detector;
346  DescriptorPtr descriptor;
347 
348 #ifndef DISABLE_FLANN
349  FlannIdxPtr flann_idx;
350  FlannMatPtr flann_dataset, flann_query;
351 #endif
352  cv::Mat curr_img_in, curr_img, prev_img;
353  std::vector<cv::KeyPoint> curr_key_pts, prev_key_pts;
354  std::vector<cv::Point2f> curr_pts, prev_pts;
355  cv::Mat best_idices, best_distances;
356 
357  int n_pts, n_key_pts, n_good_key_pts;
358  cv::Size search_window;
359  cv::Mat curr_descriptors, prev_descriptors;
360  std::vector<uchar> pix_mask;
361  std::vector<int> good_indices;
362 
363  CornersT opt_warped_corners;
364  VectorXd ssm_update;
365 
366  cv::Mat curr_img_disp;
367 
368  char* patch_win_name;
369 
370  MatrixXi _linear_idx;
371  int pause_seq;
372  bool use_feature_detector;
373 
374  void matchKeyPoints();
375  void cmptWarpedCorners();
376  void showKeyPoints();
377 };
378 
379 _MTF_END_NAMESPACE
380 
381 #endif
382 
bool show_keypoints
show the locations of individual key points
Definition: FeatureTracker.h:254
Definition: StateSpaceModel.h:35
Definition: FeatureBase.h:9
Definition: FeatureTracker.h:52
Definition: FeatureTracker.h:24
Definition: FeatureTracker.h:80
Definition: FeatureTracker.h:63
index specific params
Definition: FLANNParams.h:10
index specific params
Definition: FLANNCVParams.h:10
DescriptorType
Definition: FeatureTracker.h:216
Definition: FeatureTracker.h:87
Definition: FeatureTracker.h:281
Definition: FeatureTracker.h:202
Definition: FeatureTracker.h:37
DetectorType
Definition: FeatureTracker.h:203
Definition: FeatureTracker.h:100
bool show_matches
show the matches between keypoints
Definition: FeatureTracker.h:256
int max_iters
maximum iterations of the GridTracker algorithm to run for each frame
Definition: FeatureTracker.h:245