tracking.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_LENLEN_HPP__
  42. #define __OPENCV_TRACKING_LENLEN_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 one of most 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. This 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 appearence 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. UML design: see @ref tracking_diagrams
  64. To see how API works, try tracker demo:
  65. <https://github.com/lenlen/opencv/blob/tracking_api/samples/cpp/tracker.cpp>
  66. @note This Tracking API has been designed with PlantUML. If you modify this API please change UML
  67. in <em>modules/tracking/doc/tracking_diagrams.markdown</em>. The following reference was used in the API
  68. Creating Own Tracker
  69. --------------------
  70. If you want create a new tracker, here's what you have to do. First, decide on the name of the class
  71. for the tracker (to meet the existing style, we suggest something with prefix "tracker", e.g.
  72. trackerMIL, trackerBoosting) -- we shall refer to this choice as to "classname" in subsequent. Also,
  73. you should decide upon the name of the tracker, is it will be known to user (the current style
  74. suggests using all capitals, say MIL or BOOSTING) --we'll call it a "name".
  75. - Declare your tracker in include/opencv2/tracking/tracker.hpp. Your tracker should inherit from
  76. Tracker (please, see the example below). You should declare the specialized Param structure,
  77. where you probably will want to put the data, needed to initialize your tracker. Also don't
  78. forget to put the BOILERPLATE_CODE(name,classname) macro inside the class declaration. That
  79. macro will generate static createTracker() function, which we'll talk about later. You should
  80. get something similar to :
  81. @code
  82. class CV_EXPORTS_W TrackerMIL : public Tracker
  83. {
  84. public:
  85. struct CV_EXPORTS Params
  86. {
  87. Params();
  88. //parameters for sampler
  89. float samplerInitInRadius; // radius for gathering positive instances during init
  90. int samplerInitMaxNegNum; // # negative samples to use during init
  91. float samplerSearchWinSize; // size of search window
  92. float samplerTrackInRadius; // radius for gathering positive instances during tracking
  93. int samplerTrackMaxPosNum; // # positive samples to use during tracking
  94. int samplerTrackMaxNegNum; // # negative samples to use during tracking
  95. int featureSetNumFeatures; // #features
  96. void read( const FileNode& fn );
  97. void write( FileStorage& fs ) const;
  98. };
  99. @endcode
  100. of course, you can also add any additional methods of your choice. It should be pointed out,
  101. however, that it is not expected to have a constructor declared, as creation should be done via
  102. the corresponding createTracker() method.
  103. - In src/tracker.cpp file add BOILERPLATE_CODE(name,classname) line to the body of
  104. Tracker::create() method you will find there, like :
  105. @code
  106. Ptr<Tracker> Tracker::create( const String& trackerType )
  107. {
  108. BOILERPLATE_CODE("BOOSTING",TrackerBoosting);
  109. BOILERPLATE_CODE("MIL",TrackerMIL);
  110. return Ptr<Tracker>();
  111. }
  112. @endcode
  113. - Finally, you should implement the function with signature :
  114. @code
  115. Ptr<classname> classname::createTracker(const classname::Params &parameters){
  116. ...
  117. }
  118. @endcode
  119. That function can (and probably will) return a pointer to some derived class of "classname",
  120. which will probably have a real constructor.
  121. Every tracker has three component TrackerSampler, TrackerFeatureSet and TrackerModel. The first two
  122. are instantiated from Tracker base class, instead the last component is abstract, so you must
  123. implement your TrackerModel.
  124. ### TrackerSampler
  125. TrackerSampler is already instantiated, but you should define the sampling algorithm and add the
  126. classes (or single class) to TrackerSampler. You can choose one of the ready implementation as
  127. TrackerSamplerCSC or you can implement your sampling method, in this case the class must inherit
  128. TrackerSamplerAlgorithm. Fill the samplingImpl method that writes the result in "sample" output
  129. argument.
  130. Example of creating specialized TrackerSamplerAlgorithm TrackerSamplerCSC : :
  131. @code
  132. class CV_EXPORTS_W TrackerSamplerCSC : public TrackerSamplerAlgorithm
  133. {
  134. public:
  135. TrackerSamplerCSC( const TrackerSamplerCSC::Params &parameters = TrackerSamplerCSC::Params() );
  136. ~TrackerSamplerCSC();
  137. ...
  138. protected:
  139. bool samplingImpl( const Mat& image, Rect boundingBox, std::vector<Mat>& sample );
  140. ...
  141. };
  142. @endcode
  143. Example of adding TrackerSamplerAlgorithm to TrackerSampler : :
  144. @code
  145. //sampler is the TrackerSampler
  146. Ptr<TrackerSamplerAlgorithm> CSCSampler = new TrackerSamplerCSC( CSCparameters );
  147. if( !sampler->addTrackerSamplerAlgorithm( CSCSampler ) )
  148. return false;
  149. //or add CSC sampler with default parameters
  150. //sampler->addTrackerSamplerAlgorithm( "CSC" );
  151. @endcode
  152. @sa
  153. TrackerSamplerCSC, TrackerSamplerAlgorithm
  154. ### TrackerFeatureSet
  155. TrackerFeatureSet is already instantiated (as first) , but you should define what kinds of features
  156. you'll use in your tracker. You can use multiple feature types, so you can add a ready
  157. implementation as TrackerFeatureHAAR in your TrackerFeatureSet or develop your own implementation.
  158. In this case, in the computeImpl method put the code that extract the features and in the selection
  159. method optionally put the code for the refinement and selection of the features.
  160. Example of creating specialized TrackerFeature TrackerFeatureHAAR : :
  161. @code
  162. class CV_EXPORTS_W TrackerFeatureHAAR : public TrackerFeature
  163. {
  164. public:
  165. TrackerFeatureHAAR( const TrackerFeatureHAAR::Params &parameters = TrackerFeatureHAAR::Params() );
  166. ~TrackerFeatureHAAR();
  167. void selection( Mat& response, int npoints );
  168. ...
  169. protected:
  170. bool computeImpl( const std::vector<Mat>& images, Mat& response );
  171. ...
  172. };
  173. @endcode
  174. Example of adding TrackerFeature to TrackerFeatureSet : :
  175. @code
  176. //featureSet is the TrackerFeatureSet
  177. Ptr<TrackerFeature> trackerFeature = new TrackerFeatureHAAR( HAARparameters );
  178. featureSet->addTrackerFeature( trackerFeature );
  179. @endcode
  180. @sa
  181. TrackerFeatureHAAR, TrackerFeatureSet
  182. ### TrackerModel
  183. TrackerModel is abstract, so in your implementation you must develop your TrackerModel that inherit
  184. from TrackerModel. Fill the method for the estimation of the state "modelEstimationImpl", that
  185. estimates the most likely target location, see @cite AAM table I (ME) for further information. Fill
  186. "modelUpdateImpl" in order to update the model, see @cite AAM table I (MU). In this class you can use
  187. the :cConfidenceMap and :cTrajectory to storing the model. The first represents the model on the all
  188. possible candidate states and the second represents the list of all estimated states.
  189. Example of creating specialized TrackerModel TrackerMILModel : :
  190. @code
  191. class TrackerMILModel : public TrackerModel
  192. {
  193. public:
  194. TrackerMILModel( const Rect& boundingBox );
  195. ~TrackerMILModel();
  196. ...
  197. protected:
  198. void modelEstimationImpl( const std::vector<Mat>& responses );
  199. void modelUpdateImpl();
  200. ...
  201. };
  202. @endcode
  203. And add it in your Tracker : :
  204. @code
  205. bool TrackerMIL::initImpl( const Mat& image, const Rect2d& boundingBox )
  206. {
  207. ...
  208. //model is the general TrackerModel field of the general Tracker
  209. model = new TrackerMILModel( boundingBox );
  210. ...
  211. }
  212. @endcode
  213. In the last step you should define the TrackerStateEstimator based on your implementation or you can
  214. use one of ready class as TrackerStateEstimatorMILBoosting. It represent the statistical part of the
  215. model that estimates the most likely target state.
  216. Example of creating specialized TrackerStateEstimator TrackerStateEstimatorMILBoosting : :
  217. @code
  218. class CV_EXPORTS_W TrackerStateEstimatorMILBoosting : public TrackerStateEstimator
  219. {
  220. class TrackerMILTargetState : public TrackerTargetState
  221. {
  222. ...
  223. };
  224. public:
  225. TrackerStateEstimatorMILBoosting( int nFeatures = 250 );
  226. ~TrackerStateEstimatorMILBoosting();
  227. ...
  228. protected:
  229. Ptr<TrackerTargetState> estimateImpl( const std::vector<ConfidenceMap>& confidenceMaps );
  230. void updateImpl( std::vector<ConfidenceMap>& confidenceMaps );
  231. ...
  232. };
  233. @endcode
  234. And add it in your TrackerModel : :
  235. @code
  236. //model is the TrackerModel of your Tracker
  237. Ptr<TrackerStateEstimatorMILBoosting> stateEstimator = new TrackerStateEstimatorMILBoosting( params.featureSetNumFeatures );
  238. model->setTrackerStateEstimator( stateEstimator );
  239. @endcode
  240. @sa
  241. TrackerModel, TrackerStateEstimatorMILBoosting, TrackerTargetState
  242. During this step, you should define your TrackerTargetState based on your implementation.
  243. TrackerTargetState base class has only the bounding box (upper-left position, width and height), you
  244. can enrich it adding scale factor, target rotation, etc.
  245. Example of creating specialized TrackerTargetState TrackerMILTargetState : :
  246. @code
  247. class TrackerMILTargetState : public TrackerTargetState
  248. {
  249. public:
  250. TrackerMILTargetState( const Point2f& position, int targetWidth, int targetHeight, bool foreground, const Mat& features );
  251. ~TrackerMILTargetState();
  252. ...
  253. private:
  254. bool isTarget;
  255. Mat targetFeatures;
  256. ...
  257. };
  258. @endcode
  259. ### Try it
  260. To try your tracker you can use the demo at
  261. <https://github.com/lenlen/opencv/blob/tracking_api/samples/cpp/tracker.cpp>.
  262. The first argument is the name of the tracker and the second is a video source.
  263. */
  264. #include <opencv2/tracking/tracker.hpp>
  265. #include <opencv2/tracking/tldDataset.hpp>
  266. #endif //__OPENCV_TRACKING_LENLEN