tracking.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef __OPENCV_TRACKING_HPP__
  42. #define __OPENCV_TRACKING_HPP__
  43. #include "opencv2/core/cvdef.h"
  44. /** @defgroup tracking Tracking API
  45. Long-term optical tracking API
  46. ------------------------------
  47. Long-term optical tracking is an important issue for many computer vision applications in
  48. real world scenario. The development in this area is very fragmented and this API is an unique
  49. interface useful for plug several algorithms and compare them. This work is partially based on
  50. @cite AAM and @cite AMVOT .
  51. These algorithms start from a bounding box of the target and with their internal representation they
  52. avoid the drift during the tracking. These long-term trackers are able to evaluate online the
  53. quality of the location of the target in the new frame, without ground truth.
  54. There are three main components: the TrackerSampler, the TrackerFeatureSet and the TrackerModel. The
  55. first component is the object that computes the patches over the frame based on the last target
  56. location. The TrackerFeatureSet is the class that manages the Features, is possible plug many kind
  57. of these (HAAR, HOG, LBP, Feature2D, etc). The last component is the internal representation of the
  58. target, it is the appearance model. It stores all state candidates and compute the trajectory (the
  59. most likely target states). The class TrackerTargetState represents a possible state of the target.
  60. The TrackerSampler and the TrackerFeatureSet are the visual representation of the target, instead
  61. the TrackerModel is the statistical model.
  62. A recent benchmark between these algorithms can be found in @cite OOT
  63. Creating Your Own %Tracker
  64. --------------------
  65. If you want to create a new tracker, here's what you have to do. First, decide on the name of the class
  66. for the tracker (to meet the existing style, we suggest something with prefix "tracker", e.g.
  67. trackerMIL, trackerBoosting) -- we shall refer to this choice as to "classname" in subsequent.
  68. - Declare your tracker in modules/tracking/include/opencv2/tracking/tracker.hpp. Your tracker should inherit from
  69. Tracker (please, see the example below). You should declare the specialized Param structure,
  70. where you probably will want to put the data, needed to initialize your tracker. You should
  71. get something similar to :
  72. @code
  73. class CV_EXPORTS_W TrackerMIL : public Tracker
  74. {
  75. public:
  76. struct CV_EXPORTS Params
  77. {
  78. Params();
  79. //parameters for sampler
  80. float samplerInitInRadius; // radius for gathering positive instances during init
  81. int samplerInitMaxNegNum; // # negative samples to use during init
  82. float samplerSearchWinSize; // size of search window
  83. float samplerTrackInRadius; // radius for gathering positive instances during tracking
  84. int samplerTrackMaxPosNum; // # positive samples to use during tracking
  85. int samplerTrackMaxNegNum; // # negative samples to use during tracking
  86. int featureSetNumFeatures; // #features
  87. void read( const FileNode& fn );
  88. void write( FileStorage& fs ) const;
  89. };
  90. @endcode
  91. of course, you can also add any additional methods of your choice. It should be pointed out,
  92. however, that it is not expected to have a constructor declared, as creation should be done via
  93. the corresponding create() method.
  94. - Finally, you should implement the function with signature :
  95. @code
  96. Ptr<classname> classname::create(const classname::Params &parameters){
  97. ...
  98. }
  99. @endcode
  100. That function can (and probably will) return a pointer to some derived class of "classname",
  101. which will probably have a real constructor.
  102. Every tracker has three component TrackerSampler, TrackerFeatureSet and TrackerModel. The first two
  103. are instantiated from Tracker base class, instead the last component is abstract, so you must
  104. implement your TrackerModel.
  105. ### TrackerSampler
  106. TrackerSampler is already instantiated, but you should define the sampling algorithm and add the
  107. classes (or single class) to TrackerSampler. You can choose one of the ready implementation as
  108. TrackerSamplerCSC or you can implement your sampling method, in this case the class must inherit
  109. TrackerSamplerAlgorithm. Fill the samplingImpl method that writes the result in "sample" output
  110. argument.
  111. Example of creating specialized TrackerSamplerAlgorithm TrackerSamplerCSC : :
  112. @code
  113. class CV_EXPORTS_W TrackerSamplerCSC : public TrackerSamplerAlgorithm
  114. {
  115. public:
  116. TrackerSamplerCSC( const TrackerSamplerCSC::Params &parameters = TrackerSamplerCSC::Params() );
  117. ~TrackerSamplerCSC();
  118. ...
  119. protected:
  120. bool samplingImpl( const Mat& image, Rect boundingBox, std::vector<Mat>& sample );
  121. ...
  122. };
  123. @endcode
  124. Example of adding TrackerSamplerAlgorithm to TrackerSampler : :
  125. @code
  126. //sampler is the TrackerSampler
  127. Ptr<TrackerSamplerAlgorithm> CSCSampler = new TrackerSamplerCSC( CSCparameters );
  128. if( !sampler->addTrackerSamplerAlgorithm( CSCSampler ) )
  129. return false;
  130. //or add CSC sampler with default parameters
  131. //sampler->addTrackerSamplerAlgorithm( "CSC" );
  132. @endcode
  133. @sa
  134. TrackerSamplerCSC, TrackerSamplerAlgorithm
  135. ### TrackerFeatureSet
  136. TrackerFeatureSet is already instantiated (as first) , but you should define what kinds of features
  137. you'll use in your tracker. You can use multiple feature types, so you can add a ready
  138. implementation as TrackerFeatureHAAR in your TrackerFeatureSet or develop your own implementation.
  139. In this case, in the computeImpl method put the code that extract the features and in the selection
  140. method optionally put the code for the refinement and selection of the features.
  141. Example of creating specialized TrackerFeature TrackerFeatureHAAR : :
  142. @code
  143. class CV_EXPORTS_W TrackerFeatureHAAR : public TrackerFeature
  144. {
  145. public:
  146. TrackerFeatureHAAR( const TrackerFeatureHAAR::Params &parameters = TrackerFeatureHAAR::Params() );
  147. ~TrackerFeatureHAAR();
  148. void selection( Mat& response, int npoints );
  149. ...
  150. protected:
  151. bool computeImpl( const std::vector<Mat>& images, Mat& response );
  152. ...
  153. };
  154. @endcode
  155. Example of adding TrackerFeature to TrackerFeatureSet : :
  156. @code
  157. //featureSet is the TrackerFeatureSet
  158. Ptr<TrackerFeature> trackerFeature = new TrackerFeatureHAAR( HAARparameters );
  159. featureSet->addTrackerFeature( trackerFeature );
  160. @endcode
  161. @sa
  162. TrackerFeatureHAAR, TrackerFeatureSet
  163. ### TrackerModel
  164. TrackerModel is abstract, so in your implementation you must develop your TrackerModel that inherit
  165. from TrackerModel. Fill the method for the estimation of the state "modelEstimationImpl", that
  166. estimates the most likely target location, see @cite AAM table I (ME) for further information. Fill
  167. "modelUpdateImpl" in order to update the model, see @cite AAM table I (MU). In this class you can use
  168. the :cConfidenceMap and :cTrajectory to storing the model. The first represents the model on the all
  169. possible candidate states and the second represents the list of all estimated states.
  170. Example of creating specialized TrackerModel TrackerMILModel : :
  171. @code
  172. class TrackerMILModel : public TrackerModel
  173. {
  174. public:
  175. TrackerMILModel( const Rect& boundingBox );
  176. ~TrackerMILModel();
  177. ...
  178. protected:
  179. void modelEstimationImpl( const std::vector<Mat>& responses );
  180. void modelUpdateImpl();
  181. ...
  182. };
  183. @endcode
  184. And add it in your Tracker : :
  185. @code
  186. bool TrackerMIL::initImpl( const Mat& image, const Rect2d& boundingBox )
  187. {
  188. ...
  189. //model is the general TrackerModel field of the general Tracker
  190. model = new TrackerMILModel( boundingBox );
  191. ...
  192. }
  193. @endcode
  194. In the last step you should define the TrackerStateEstimator based on your implementation or you can
  195. use one of ready class as TrackerStateEstimatorMILBoosting. It represent the statistical part of the
  196. model that estimates the most likely target state.
  197. Example of creating specialized TrackerStateEstimator TrackerStateEstimatorMILBoosting : :
  198. @code
  199. class CV_EXPORTS_W TrackerStateEstimatorMILBoosting : public TrackerStateEstimator
  200. {
  201. class TrackerMILTargetState : public TrackerTargetState
  202. {
  203. ...
  204. };
  205. public:
  206. TrackerStateEstimatorMILBoosting( int nFeatures = 250 );
  207. ~TrackerStateEstimatorMILBoosting();
  208. ...
  209. protected:
  210. Ptr<TrackerTargetState> estimateImpl( const std::vector<ConfidenceMap>& confidenceMaps );
  211. void updateImpl( std::vector<ConfidenceMap>& confidenceMaps );
  212. ...
  213. };
  214. @endcode
  215. And add it in your TrackerModel : :
  216. @code
  217. //model is the TrackerModel of your Tracker
  218. Ptr<TrackerStateEstimatorMILBoosting> stateEstimator = new TrackerStateEstimatorMILBoosting( params.featureSetNumFeatures );
  219. model->setTrackerStateEstimator( stateEstimator );
  220. @endcode
  221. @sa
  222. TrackerModel, TrackerStateEstimatorMILBoosting, TrackerTargetState
  223. During this step, you should define your TrackerTargetState based on your implementation.
  224. TrackerTargetState base class has only the bounding box (upper-left position, width and height), you
  225. can enrich it adding scale factor, target rotation, etc.
  226. Example of creating specialized TrackerTargetState TrackerMILTargetState : :
  227. @code
  228. class TrackerMILTargetState : public TrackerTargetState
  229. {
  230. public:
  231. TrackerMILTargetState( const Point2f& position, int targetWidth, int targetHeight, bool foreground, const Mat& features );
  232. ~TrackerMILTargetState();
  233. ...
  234. private:
  235. bool isTarget;
  236. Mat targetFeatures;
  237. ...
  238. };
  239. @endcode
  240. */
  241. #include <opencv2/tracking/tracker.hpp>
  242. #include <opencv2/tracking/tldDataset.hpp>
  243. #endif //__OPENCV_TRACKING_HPP__