tracking.detail.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_VIDEO_DETAIL_TRACKING_HPP
  5. #define OPENCV_VIDEO_DETAIL_TRACKING_HPP
  6. /*
  7. * Partially based on:
  8. * ====================================================================================================================
  9. * - [AAM] S. Salti, A. Cavallaro, L. Di Stefano, Adaptive Appearance Modeling for Video Tracking: Survey and Evaluation
  10. * - [AMVOT] X. Li, W. Hu, C. Shen, Z. Zhang, A. Dick, A. van den Hengel, A Survey of Appearance Models in Visual Object Tracking
  11. *
  12. * This Tracking API has been designed with PlantUML. If you modify this API please change UML files under modules/tracking/doc/uml
  13. *
  14. */
  15. #include "opencv2/core.hpp"
  16. namespace cv {
  17. namespace detail {
  18. inline namespace tracking {
  19. /** @addtogroup tracking_detail
  20. @{
  21. */
  22. /************************************ TrackerFeature Base Classes ************************************/
  23. /** @brief Abstract base class for TrackerFeature that represents the feature.
  24. */
  25. class CV_EXPORTS TrackerFeature
  26. {
  27. public:
  28. virtual ~TrackerFeature();
  29. /** @brief Compute the features in the images collection
  30. @param images The images
  31. @param response The output response
  32. */
  33. void compute(const std::vector<Mat>& images, Mat& response);
  34. protected:
  35. virtual bool computeImpl(const std::vector<Mat>& images, Mat& response) = 0;
  36. };
  37. /** @brief Class that manages the extraction and selection of features
  38. @cite AAM Feature Extraction and Feature Set Refinement (Feature Processing and Feature Selection).
  39. See table I and section III C @cite AMVOT Appearance modelling -\> Visual representation (Table II,
  40. section 3.1 - 3.2)
  41. TrackerFeatureSet is an aggregation of TrackerFeature
  42. @sa
  43. TrackerFeature
  44. */
  45. class CV_EXPORTS TrackerFeatureSet
  46. {
  47. public:
  48. TrackerFeatureSet();
  49. ~TrackerFeatureSet();
  50. /** @brief Extract features from the images collection
  51. @param images The input images
  52. */
  53. void extraction(const std::vector<Mat>& images);
  54. /** @brief Add TrackerFeature in the collection. Return true if TrackerFeature is added, false otherwise
  55. @param feature The TrackerFeature class
  56. */
  57. bool addTrackerFeature(const Ptr<TrackerFeature>& feature);
  58. /** @brief Get the TrackerFeature collection (TrackerFeature name, TrackerFeature pointer)
  59. */
  60. const std::vector<Ptr<TrackerFeature>>& getTrackerFeatures() const;
  61. /** @brief Get the responses
  62. @note Be sure to call extraction before getResponses Example TrackerFeatureSet::getResponses
  63. */
  64. const std::vector<Mat>& getResponses() const;
  65. private:
  66. void clearResponses();
  67. bool blockAddTrackerFeature;
  68. std::vector<Ptr<TrackerFeature>> features; // list of features
  69. std::vector<Mat> responses; // list of response after compute
  70. };
  71. /************************************ TrackerSampler Base Classes ************************************/
  72. /** @brief Abstract base class for TrackerSamplerAlgorithm that represents the algorithm for the specific
  73. sampler.
  74. */
  75. class CV_EXPORTS TrackerSamplerAlgorithm
  76. {
  77. public:
  78. virtual ~TrackerSamplerAlgorithm();
  79. /** @brief Computes the regions starting from a position in an image.
  80. Return true if samples are computed, false otherwise
  81. @param image The current frame
  82. @param boundingBox The bounding box from which regions can be calculated
  83. @param sample The computed samples @cite AAM Fig. 1 variable Sk
  84. */
  85. virtual bool sampling(const Mat& image, const Rect& boundingBox, std::vector<Mat>& sample) = 0;
  86. };
  87. /**
  88. * \brief Class that manages the sampler in order to select regions for the update the model of the tracker
  89. * [AAM] Sampling e Labeling. See table I and section III B
  90. */
  91. /** @brief Class that manages the sampler in order to select regions for the update the model of the tracker
  92. @cite AAM Sampling e Labeling. See table I and section III B
  93. TrackerSampler is an aggregation of TrackerSamplerAlgorithm
  94. @sa
  95. TrackerSamplerAlgorithm
  96. */
  97. class CV_EXPORTS TrackerSampler
  98. {
  99. public:
  100. TrackerSampler();
  101. ~TrackerSampler();
  102. /** @brief Computes the regions starting from a position in an image
  103. @param image The current frame
  104. @param boundingBox The bounding box from which regions can be calculated
  105. */
  106. void sampling(const Mat& image, Rect boundingBox);
  107. /** @brief Return the collection of the TrackerSamplerAlgorithm
  108. */
  109. const std::vector<Ptr<TrackerSamplerAlgorithm>>& getSamplers() const;
  110. /** @brief Return the samples from all TrackerSamplerAlgorithm, @cite AAM Fig. 1 variable Sk
  111. */
  112. const std::vector<Mat>& getSamples() const;
  113. /** @brief Add TrackerSamplerAlgorithm in the collection. Return true if sampler is added, false otherwise
  114. @param sampler The TrackerSamplerAlgorithm
  115. */
  116. bool addTrackerSamplerAlgorithm(const Ptr<TrackerSamplerAlgorithm>& sampler);
  117. private:
  118. std::vector<Ptr<TrackerSamplerAlgorithm>> samplers;
  119. std::vector<Mat> samples;
  120. bool blockAddTrackerSampler;
  121. void clearSamples();
  122. };
  123. /************************************ TrackerModel Base Classes ************************************/
  124. /** @brief Abstract base class for TrackerTargetState that represents a possible state of the target.
  125. See @cite AAM \f$\hat{x}^{i}_{k}\f$ all the states candidates.
  126. Inherits this class with your Target state, In own implementation you can add scale variation,
  127. width, height, orientation, etc.
  128. */
  129. class CV_EXPORTS TrackerTargetState
  130. {
  131. public:
  132. virtual ~TrackerTargetState() {};
  133. /** @brief Get the position
  134. * @return The position
  135. */
  136. Point2f getTargetPosition() const;
  137. /** @brief Set the position
  138. * @param position The position
  139. */
  140. void setTargetPosition(const Point2f& position);
  141. /** @brief Get the width of the target
  142. * @return The width of the target
  143. */
  144. int getTargetWidth() const;
  145. /** @brief Set the width of the target
  146. * @param width The width of the target
  147. */
  148. void setTargetWidth(int width);
  149. /** @brief Get the height of the target
  150. * @return The height of the target
  151. */
  152. int getTargetHeight() const;
  153. /** @brief Set the height of the target
  154. * @param height The height of the target
  155. */
  156. void setTargetHeight(int height);
  157. protected:
  158. Point2f targetPosition;
  159. int targetWidth;
  160. int targetHeight;
  161. };
  162. /** @brief Represents the model of the target at frame \f$k\f$ (all states and scores)
  163. See @cite AAM The set of the pair \f$\langle \hat{x}^{i}_{k}, C^{i}_{k} \rangle\f$
  164. @sa TrackerTargetState
  165. */
  166. typedef std::vector<std::pair<Ptr<TrackerTargetState>, float>> ConfidenceMap;
  167. /** @brief Represents the estimate states for all frames
  168. @cite AAM \f$x_{k}\f$ is the trajectory of the target up to time \f$k\f$
  169. @sa TrackerTargetState
  170. */
  171. typedef std::vector<Ptr<TrackerTargetState>> Trajectory;
  172. /** @brief Abstract base class for TrackerStateEstimator that estimates the most likely target state.
  173. See @cite AAM State estimator
  174. See @cite AMVOT Statistical modeling (Fig. 3), Table III (generative) - IV (discriminative) - V (hybrid)
  175. */
  176. class CV_EXPORTS TrackerStateEstimator
  177. {
  178. public:
  179. virtual ~TrackerStateEstimator();
  180. /** @brief Estimate the most likely target state, return the estimated state
  181. @param confidenceMaps The overall appearance model as a list of :cConfidenceMap
  182. */
  183. Ptr<TrackerTargetState> estimate(const std::vector<ConfidenceMap>& confidenceMaps);
  184. /** @brief Update the ConfidenceMap with the scores
  185. @param confidenceMaps The overall appearance model as a list of :cConfidenceMap
  186. */
  187. void update(std::vector<ConfidenceMap>& confidenceMaps);
  188. /** @brief Create TrackerStateEstimator by tracker state estimator type
  189. @param trackeStateEstimatorType The TrackerStateEstimator name
  190. The modes available now:
  191. - "BOOSTING" -- Boosting-based discriminative appearance models. See @cite AMVOT section 4.4
  192. The modes available soon:
  193. - "SVM" -- SVM-based discriminative appearance models. See @cite AMVOT section 4.5
  194. */
  195. static Ptr<TrackerStateEstimator> create(const String& trackeStateEstimatorType);
  196. /** @brief Get the name of the specific TrackerStateEstimator
  197. */
  198. String getClassName() const;
  199. protected:
  200. virtual Ptr<TrackerTargetState> estimateImpl(const std::vector<ConfidenceMap>& confidenceMaps) = 0;
  201. virtual void updateImpl(std::vector<ConfidenceMap>& confidenceMaps) = 0;
  202. String className;
  203. };
  204. /** @brief Abstract class that represents the model of the target.
  205. It must be instantiated by specialized tracker
  206. See @cite AAM Ak
  207. Inherits this with your TrackerModel
  208. */
  209. class CV_EXPORTS TrackerModel
  210. {
  211. public:
  212. TrackerModel();
  213. virtual ~TrackerModel();
  214. /** @brief Set TrackerEstimator, return true if the tracker state estimator is added, false otherwise
  215. @param trackerStateEstimator The TrackerStateEstimator
  216. @note You can add only one TrackerStateEstimator
  217. */
  218. bool setTrackerStateEstimator(Ptr<TrackerStateEstimator> trackerStateEstimator);
  219. /** @brief Estimate the most likely target location
  220. @cite AAM ME, Model Estimation table I
  221. @param responses Features extracted from TrackerFeatureSet
  222. */
  223. void modelEstimation(const std::vector<Mat>& responses);
  224. /** @brief Update the model
  225. @cite AAM MU, Model Update table I
  226. */
  227. void modelUpdate();
  228. /** @brief Run the TrackerStateEstimator, return true if is possible to estimate a new state, false otherwise
  229. */
  230. bool runStateEstimator();
  231. /** @brief Set the current TrackerTargetState in the Trajectory
  232. @param lastTargetState The current TrackerTargetState
  233. */
  234. void setLastTargetState(const Ptr<TrackerTargetState>& lastTargetState);
  235. /** @brief Get the last TrackerTargetState from Trajectory
  236. */
  237. Ptr<TrackerTargetState> getLastTargetState() const;
  238. /** @brief Get the list of the ConfidenceMap
  239. */
  240. const std::vector<ConfidenceMap>& getConfidenceMaps() const;
  241. /** @brief Get the last ConfidenceMap for the current frame
  242. */
  243. const ConfidenceMap& getLastConfidenceMap() const;
  244. /** @brief Get the TrackerStateEstimator
  245. */
  246. Ptr<TrackerStateEstimator> getTrackerStateEstimator() const;
  247. private:
  248. void clearCurrentConfidenceMap();
  249. protected:
  250. std::vector<ConfidenceMap> confidenceMaps;
  251. Ptr<TrackerStateEstimator> stateEstimator;
  252. ConfidenceMap currentConfidenceMap;
  253. Trajectory trajectory;
  254. int maxCMLength;
  255. virtual void modelEstimationImpl(const std::vector<Mat>& responses) = 0;
  256. virtual void modelUpdateImpl() = 0;
  257. };
  258. /************************************ Specific TrackerStateEstimator Classes ************************************/
  259. // None
  260. /************************************ Specific TrackerSamplerAlgorithm Classes ************************************/
  261. /** @brief TrackerSampler based on CSC (current state centered), used by MIL algorithm TrackerMIL
  262. */
  263. class CV_EXPORTS TrackerSamplerCSC : public TrackerSamplerAlgorithm
  264. {
  265. public:
  266. ~TrackerSamplerCSC();
  267. enum MODE
  268. {
  269. MODE_INIT_POS = 1, //!< mode for init positive samples
  270. MODE_INIT_NEG = 2, //!< mode for init negative samples
  271. MODE_TRACK_POS = 3, //!< mode for update positive samples
  272. MODE_TRACK_NEG = 4, //!< mode for update negative samples
  273. MODE_DETECT = 5 //!< mode for detect samples
  274. };
  275. struct CV_EXPORTS Params
  276. {
  277. Params();
  278. float initInRad; //!< radius for gathering positive instances during init
  279. float trackInPosRad; //!< radius for gathering positive instances during tracking
  280. float searchWinSize; //!< size of search window
  281. int initMaxNegNum; //!< # negative samples to use during init
  282. int trackMaxPosNum; //!< # positive samples to use during training
  283. int trackMaxNegNum; //!< # negative samples to use during training
  284. };
  285. /** @brief Constructor
  286. @param parameters TrackerSamplerCSC parameters TrackerSamplerCSC::Params
  287. */
  288. TrackerSamplerCSC(const TrackerSamplerCSC::Params& parameters = TrackerSamplerCSC::Params());
  289. /** @brief Set the sampling mode of TrackerSamplerCSC
  290. @param samplingMode The sampling mode
  291. The modes are:
  292. - "MODE_INIT_POS = 1" -- for the positive sampling in initialization step
  293. - "MODE_INIT_NEG = 2" -- for the negative sampling in initialization step
  294. - "MODE_TRACK_POS = 3" -- for the positive sampling in update step
  295. - "MODE_TRACK_NEG = 4" -- for the negative sampling in update step
  296. - "MODE_DETECT = 5" -- for the sampling in detection step
  297. */
  298. void setMode(int samplingMode);
  299. bool sampling(const Mat& image, const Rect& boundingBox, std::vector<Mat>& sample) CV_OVERRIDE;
  300. private:
  301. Params params;
  302. int mode;
  303. RNG rng;
  304. std::vector<Mat> sampleImage(const Mat& img, int x, int y, int w, int h, float inrad, float outrad = 0, int maxnum = 1000000);
  305. };
  306. //! @}
  307. }}} // namespace cv::detail::tracking
  308. #endif // OPENCV_VIDEO_DETAIL_TRACKING_HPP