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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019