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