tracking.hpp 13 KB

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