|
| 1 | +Common Interfaces of Tracker |
| 2 | +============================ |
| 3 | + |
| 4 | +.. highlight:: cpp |
| 5 | + |
| 6 | + |
| 7 | +Tracker : Algorithm |
| 8 | +------------------- |
| 9 | + |
| 10 | +.. ocv:class:: Tracker |
| 11 | +
|
| 12 | +Base abstract class for the long-term tracker:: |
| 13 | + |
| 14 | + class CV_EXPORTS_W Tracker : public virtual Algorithm |
| 15 | + { |
| 16 | + virtual ~Tracker(); |
| 17 | + |
| 18 | + bool init( const Mat& image, const Rect& boundingBox ); |
| 19 | + |
| 20 | + bool update( const Mat& image, Rect& boundingBox ); |
| 21 | + |
| 22 | + static Ptr<Tracker> create( const String& trackerType ); |
| 23 | + |
| 24 | + }; |
| 25 | + |
| 26 | +Tracker::init |
| 27 | +------------- |
| 28 | + |
| 29 | +Initialize the tracker with a know bounding box that surrounding the target |
| 30 | + |
| 31 | +.. ocv:function:: bool Tracker::init( const Mat& image, const Rect& boundingBox ) |
| 32 | +
|
| 33 | + :param image: The initial frame |
| 34 | + |
| 35 | + :param boundingBox: The initial boundig box |
| 36 | + |
| 37 | + |
| 38 | +Tracker::update |
| 39 | +--------------- |
| 40 | + |
| 41 | +Update the tracker, find the new most likely bounding box for the target |
| 42 | + |
| 43 | +.. ocv:function:: bool Tracker::update( const Mat& image, Rect& boundingBox ) |
| 44 | +
|
| 45 | + :param image: The current frame |
| 46 | + |
| 47 | + :param boundingBox: The boundig box that represent the new target location |
| 48 | + |
| 49 | + |
| 50 | +Tracker::create |
| 51 | +--------------- |
| 52 | + |
| 53 | +Creates a tracker by its name. |
| 54 | + |
| 55 | +.. ocv:function:: static Ptr<Tracker> Tracker::create( const String& trackerType ) |
| 56 | +
|
| 57 | + :param trackerType: Tracker type |
| 58 | + |
| 59 | +The following detector types are supported: |
| 60 | + |
| 61 | +* ``"MIL"`` -- :ocv:class:`TrackerMIL` |
| 62 | + |
| 63 | +* ``"BOOSTING"`` -- :ocv:class:`TrackerBoosting` |
| 64 | + |
| 65 | +Creating Own Tracker |
| 66 | +-------------------- |
| 67 | + |
| 68 | +If you want create a new tracker, you should follow some simple rules. |
| 69 | + |
| 70 | +First, your tracker should be inherit from :ocv:class:`Tracker`, so you must implement two method: |
| 71 | + |
| 72 | +* Tracker: initImpl, it should be called once in the first frame, here you should initialize all structures. The second argument is the initial bounding box of the target. |
| 73 | + |
| 74 | +* Tracker:updateImpl, it should be called at the begin of in loop through video frames. Here you should overwrite the bounding box with new location. |
| 75 | + |
| 76 | +Example of creating specialized Tracker ``TrackerMIL`` : :: |
| 77 | + |
| 78 | + class CV_EXPORTS_W TrackerMIL : public Tracker |
| 79 | + { |
| 80 | + public: |
| 81 | + TrackerMIL( const TrackerMIL::Params ¶meters = TrackerMIL::Params() ); |
| 82 | + virtual ~TrackerMIL(); |
| 83 | + ... |
| 84 | + |
| 85 | + protected: |
| 86 | + bool initImpl( const Mat& image, const Rect& boundingBox ); |
| 87 | + bool updateImpl( const Mat& image, Rect& boundingBox ); |
| 88 | + ... |
| 89 | + }; |
| 90 | + |
| 91 | + |
| 92 | +Every tracker has three component :ocv:class:`TrackerSampler`, :ocv:class:`TrackerFeatureSet` and :ocv:class:`TrackerModel`. |
| 93 | +The first two are instantiated from Tracker base class, instead the last component is abstract, so you must implement your TrackerModel. |
| 94 | + |
| 95 | +Finally add your tracker in the file tracking_init.cpp |
| 96 | + |
| 97 | +TrackerSampler |
| 98 | +.............. |
| 99 | + |
| 100 | +TrackerSampler is already instantiated, but you should define the sampling algorithm and add the classes (or single class) to TrackerSampler. |
| 101 | +You can choose one of the ready implementation as TrackerSamplerCSC or you can implement your sampling method, in this case |
| 102 | +the class must inherit :ocv:class:`TrackerSamplerAlgorithm`. Fill the samplingImpl method that writes the result in "sample" output argument. |
| 103 | + |
| 104 | +Example of creating specialized TrackerSamplerAlgorithm ``TrackerSamplerCSC`` : :: |
| 105 | + |
| 106 | + class CV_EXPORTS_W TrackerSamplerCSC : public TrackerSamplerAlgorithm |
| 107 | + { |
| 108 | + public: |
| 109 | + TrackerSamplerCSC( const TrackerSamplerCSC::Params ¶meters = TrackerSamplerCSC::Params() ); |
| 110 | + ~TrackerSamplerCSC(); |
| 111 | + ... |
| 112 | + |
| 113 | + protected: |
| 114 | + bool samplingImpl( const Mat& image, Rect boundingBox, std::vector<Mat>& sample ); |
| 115 | + ... |
| 116 | + |
| 117 | + }; |
| 118 | + |
| 119 | +Example of adding TrackerSamplerAlgorithm to TrackerSampler : :: |
| 120 | + |
| 121 | + //sampler is the TrackerSampler |
| 122 | + Ptr<TrackerSamplerAlgorithm> CSCSampler = new TrackerSamplerCSC( CSCparameters ); |
| 123 | + if( !sampler->addTrackerSamplerAlgorithm( CSCSampler ) ) |
| 124 | + return false; |
| 125 | + |
| 126 | + //or add CSC sampler with default parameters |
| 127 | + //sampler->addTrackerSamplerAlgorithm( "CSC" ); |
| 128 | + |
| 129 | +.. seealso:: |
| 130 | + |
| 131 | + :ocv:class:`TrackerSamplerCSC`, :ocv:class:`TrackerSamplerAlgorithm` |
| 132 | + |
| 133 | + |
| 134 | +TrackerFeatureSet |
| 135 | +................. |
| 136 | + |
| 137 | +TrackerFeatureSet is already instantiated (as first) , but you should define what kinds of features you'll use in your tracker. |
| 138 | +You can use multiple feature types, so you can add a ready implementation as :ocv:class:`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 |
| 140 | +in the selection method optionally put the code for the refinement and selection of the features. |
| 141 | + |
| 142 | +Example of creating specialized TrackerFeature ``TrackerFeatureHAAR`` : :: |
| 143 | + |
| 144 | + class CV_EXPORTS_W TrackerFeatureHAAR : public TrackerFeature |
| 145 | + { |
| 146 | + public: |
| 147 | + TrackerFeatureHAAR( const TrackerFeatureHAAR::Params ¶meters = TrackerFeatureHAAR::Params() ); |
| 148 | + ~TrackerFeatureHAAR(); |
| 149 | + void selection( Mat& response, int npoints ); |
| 150 | + ... |
| 151 | + |
| 152 | + protected: |
| 153 | + bool computeImpl( const std::vector<Mat>& images, Mat& response ); |
| 154 | + ... |
| 155 | + |
| 156 | + }; |
| 157 | + |
| 158 | +Example of adding TrackerFeature to TrackerFeatureSet : :: |
| 159 | + |
| 160 | + //featureSet is the TrackerFeatureSet |
| 161 | + Ptr<TrackerFeature> trackerFeature = new TrackerFeatureHAAR( HAARparameters ); |
| 162 | + featureSet->addTrackerFeature( trackerFeature ); |
| 163 | + |
| 164 | +.. seealso:: |
| 165 | + |
| 166 | + :ocv:class:`TrackerFeatureHAAR`, :ocv:class:`TrackerFeatureSet` |
| 167 | + |
| 168 | +TrackerModel |
| 169 | +............ |
| 170 | + |
| 171 | +TrackerModel is abstract, so in your implementation you must develop your TrackerModel that inherit from :ocv:class:`TrackerModel`. |
| 172 | +Fill the method for the estimation of the state "modelEstimationImpl", that estimates the most likely target location, |
| 173 | +see [AAM]_ table I (ME) for further information. Fill "modelUpdateImpl" in order to update the model, see [AAM]_ table I (MU). |
| 174 | +In this class you can use the :c:type:`ConfidenceMap` and :c:type:`Trajectory` to storing the model. The first represents the model on the all |
| 175 | +possible candidate states and the second represents the list of all estimated states. |
| 176 | + |
| 177 | +Example of creating specialized TrackerModel ``TrackerMILModel`` : :: |
| 178 | + |
| 179 | + class TrackerMILModel : public TrackerModel |
| 180 | + { |
| 181 | + public: |
| 182 | + TrackerMILModel( const Rect& boundingBox ); |
| 183 | + ~TrackerMILModel(); |
| 184 | + ... |
| 185 | + |
| 186 | + protected: |
| 187 | + void modelEstimationImpl( const std::vector<Mat>& responses ); |
| 188 | + void modelUpdateImpl(); |
| 189 | + ... |
| 190 | + |
| 191 | + }; |
| 192 | + |
| 193 | +And add it in your Tracker : :: |
| 194 | + |
| 195 | + bool TrackerMIL::initImpl( const Mat& image, const Rect& boundingBox ) |
| 196 | + { |
| 197 | + ... |
| 198 | + //model is the general TrackerModel field od the general Tracker |
| 199 | + model = new TrackerMILModel( boundingBox ); |
| 200 | + ... |
| 201 | + } |
| 202 | + |
| 203 | + |
| 204 | +In the last step you should define the TrackerStateEstimator based on your implementation or you can use one of ready class as :ocv:class:`TrackerStateEstimatorMILBoosting`. |
| 205 | +It represent the statistical part of the model that estimates the most likely target state. |
| 206 | + |
| 207 | +Example of creating specialized TrackerStateEstimator ``TrackerStateEstimatorMILBoosting`` : :: |
| 208 | + |
| 209 | + class CV_EXPORTS_W TrackerStateEstimatorMILBoosting : public TrackerStateEstimator |
| 210 | + { |
| 211 | + class TrackerMILTargetState : public TrackerTargetState |
| 212 | + { |
| 213 | + ... |
| 214 | + }; |
| 215 | + |
| 216 | + public: |
| 217 | + TrackerStateEstimatorMILBoosting( int nFeatures = 250 ); |
| 218 | + ~TrackerStateEstimatorMILBoosting(); |
| 219 | + ... |
| 220 | + |
| 221 | + protected: |
| 222 | + Ptr<TrackerTargetState> estimateImpl( const std::vector<ConfidenceMap>& confidenceMaps ); |
| 223 | + void updateImpl( std::vector<ConfidenceMap>& confidenceMaps ); |
| 224 | + ... |
| 225 | + |
| 226 | + }; |
| 227 | + |
| 228 | +And add it in your TrackerModel : :: |
| 229 | + |
| 230 | + //model is the TrackerModel of your Tracker |
| 231 | + Ptr<TrackerStateEstimatorMILBoosting> stateEstimator = new TrackerStateEstimatorMILBoosting( params.featureSetNumFeatures ); |
| 232 | + model->setTrackerStateEstimator( stateEstimator ); |
| 233 | + |
| 234 | +.. seealso:: |
| 235 | + |
| 236 | + :ocv:class:`TrackerModel`, :ocv:class:`TrackerStateEstimatorMILBoosting`, :ocv:class:`TrackerTargetState` |
| 237 | + |
| 238 | + |
| 239 | +During this step, you should define your TrackerTargetState based on your implementation. :ocv:class:`TrackerTargetState` base class has only the bounding box (upper-left position, width and height), you can |
| 240 | +enrich it adding scale factor, target rotation, etc. |
| 241 | + |
| 242 | +Example of creating specialized TrackerTargetState ``TrackerMILTargetState`` : :: |
| 243 | + |
| 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 | + |
| 251 | + private: |
| 252 | + bool isTarget; |
| 253 | + Mat targetFeatures; |
| 254 | + ... |
| 255 | + |
| 256 | + }; |
| 257 | + |
| 258 | + |
| 259 | +Try it |
| 260 | +...... |
| 261 | + |
| 262 | +To try your tracker you can use the demo at https://github.com/lenlen/opencv/blob/tracking_api/samples/cpp/tracker.cpp. |
| 263 | + |
| 264 | +The first argument is the name of the tracker and the second is a video source. |
0 commit comments