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



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014