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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST