00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 00005 | | 00006 | Copyright (C) 2005-2011 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 00029 #ifndef mrpt_vision_tracking_H 00030 #define mrpt_vision_tracking_H 00031 00032 #include <mrpt/vision/types.h> 00033 #include <mrpt/vision/link_pragmas.h> 00034 00035 #include <mrpt/vision/CFeature.h> 00036 #include <mrpt/vision/TSimpleFeature.h> 00037 #include <mrpt/utils/CImage.h> 00038 #include <mrpt/utils/CTimeLogger.h> 00039 #include <mrpt/utils/TParameters.h> 00040 00041 #include <mrpt/utils/metaprogramming.h> 00042 00043 #include <memory> // for auto_ptr 00044 00045 namespace mrpt 00046 { 00047 namespace vision 00048 { 00049 using namespace mrpt::math; 00050 using namespace mrpt::utils; 00051 00052 /** \addtogroup vision_tracking Feature detection and tracking 00053 * \ingroup mrpt_vision_grp 00054 * @{ */ 00055 00056 /** A virtual interface for all feature trackers, implementing the part of feature tracking that is common to any specific tracker implementation. 00057 * This class provides a quite robust tracking of features, avoiding as many outliers as possible but not all of them: 00058 * more robust tracking would require application-specific information and could be done in a number of very different approaches, 00059 * so this class will not try to do any kind of RANSAC or any other advanced outlier rejection; instead, it should 00060 * be done by the users or the classes that employ this class. 00061 * 00062 * The basic usage of this class is as follows: 00063 * \code 00064 * CFeatureTracker_KL tracker; // Note: CFeatureTracker_KL is the most robust implementation for now. 00065 * tracker.extra_params["add_new_features"] = 1; // Enable detection of new features, not only tracking 00066 * tracker.extra_params[...] = ... 00067 * // .... 00068 * CFeatureList theFeats; // The list of features 00069 * mrpt::utils::CImage previous_img, current_img; 00070 * 00071 * while (true) { 00072 * current_img = ... // Grab new image. 00073 * if ( previous_img_is_ok ) 00074 * tracker.trackFeatures(previous_img, current_img, theFeats); 00075 * previous_img = current_img; 00076 * } 00077 * \endcode 00078 * 00079 * Below follows the list of optional parameters for "extra_params" which can be set 00080 * and will be understood by this base class for any specific tracker implementation. 00081 * Note that all parameters are double's, but boolean flags are emulated by the values 0.0 (false) and 1.0 (true). 00082 * 00083 * List of parameters: 00084 * <table border="1" > 00085 * <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> 00086 * <tr><td align="center" > add_new_features </td> <td align="center" > 0 </td> 00087 * <td> If set to "1", the class will not only track existing features, but will also perform (after doing the actual tracking) an efficient 00088 * search for new features with the FAST detector, and will add them to the passed "CFeatureList" if they fulfill a set of restrictions, 00089 * 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>). 00090 * </td> </tr> 00091 * <tr><td align="center" > add_new_feat_min_separation </td> <td align="center" > 15 </td> 00092 * <td> If <i>add_new_features</i>==1, this is the minimum separation (in pixels) to any other (old, or new) feature for it 00093 * being considered a candidate to be added. 00094 * </td> </tr> 00095 * <tr><td align="center" > desired_num_features_adapt </td> <td align="center" > (img_width*img_height)/512 </td> 00096 * <td> If <i>add_new_features</i>==1, the threshold of the FAST(ER) feature detector is dynamically adapted such as the number of 00097 * raw FAST keypoints is around this number. This number should be much higher than the real desired numbre of features, since this 00098 * one includes many features concentrated in space which are later discarded for the minimum distance. 00099 * </td> </tr> 00100 * <tr><td align="center" > desired_num_features </td> <td align="center" > 100 </td> 00101 * <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> 00102 * <tr><td align="center" > add_new_feat_patch_size </td> <td align="center" > 11 </td> 00103 * <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). 00104 * </td> </tr> 00105 * <tr><td align="center" > minimum_KLT_response_to_add </td> <td align="center" > 10 </td> 00106 * <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). 00107 * </td> </tr> 00108 * <tr><td align="center" > check_KLT_response_every </td> <td align="center" > 0 </td> 00109 * <td> If >0, it will compute the KLT response at each feature point every <i>N</i> frames 00110 * and those below <i>minimum_KLT_response</i> will be marked as "lost" in their "track_status" field. 00111 * </td> </tr> 00112 * <tr><td align="center" > minimum_KLT_response </td> <td align="center" > 5 </td> 00113 * <td> See explanation of <i>check_KLT_response_every</i>. 00114 * </td> </tr> 00115 * <tr><td align="center" > KLT_response_half_win </td> <td align="center" > 4 </td> 00116 * <td> When computing the KLT response of features (see <i>minimum_KLT_response</i> and <i>minimum_KLT_response_to_add</i>), 00117 * 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. 00118 * </td> </tr> 00119 * <tr><td align="center" > update_patches_every </td> <td align="center" > 0 </td> 00120 * <td> If !=0, the patch associated to each feature will be updated with every N'th frame. </td> </tr> 00121 * <tr><td align="center" > remove_lost_features </td> <td align="center" > 0 </td> 00122 * <td> If !=0, out-of-bound features or those lost while tracking, will be automatically removed from the list of features. 00123 * Otherwise, the user will have to manually remove them by checking the track_status field. </td> </tr> 00124 * </table> 00125 * 00126 * This class also offers a time profiler, disabled by default (see getProfiler and enableTimeLogger). 00127 * 00128 * \sa CFeatureTracker_KL, the example application "track-video-features". 00129 */ 00130 struct VISION_IMPEXP CGenericFeatureTracker 00131 { 00132 /** Optional list of extra parameters to the algorithm. */ 00133 mrpt::utils::TParametersDouble extra_params; 00134 00135 /** Default ctor */ 00136 inline CGenericFeatureTracker() : m_timlog(false), m_update_patches_counter(0),m_check_KLT_counter(0),m_detector_adaptive_thres(10) 00137 { } 00138 /** Ctor with extra parameters */ 00139 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) 00140 { } 00141 /** Dtor */ 00142 virtual ~CGenericFeatureTracker() 00143 { } 00144 00145 /** Perform feature tracking from "old_img" to "new_img", with a (possibly empty) list of previously tracked features "inout_featureList". 00146 * This is a list of parameters (in "extraParams") accepted by ALL implementations of feature tracker (see each derived class for more specific parameters). 00147 * - "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. 00148 * This method does: 00149 * - Convert old and new images to grayscale, if they're in color. 00150 * - Call the pure virtual "trackFeatures_impl" method. 00151 * - Implement the optional detection of new features if "add_new_features"!=0. 00152 */ 00153 void trackFeatures( 00154 const CImage &old_img, 00155 const CImage &new_img, 00156 TSimpleFeatureList &inout_featureList ); 00157 00158 /** \overload This overload version uses the old (and much slower) CFeatureList */ 00159 void trackFeatures( 00160 const CImage &old_img, 00161 const CImage &new_img, 00162 CFeatureList &inout_featureList ); 00163 00164 /** A wrapper around the basic trackFeatures() method, but keeping the original list of features unmodified and returns the tracked ones in a new list. */ 00165 inline void trackFeaturesNewList( 00166 const CImage &old_img, 00167 const CImage &new_img, 00168 const vision::CFeatureList &in_featureList, 00169 vision::CFeatureList &out_featureList 00170 ) 00171 { 00172 out_featureList = in_featureList; 00173 std::for_each( 00174 out_featureList.begin(),out_featureList.end(), 00175 mrpt::utils::metaprogramming::ObjectMakeUnique() ); 00176 this->trackFeatures(old_img, new_img, out_featureList); 00177 } 00178 00179 /** Returns a read-only reference to the internal time logger */ 00180 inline const mrpt::utils::CTimeLogger & getProfiler() const { return m_timlog; } 00181 /** Returns a reference to the internal time logger */ 00182 inline mrpt::utils::CTimeLogger & getProfiler() { return m_timlog; } 00183 00184 /** Returns a read-only reference to the internal time logger */ 00185 inline void enableTimeLogger(bool enable=true) { m_timlog.enable(enable); } 00186 00187 /** Returns the current adaptive threshold used by the FAST(ER) detector to find out new features in empty areas */ 00188 inline int getDetectorAdaptiveThreshold() const { return m_detector_adaptive_thres; } 00189 00190 struct VISION_IMPEXP TExtraOutputInfo 00191 { 00192 size_t raw_FAST_feats_detected; //!< In the new_img with the last adaptive threshold 00193 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) 00194 }; 00195 00196 TExtraOutputInfo last_execution_extra_info; //!< Updated with each call to trackFeatures() 00197 00198 protected: 00199 /** The tracking method implementation, to be implemented in children classes. */ 00200 virtual void trackFeatures_impl( 00201 const CImage &old_img, 00202 const CImage &new_img, 00203 TSimpleFeatureList &inout_featureList ) = 0; 00204 00205 /** This version falls back to the version with TSimpleFeatureList if the derived class does not implement it. */ 00206 virtual void trackFeatures_impl( 00207 const CImage &old_img, 00208 const CImage &new_img, 00209 CFeatureList &inout_featureList ) = 0; 00210 00211 mrpt::utils::CTimeLogger m_timlog; //!< the internal time logger, disabled by default. 00212 00213 /** This field is clared by \a trackFeatures() before calling \a trackFeatures_impl(), and 00214 * can be filled out with newly defected FAST(ER) features in the latter. 00215 * If it's not the case, feats will be computed anyway if the user enabled the "add_new_features" option. 00216 */ 00217 mrpt::vision::TSimpleFeatureList m_newly_detected_feats; 00218 00219 /** Adapts the threshold \a m_detector_adaptive_thres according to the real and desired number of features just detected */ 00220 void updateAdaptiveNewFeatsThreshold( 00221 const size_t nNewlyDetectedFeats, 00222 const size_t desired_num_features); 00223 00224 private: 00225 size_t m_update_patches_counter; //!< for use when "update_patches_every">=1 00226 size_t m_check_KLT_counter; //!< For use when "check_KLT_response_every">=1 00227 int m_detector_adaptive_thres; //!< For use in "add_new_features" == true 00228 00229 00230 template <typename FEATLIST> 00231 void internal_trackFeatures( 00232 const CImage &old_img, 00233 const CImage &new_img, 00234 FEATLIST &inout_featureList ); 00235 }; 00236 00237 typedef std::auto_ptr<CGenericFeatureTracker> CGenericFeatureTrackerAutoPtr; 00238 00239 00240 /** Track a set of features from old_img -> new_img using sparse optimal flow (classic KL method). 00241 * 00242 * See CGenericFeatureTracker for a more detailed explanation on how to use this class. 00243 * 00244 * List of additional parameters in "extra_params" (apart from those in CGenericFeatureTracker) accepted by this class: 00245 * - "window_width" (Default=15) 00246 * - "window_height" (Default=15) 00247 * - "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). 00248 * - "LK_max_iters" (Default=10) Max. number of iterations in LK tracking. 00249 * - "LK_epsilon" (Default=0.1) Minimum epsilon step in interations of LK_tracking. 00250 * - "LK_max_tracking_error" (Default=150.0) The maximum "tracking error" of LK tracking such as a feature is marked as "lost". 00251 * 00252 * \sa OpenCV's method cvCalcOpticalFlowPyrLK 00253 */ 00254 struct VISION_IMPEXP CFeatureTracker_KL : public CGenericFeatureTracker 00255 { 00256 /** Default ctor */ 00257 inline CFeatureTracker_KL() { } 00258 /** Ctor with extra parameters */ 00259 inline CFeatureTracker_KL(mrpt::utils::TParametersDouble extraParams) : CGenericFeatureTracker(extraParams) { } 00260 00261 protected: 00262 virtual void trackFeatures_impl( 00263 const CImage &old_img, 00264 const CImage &new_img, 00265 vision::CFeatureList &inout_featureList ); 00266 00267 /** The tracking method implementation, to be implemented in children classes. */ 00268 virtual void trackFeatures_impl( 00269 const CImage &old_img, 00270 const CImage &new_img, 00271 TSimpleFeatureList &inout_featureList ); 00272 00273 private: 00274 template <typename FEATLIST> 00275 void trackFeatures_impl_templ( 00276 const CImage &old_img, 00277 const CImage &new_img, 00278 FEATLIST &inout_featureList ); 00279 00280 }; 00281 00282 /** Track a set of features from old_img -> new_img by patch correlation over the closest FAST features, using a KD-tree for looking closest correspondences. 00283 * See CGenericFeatureTracker for a more detailed explanation on how to use this class. 00284 * 00285 * List of additional parameters in "extra_params" (apart from those in CGenericFeatureTracker) accepted by this class: 00286 * - "window_width" (Default=15) 00287 * - "window_height" (Default=15) 00288 * 00289 */ 00290 struct VISION_IMPEXP CFeatureTracker_FAST : public CGenericFeatureTracker 00291 { 00292 /** Ctor */ 00293 CFeatureTracker_FAST(const mrpt::utils::TParametersDouble & extraParams = mrpt::utils::TParametersDouble() ); 00294 00295 protected: 00296 virtual void trackFeatures_impl( 00297 const CImage &old_img, 00298 const CImage &new_img, 00299 vision::CFeatureList &inout_featureList ); 00300 00301 virtual void trackFeatures_impl( 00302 const CImage &old_img, 00303 const CImage &new_img, 00304 TSimpleFeatureList &inout_featureList ); 00305 00306 }; 00307 00308 00309 /** Track a set of features from old_img -> new_img by patch matching over a fixed window centered at each feature's previous location. 00310 * See CGenericFeatureTracker for a more detailed explanation on how to use this class. 00311 * 00312 * List of additional parameters in "extra_params" (apart from those in CGenericFeatureTracker) accepted by this class: 00313 * - "window_width" (Default=15) 00314 * - "window_height" (Default=15) 00315 * 00316 */ 00317 struct VISION_IMPEXP CFeatureTracker_PatchMatch : public CGenericFeatureTracker 00318 { 00319 /** Ctor */ 00320 CFeatureTracker_PatchMatch(const mrpt::utils::TParametersDouble & extraParams = mrpt::utils::TParametersDouble() ); 00321 00322 protected: 00323 virtual void trackFeatures_impl( 00324 const CImage &old_img, 00325 const CImage &new_img, 00326 vision::CFeatureList &inout_featureList ); 00327 }; 00328 00329 /** Search for correspondences which are not in the same row and deletes them 00330 * ... 00331 */ 00332 void VISION_IMPEXP checkTrackedFeatures( CFeatureList &leftList, 00333 CFeatureList &rightList, 00334 vision::TMatchingOptions options); 00335 00336 00337 /** Filter bad correspondences by distance 00338 * ... 00339 */ 00340 void VISION_IMPEXP filterBadCorrsByDistance( mrpt::utils::TMatchingPairList &list, // The list of correspondences 00341 unsigned int numberOfSigmas ); // Threshold 00342 00343 00344 00345 /** @} */ // end of grouping 00346 } 00347 } 00348 00349 00350 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |