MRPT  1.9.9
tracking.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef mrpt_vision_tracking_H
11 #define mrpt_vision_tracking_H
12 
13 #include <mrpt/vision/types.h>
14 
15 #include <mrpt/vision/CFeature.h>
17 #include <mrpt/img/CImage.h>
20 #include <memory> // for unique_ptr
21 
22 namespace mrpt::vision
23 {
24 /** \addtogroup vision_tracking Feature detection and tracking
25  * \ingroup mrpt_vision_grp
26  * @{ */
27 
28 /** A virtual interface for all feature trackers, implementing the part of
29  * feature tracking that is common to any specific tracker implementation.
30  * This class provides a quite robust tracking of features, avoiding as many
31  * outliers as possible but not all of them:
32  * more robust tracking would require application-specific information and
33  * could be done in a number of very different approaches,
34  * so this class will not try to do any kind of RANSAC or any other advanced
35  * outlier rejection; instead, it should
36  * be done by the users or the classes that employ this class.
37  *
38  * The basic usage of this class is as follows:
39  * \code
40  * CFeatureTracker_KL tracker; // Note: CFeatureTracker_KL is the
41  * most robust implementation for now.
42  * tracker.extra_params["add_new_features"] = 1; // Enable detection of
43  * new features, not only tracking
44  * tracker.extra_params[...] = ...
45  * // ....
46  * CFeatureList theFeats; // The list of features
47  * mrpt::img::CImage previous_img, current_img;
48  *
49  * while (true) {
50  * current_img = ... // Grab new image.
51  * if ( previous_img_is_ok )
52  * tracker.trackFeatures(previous_img, current_img, theFeats);
53  * previous_img = current_img;
54  * }
55  * \endcode
56  *
57  * Below follows the list of optional parameters for "extra_params" which can
58  * be set
59  * and will be understood by this base class for any specific tracker
60  * implementation.
61  * Note that all parameters are double's, but boolean flags are emulated by
62  * the values 0.0 (false) and 1.0 (true).
63  *
64  * List of parameters:
65  * <table border="1" >
66  * <tr><td align="center" > <b>Parameter name</b> </td> <td align="center"
67  * > <b>Default value</b> </td> <td align="center" > <b>Comments</b> </td> </tr>
68  * <tr><td align="center" > add_new_features </td> <td align="center" > 0
69  * </td>
70  * <td> If set to "1", the class will not only track existing features,
71  * but will also perform (after doing the actual tracking) an efficient
72  * search for new features with the FAST detector, and will add them
73  * to the passed "CFeatureList" if they fulfill a set of restrictions,
74  * as stablished by the other parameters (see
75  * <i>add_new_feat_min_separation</i>,<i>add_new_feat_max_features</i>,<i>minimum_KLT_response_to_add</i>).
76  * </td> </tr>
77  * <tr><td align="center" > add_new_feat_min_separation </td> <td
78  * align="center" > 15 </td>
79  * <td> If <i>add_new_features</i>==1, this is the minimum separation (in
80  * pixels) to any other (old, or new) feature for it
81  * being considered a candidate to be added.
82  * </td> </tr>
83  * <tr><td align="center" > desired_num_features_adapt </td> <td
84  * align="center" > (img_width*img_height)/512 </td>
85  * <td> If <i>add_new_features</i>==1, the threshold of the FAST(ER)
86  * feature detector is dynamically adapted such as the number of
87  * raw FAST keypoints is around this number. This number should be much
88  * higher than the real desired numbre of features, since this
89  * one includes many features concentrated in space which are later
90  * discarded for the minimum distance.
91  * </td> </tr>
92  * <tr><td align="center" > desired_num_features </td> <td align="center" >
93  * 100 </td>
94  * <td> If <i>add_new_features</i>==1, the target number of the patch
95  * associated to each feature will be updated with every N'th frame. </td> </tr>
96  * <tr><td align="center" > add_new_feat_patch_size </td> <td
97  * align="center" > 11 </td>
98  * <td> If <i>add_new_features</i>==1, for each new added feature, this
99  * is the size of the patch to be extracted around the keypoint (set to 0 if
100  * patches are not required at all).
101  * </td> </tr>
102  * <tr><td align="center" > minimum_KLT_response_to_add </td> <td
103  * align="center" > 10 </td>
104  * <td> If <i>add_new_features</i>==1, this sets the minimum KLT response
105  * of candidate FAST features to be added in each frame, if they also fulfil the
106  * other restrictions (e.g. min.distance).
107  * </td> </tr>
108  * <tr><td align="center" > check_KLT_response_every </td> <td
109  * align="center" > 0 </td>
110  * <td> If >0, it will compute the KLT response at each feature point
111  * every <i>N</i> frames
112  * and those below <i>minimum_KLT_response</i> will be marked as
113  * "lost" in their "track_status" field.
114  * </td> </tr>
115  * <tr><td align="center" > minimum_KLT_response </td> <td align="center" >
116  * 5 </td>
117  * <td> See explanation of <i>check_KLT_response_every</i>.
118  * </td> </tr>
119  * <tr><td align="center" > KLT_response_half_win </td> <td align="center"
120  * > 4 </td>
121  * <td> When computing the KLT response of features (see
122  * <i>minimum_KLT_response</i> and <i>minimum_KLT_response_to_add</i>),
123  * the window centered at the point for its estimation will be of
124  * size (2*W+1)x(2*W+1), with <i>W</i> being this parameter value.
125  * </td> </tr>
126  * <tr><td align="center" > update_patches_every </td> <td align="center" >
127  * 0 </td>
128  * <td> If !=0, the patch associated to each feature will be updated with
129  * every N'th frame. </td> </tr>
130  * <tr><td align="center" > remove_lost_features </td> <td align="center" >
131  * 0 </td>
132  * <td> If !=0, out-of-bound features or those lost while tracking, will
133  * be automatically removed from the list of features.
134  * Otherwise, the user will have to manually remove them by checking
135  * the track_status field. </td> </tr>
136  * </table>
137  *
138  * This class also offers a time profiler, disabled by default (see
139  * getProfiler and enableTimeLogger).
140  *
141  * \sa CFeatureTracker_KL, the example application "track-video-features".
142  */
144 {
145  /** Optional list of extra parameters to the algorithm. */
147 
148  /** Default ctor */
150  : m_timlog(false),
154  {
155  }
156  /** Ctor with extra parameters */
158  : extra_params(extraParams),
159  m_timlog(false),
163  {
164  }
165  /** Dtor */
167  /** Perform feature tracking from "old_img" to "new_img", with a (possibly
168  *empty) list of previously tracked features "inout_featureList".
169  * This is a list of parameters (in "extraParams") accepted by ALL
170  *implementations of feature tracker (see each derived class for more
171  *specific parameters).
172  * - "add_new_features" (Default=0). If set to "1", new features will
173  *be
174  *also added to the existing ones in areas of the image poor of features.
175  * This method does:
176  * - Convert old and new images to grayscale, if they're in color.
177  * - Call the pure virtual "trackFeatures_impl" method.
178  * - Implement the optional detection of new features if
179  *"add_new_features"!=0.
180  */
181  void trackFeatures(
182  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
183  TSimpleFeatureList& inout_featureList);
184 
185  /** overload with subpixel precision */
186  void trackFeatures(
187  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
188  TSimpleFeaturefList& inout_featureList);
189 
190  /** overload This overload version uses the old (and much slower)
191  * CFeatureList */
192  void trackFeatures(
193  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
194  CFeatureList& inout_featureList);
195 
196  /** A wrapper around the basic trackFeatures() method, but keeping the
197  * original list of features unmodified and returns the tracked ones in a
198  * new list. */
199  inline void trackFeaturesNewList(
200  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
201  const vision::CFeatureList& in_featureList,
202  vision::CFeatureList& out_featureList)
203  {
204  out_featureList = in_featureList;
205  std::for_each(
206  out_featureList.begin(), out_featureList.end(), [](auto& ptr) {
207  ptr.reset(dynamic_cast<CFeature*>(ptr->clone()));
208  });
209  this->trackFeatures(old_img, new_img, out_featureList);
210  }
211 
212  /** Returns a read-only reference to the internal time logger */
214  {
215  return m_timlog;
216  }
217  /** Returns a reference to the internal time logger */
219  /** Returns a read-only reference to the internal time logger */
220  inline void enableTimeLogger(bool enable = true)
221  {
222  m_timlog.enable(enable);
223  }
224 
225  /** Returns the current adaptive threshold used by the FAST(ER) detector to
226  * find out new features in empty areas */
227  inline int getDetectorAdaptiveThreshold() const
228  {
230  }
231 
233  {
234  /** In the new_img with the last adaptive threshold */
236  /** The number of features which were deleted due to OOB, bad tracking,
237  * etc... (only if "remove_lost_features" is enabled) */
239  };
240 
241  /** Updated with each call to trackFeatures() */
243 
244  protected:
245  /** The tracking method implementation, to be implemented in children
246  * classes. */
247  virtual void trackFeatures_impl(
248  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
249  TSimpleFeaturefList& inout_featureList);
250 
251  /** The tracking method implementation, to be implemented in children
252  * classes. */
253  virtual void trackFeatures_impl(
254  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
255  TSimpleFeatureList& inout_featureList) = 0;
256 
257  /** This version falls back to the version with TSimpleFeatureList if the
258  * derived class does not implement it. */
259  virtual void trackFeatures_impl(
260  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
261  CFeatureList& inout_featureList) = 0;
262 
263  /** the internal time logger, disabled by default. */
265 
266  /** This field is clared by \a trackFeatures() before calling \a
267  * trackFeatures_impl(), and
268  * can be filled out with newly defected FAST(ER) features in the latter.
269  * If it's not the case, feats will be computed anyway if the user enabled
270  * the "add_new_features" option.
271  */
273 
274  /** Adapts the threshold \a m_detector_adaptive_thres according to the real
275  * and desired number of features just detected */
277  const size_t nNewlyDetectedFeats, const size_t desired_num_features);
278 
279  private:
280  /** for use when "update_patches_every">=1 */
282  /** For use when "check_KLT_response_every">=1 */
284  /** For use in "add_new_features" == true */
286 
287  template <typename FEATLIST>
289  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
290  FEATLIST& inout_featureList);
291 };
292 
293 using CGenericFeatureTrackerAutoPtr = std::unique_ptr<CGenericFeatureTracker>;
294 
295 /** Track a set of features from old_img -> new_img using sparse optimal flow
296  *(classic KL method).
297  *
298  * See CGenericFeatureTracker for a more detailed explanation on how to use
299  *this class.
300  *
301  * List of additional parameters in "extra_params" (apart from those in
302  *CGenericFeatureTracker) accepted by this class:
303  * - "window_width" (Default=15)
304  * - "window_height" (Default=15)
305  * - "LK_levels" (Default=3) Number of pyramids to build for LK tracking
306  *(this
307  *parameter only has effects when tracking with CImage's, not with
308  *CImagePyramid's).
309  * - "LK_max_iters" (Default=10) Max. number of iterations in LK tracking.
310  * - "LK_epsilon" (Default=0.1) Minimum epsilon step in interations of
311  *LK_tracking.
312  * - "LK_max_tracking_error" (Default=150.0) The maximum "tracking error"
313  *of
314  *LK tracking such as a feature is marked as "lost".
315  *
316  * \sa OpenCV's method cvCalcOpticalFlowPyrLK
317  */
319 {
320  /** Default ctor */
321  inline CFeatureTracker_KL() {}
322  /** Ctor with extra parameters */
324  : CGenericFeatureTracker(extraParams)
325  {
326  }
327 
328  protected:
329  virtual void trackFeatures_impl(
330  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
331  vision::CFeatureList& inout_featureList) override;
332  virtual void trackFeatures_impl(
333  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
334  TSimpleFeatureList& inout_featureList) override;
335  virtual void trackFeatures_impl(
336  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
337  TSimpleFeaturefList& inout_featureList) override;
338 
339  private:
340  template <typename FEATLIST>
342  const mrpt::img::CImage& old_img, const mrpt::img::CImage& new_img,
343  FEATLIST& inout_featureList);
344 };
345 
346 /** Search for correspondences which are not in the same row and deletes them
347  * ...
348  */
350  CFeatureList& leftList, CFeatureList& rightList,
351  vision::TMatchingOptions options);
352 
353 /** Filter bad correspondences by distance
354  * ...
355  */
357  mrpt::tfest::TMatchingPairList& list, // The list of correspondences
358  unsigned int numberOfSigmas); // Threshold
359 
360 /** @} */ // end of grouping
361 }
362 #endif
363 
364 
mrpt::system::TParametersDouble extra_params
Optional list of extra parameters to the algorithm.
Definition: tracking.h:146
void internal_trackFeatures(const mrpt::img::CImage &old_img, const mrpt::img::CImage &new_img, FEATLIST &inout_featureList)
Perform feature tracking from "old_img" to "new_img", with a (possibly empty) list of previously trac...
Definition: tracking.cpp:579
mrpt::system::CTimeLogger m_timlog
the internal time logger, disabled by default.
Definition: tracking.h:264
int m_detector_adaptive_thres
For use in "add_new_features" == true.
Definition: tracking.h:285
virtual void trackFeatures_impl(const mrpt::img::CImage &old_img, const mrpt::img::CImage &new_img, vision::CFeatureList &inout_featureList) override
This version falls back to the version with TSimpleFeatureList if the derived class does not implemen...
virtual void trackFeatures_impl(const mrpt::img::CImage &old_img, const mrpt::img::CImage &new_img, TSimpleFeaturefList &inout_featureList)
The tracking method implementation, to be implemented in children classes.
Definition: tracking.cpp:556
CFeatureTracker_KL(mrpt::system::TParametersDouble extraParams)
Ctor with extra parameters.
Definition: tracking.h:323
A list of TMatchingPair.
Definition: TMatchingPair.h:81
void updateAdaptiveNewFeatsThreshold(const size_t nNewlyDetectedFeats, const size_t desired_num_features)
Adapts the threshold m_detector_adaptive_thres according to the real and desired number of features j...
Definition: tracking.cpp:796
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:18
void trackFeatures(const mrpt::img::CImage &old_img, const mrpt::img::CImage &new_img, TSimpleFeatureList &inout_featureList)
Perform feature tracking from "old_img" to "new_img", with a (possibly empty) list of previously trac...
Definition: tracking.cpp:782
size_t raw_FAST_feats_detected
In the new_img with the last adaptive threshold.
Definition: tracking.h:235
void trackFeatures_impl_templ(const mrpt::img::CImage &old_img, const mrpt::img::CImage &new_img, FEATLIST &inout_featureList)
Track a set of features from old_img -> new_img using sparse optimal flow (classic KL method) Optiona...
Definition: tracking_KL.cpp:37
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:304
size_t num_deleted_feats
The number of features which were deleted due to OOB, bad tracking, etc...
Definition: tracking.h:238
std::unique_ptr< CGenericFeatureTracker > CGenericFeatureTrackerAutoPtr
Definition: tracking.h:293
void enable(bool enabled=true)
void checkTrackedFeatures(CFeatureList &leftList, CFeatureList &rightList, vision::TMatchingOptions options)
Search for correspondences which are not in the same row and deletes them ...
Definition: tracking.cpp:815
void enableTimeLogger(bool enable=true)
Returns a read-only reference to the internal time logger.
Definition: tracking.h:220
void trackFeaturesNewList(const mrpt::img::CImage &old_img, const mrpt::img::CImage &new_img, const vision::CFeatureList &in_featureList, vision::CFeatureList &out_featureList)
A wrapper around the basic trackFeatures() method, but keeping the original list of features unmodifi...
Definition: tracking.h:199
Track a set of features from old_img -> new_img using sparse optimal flow (classic KL method)...
Definition: tracking.h:318
mrpt::system::CTimeLogger & getProfiler()
Returns a reference to the internal time logger.
Definition: tracking.h:218
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
void filterBadCorrsByDistance(mrpt::tfest::TMatchingPairList &list, unsigned int numberOfSigmas)
Filter bad correspondences by distance ...
Definition: tracking.cpp:881
CGenericFeatureTracker(mrpt::system::TParametersDouble extraParams)
Ctor with extra parameters.
Definition: tracking.h:157
A structure containing options for the matching.
mrpt::vision::TSimpleFeatureList m_newly_detected_feats
This field is clared by trackFeatures() before calling trackFeatures_impl(), and can be filled out wi...
Definition: tracking.h:272
size_t m_check_KLT_counter
For use when "check_KLT_response_every">=1.
Definition: tracking.h:283
CFeatureTracker_KL()
Default ctor.
Definition: tracking.h:321
size_t m_update_patches_counter
for use when "update_patches_every">=1
Definition: tracking.h:281
TExtraOutputInfo last_execution_extra_info
Updated with each call to trackFeatures()
Definition: tracking.h:242
virtual ~CGenericFeatureTracker()
Dtor.
Definition: tracking.h:166
CGenericFeatureTracker()
Default ctor.
Definition: tracking.h:149
A virtual interface for all feature trackers, implementing the part of feature tracking that is commo...
Definition: tracking.h:143
int getDetectorAdaptiveThreshold() const
Returns the current adaptive threshold used by the FAST(ER) detector to find out new features in empt...
Definition: tracking.h:227
const mrpt::system::CTimeLogger & getProfiler() const
Returns a read-only reference to the internal time logger.
Definition: tracking.h:213
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020