Skip to content

Commit 6ffdd0f

Browse files
committed
Merge pull request #1115 from sovrasov:tracking_api_update
2 parents b110661 + 3ac9e24 commit 6ffdd0f

27 files changed

+268
-447
lines changed

modules/tracking/include/opencv2/tracking/tracker.hpp

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@
4747
#include "feature.hpp"
4848
#include "onlineMIL.hpp"
4949
#include "onlineBoosting.hpp"
50-
#include <iostream>
51-
52-
53-
#define BOILERPLATE_CODE(name,classname) \
54-
static Ptr<classname> createTracker(const classname::Params &parameters=classname::Params());\
55-
virtual ~classname(){};
5650

5751
/*
5852
* Partially based on:
@@ -539,7 +533,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
539533
540534
@return True if initialization went succesfully, false otherwise
541535
*/
542-
CV_WRAP bool init( const Mat& image, const Rect2d& boundingBox );
536+
CV_WRAP bool init( InputArray image, const Rect2d& boundingBox );
543537

544538
/** @brief Update the tracker, find the new most likely bounding box for the target
545539
@param image The current frame
@@ -550,17 +544,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
550544
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
551545
missing from the frame (say, out of sight)
552546
*/
553-
CV_WRAP bool update( const Mat& image, CV_OUT Rect2d& boundingBox );
554-
555-
/** @brief Creates a tracker by its name.
556-
@param trackerType Tracker type
557-
558-
The following detector types are supported:
559-
560-
- "MIL" -- TrackerMIL
561-
- "BOOSTING" -- TrackerBoosting
562-
*/
563-
CV_WRAP static Ptr<Tracker> create( const String& trackerType );
547+
CV_WRAP bool update( InputArray image, CV_OUT Rect2d& boundingBox );
564548

565549
virtual void read( const FileNode& fn )=0;
566550
virtual void write( FileStorage& fs ) const=0;
@@ -1078,7 +1062,7 @@ based on @cite MIL .
10781062
10791063
Original code can be found here <http://vision.ucsd.edu/~bbabenko/project_miltrack.shtml>
10801064
*/
1081-
class CV_EXPORTS TrackerMIL : public Tracker
1065+
class CV_EXPORTS_W TrackerMIL : public Tracker
10821066
{
10831067
public:
10841068
struct CV_EXPORTS Params
@@ -1100,15 +1084,19 @@ class CV_EXPORTS TrackerMIL : public Tracker
11001084
/** @brief Constructor
11011085
@param parameters MIL parameters TrackerMIL::Params
11021086
*/
1103-
BOILERPLATE_CODE("MIL",TrackerMIL);
1087+
static Ptr<TrackerMIL> create(const TrackerMIL::Params &parameters);
1088+
1089+
CV_WRAP static Ptr<TrackerMIL> create();
1090+
1091+
virtual ~TrackerMIL() {}
11041092
};
11051093

11061094
/** @brief This is a real-time object tracking based on a novel on-line version of the AdaBoost algorithm.
11071095
11081096
The classifier uses the surrounding background as negative examples in update step to avoid the
11091097
drifting problem. The implementation is based on @cite OLB .
11101098
*/
1111-
class CV_EXPORTS TrackerBoosting : public Tracker
1099+
class CV_EXPORTS_W TrackerBoosting : public Tracker
11121100
{
11131101
public:
11141102
struct CV_EXPORTS Params
@@ -1133,7 +1121,11 @@ class CV_EXPORTS TrackerBoosting : public Tracker
11331121
/** @brief Constructor
11341122
@param parameters BOOSTING parameters TrackerBoosting::Params
11351123
*/
1136-
BOILERPLATE_CODE("BOOSTING",TrackerBoosting);
1124+
static Ptr<TrackerBoosting> create(const TrackerBoosting::Params &parameters);
1125+
1126+
CV_WRAP static Ptr<TrackerBoosting> create();
1127+
1128+
virtual ~TrackerBoosting() {}
11371129
};
11381130

11391131
/** @brief Median Flow tracker implementation.
@@ -1146,7 +1138,7 @@ by authors to outperform MIL). During the implementation period the code at
11461138
<http://www.aonsquared.co.uk/node/5>, the courtesy of the author Arthur Amarra, was used for the
11471139
reference purpose.
11481140
*/
1149-
class CV_EXPORTS TrackerMedianFlow : public Tracker
1141+
class CV_EXPORTS_W TrackerMedianFlow : public Tracker
11501142
{
11511143
public:
11521144
struct CV_EXPORTS Params
@@ -1168,7 +1160,11 @@ class CV_EXPORTS TrackerMedianFlow : public Tracker
11681160
/** @brief Constructor
11691161
@param parameters Median Flow parameters TrackerMedianFlow::Params
11701162
*/
1171-
BOILERPLATE_CODE("MEDIANFLOW",TrackerMedianFlow);
1163+
static Ptr<TrackerMedianFlow> create(const TrackerMedianFlow::Params &parameters);
1164+
1165+
CV_WRAP static Ptr<TrackerMedianFlow> create();
1166+
1167+
virtual ~TrackerMedianFlow() {}
11721168
};
11731169

11741170
/** @brief TLD is a novel tracking framework that explicitly decomposes the long-term tracking task into
@@ -1182,7 +1178,7 @@ The Median Flow algorithm (see cv::TrackerMedianFlow) was chosen as a tracking c
11821178
implementation, following authors. Tracker is supposed to be able to handle rapid motions, partial
11831179
occlusions, object absence etc.
11841180
*/
1185-
class CV_EXPORTS TrackerTLD : public Tracker
1181+
class CV_EXPORTS_W TrackerTLD : public Tracker
11861182
{
11871183
public:
11881184
struct CV_EXPORTS Params
@@ -1195,7 +1191,11 @@ class CV_EXPORTS TrackerTLD : public Tracker
11951191
/** @brief Constructor
11961192
@param parameters TLD parameters TrackerTLD::Params
11971193
*/
1198-
BOILERPLATE_CODE("TLD",TrackerTLD);
1194+
static Ptr<TrackerTLD> create(const TrackerTLD::Params &parameters);
1195+
1196+
CV_WRAP static Ptr<TrackerTLD> create();
1197+
1198+
virtual ~TrackerTLD() {}
11991199
};
12001200

12011201
/** @brief KCF is a novel tracking framework that utilizes properties of circulant matrix to enhance the processing speed.
@@ -1204,7 +1204,7 @@ class CV_EXPORTS TrackerTLD : public Tracker
12041204
* as well as the matlab implementation. For more information about KCF with color-names features, please refer to
12051205
* <http://www.cvl.isy.liu.se/research/objrec/visualtracking/colvistrack/index.html>.
12061206
*/
1207-
class CV_EXPORTS TrackerKCF : public Tracker
1207+
class CV_EXPORTS_W TrackerKCF : public Tracker
12081208
{
12091209
public:
12101210
/**
@@ -1251,12 +1251,16 @@ class CV_EXPORTS TrackerKCF : public Tracker
12511251
int desc_npca; //!< non-compressed descriptors of TrackerKCF::MODE
12521252
};
12531253

1254-
virtual void setFeatureExtractor(void(*)(const Mat, const Rect, Mat&), bool pca_func = false);
1254+
virtual void setFeatureExtractor(void(*)(const Mat, const Rect, Mat&), bool pca_func = false) = 0;
12551255

12561256
/** @brief Constructor
12571257
@param parameters KCF parameters TrackerKCF::Params
12581258
*/
1259-
BOILERPLATE_CODE("KCF", TrackerKCF);
1259+
static Ptr<TrackerKCF> create(const TrackerKCF::Params &parameters);
1260+
1261+
CV_WRAP static Ptr<TrackerKCF> create();
1262+
1263+
virtual ~TrackerKCF() {}
12601264
};
12611265

12621266
/** @brief GOTURN (@cite GOTURN) is kind of trackers based on Convolutional Neural Networks (CNN). While taking all advantages of CNN trackers,
@@ -1272,7 +1276,7 @@ class CV_EXPORTS TrackerKCF : public Tracker
12721276
* <https://github.com/Auron-X/GOTURN_Training_Toolkit>
12731277
* GOTURN architecture goturn.prototxt and trained model goturn.caffemodel are accessible on opencv_extra GitHub repository.
12741278
*/
1275-
class CV_EXPORTS TrackerGOTURN : public Tracker
1279+
class CV_EXPORTS_W TrackerGOTURN : public Tracker
12761280
{
12771281
public:
12781282
struct CV_EXPORTS Params
@@ -1285,24 +1289,26 @@ class CV_EXPORTS TrackerGOTURN : public Tracker
12851289
/** @brief Constructor
12861290
@param parameters GOTURN parameters TrackerGOTURN::Params
12871291
*/
1288-
BOILERPLATE_CODE("GOTURN", TrackerGOTURN);
1292+
static Ptr<TrackerGOTURN> create(const TrackerGOTURN::Params &parameters);
1293+
1294+
CV_WRAP static Ptr<TrackerGOTURN> create();
1295+
1296+
virtual ~TrackerGOTURN() {}
12891297
};
12901298

12911299
/************************************ MultiTracker Class ---By Laksono Kurnianggoro---) ************************************/
12921300
/** @brief This class is used to track multiple objects using the specified tracker algorithm.
12931301
* The MultiTracker is naive implementation of multiple object tracking.
12941302
* It process the tracked objects independently without any optimization accross the tracked objects.
12951303
*/
1296-
class CV_EXPORTS_W MultiTracker
1304+
class CV_EXPORTS_W MultiTracker : public Algorithm
12971305
{
12981306
public:
12991307

13001308
/**
13011309
* \brief Constructor.
1302-
* In the case of trackerType is given, it will be set as the default algorithm for all trackers.
1303-
* @param trackerType the name of the tracker algorithm to be used
13041310
*/
1305-
CV_WRAP MultiTracker(const String& trackerType = "");
1311+
CV_WRAP MultiTracker();
13061312

13071313
/**
13081314
* \brief Destructor
@@ -1311,58 +1317,51 @@ class CV_EXPORTS_W MultiTracker
13111317

13121318
/**
13131319
* \brief Add a new object to be tracked.
1314-
* The defaultAlgorithm will be used the newly added tracker.
1315-
* @param image input image
1316-
* @param boundingBox a rectangle represents ROI of the tracked object
1317-
*/
1318-
CV_WRAP bool add(const Mat& image, const Rect2d& boundingBox);
1319-
1320-
/**
1321-
* \brief Add a new object to be tracked.
1322-
* @param trackerType the name of the tracker algorithm to be used
1320+
*
1321+
* @param newTracker tracking algorithm to be used
13231322
* @param image input image
13241323
* @param boundingBox a rectangle represents ROI of the tracked object
13251324
*/
1326-
CV_WRAP bool add(const String& trackerType, const Mat& image, const Rect2d& boundingBox);
1325+
CV_WRAP bool add(Ptr<Tracker> newTracker, InputArray image, const Rect2d& boundingBox);
13271326

13281327
/**
13291328
* \brief Add a set of objects to be tracked.
1330-
* @param trackerType the name of the tracker algorithm to be used
1329+
* @param newTrackers list of tracking algorithms to be used
13311330
* @param image input image
13321331
* @param boundingBox list of the tracked objects
13331332
*/
1334-
CV_WRAP bool add(const String& trackerType, const Mat& image, std::vector<Rect2d> boundingBox);
1333+
bool add(std::vector<Ptr<Tracker> > newTrackers, InputArray image, std::vector<Rect2d> boundingBox);
13351334

13361335
/**
1337-
* \brief Add a set of objects to be tracked using the defaultAlgorithm tracker.
1336+
* \brief Update the current tracking status.
1337+
* The result will be saved in the internal storage.
13381338
* @param image input image
1339-
* @param boundingBox list of the tracked objects
13401339
*/
1341-
CV_WRAP bool add(const Mat& image, std::vector<Rect2d> boundingBox);
1340+
bool update(InputArray image);
13421341

13431342
/**
13441343
* \brief Update the current tracking status.
1345-
* The result will be saved in the internal storage.
13461344
* @param image input image
1345+
* @param boundingBox the tracking result, represent a list of ROIs of the tracked objects.
13471346
*/
1348-
bool update(const Mat& image);
1347+
CV_WRAP bool update(InputArray image, CV_OUT std::vector<Rect2d> & boundingBox);
13491348

1350-
//!< storage for the tracked objects, each object corresponds to one tracker algorithm.
1351-
std::vector<Rect2d> objects;
1349+
/**
1350+
* \brief Returns a reference to a storage for the tracked objects, each object corresponds to one tracker algorithm
1351+
*/
1352+
CV_WRAP const std::vector<Rect2d>& getObjects() const;
13521353

13531354
/**
1354-
* \brief Update the current tracking status.
1355-
* @param image input image
1356-
* @param boundingBox the tracking result, represent a list of ROIs of the tracked objects.
1355+
* \brief Returns a pointer to a new instance of MultiTracker
13571356
*/
1358-
CV_WRAP bool update(const Mat& image, CV_OUT std::vector<Rect2d> & boundingBox);
1357+
CV_WRAP static Ptr<MultiTracker> create();
13591358

13601359
protected:
13611360
//!< storage for the tracker algorithms.
13621361
std::vector< Ptr<Tracker> > trackerList;
13631362

1364-
//!< default algorithm for the tracking method.
1365-
String defaultAlgorithm;
1363+
//!< storage for the tracked objects, each object corresponds to one tracker algorithm.
1364+
std::vector<Rect2d> objects;
13661365
};
13671366

13681367
/************************************ Multi-Tracker Classes ---By Tyan Vladimir---************************************/
@@ -1384,11 +1383,11 @@ class CV_EXPORTS MultiTracker_Alt
13841383
/** @brief Add a new target to a tracking-list and initialize the tracker with a know bounding box that surrounding the target
13851384
@param image The initial frame
13861385
@param boundingBox The initial boundig box of target
1387-
@param tracker_algorithm_name Multi-tracker algorithm name
1386+
@param tracker_algorithm Multi-tracker algorithm
13881387
13891388
@return True if new target initialization went succesfully, false otherwise
13901389
*/
1391-
bool addTarget(const Mat& image, const Rect2d& boundingBox, String tracker_algorithm_name);
1390+
bool addTarget(InputArray image, const Rect2d& boundingBox, Ptr<Tracker> tracker_algorithm);
13921391

13931392
/** @brief Update all trackers from the tracking-list, find a new most likely bounding boxes for the targets
13941393
@param image The current frame
@@ -1397,7 +1396,7 @@ class CV_EXPORTS MultiTracker_Alt
13971396
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
13981397
missing from the frame (say, out of sight)
13991398
*/
1400-
bool update(const Mat& image);
1399+
bool update(InputArray image);
14011400

14021401
/** @brief Current number of targets in tracking-list
14031402
*/
@@ -1441,7 +1440,7 @@ class CV_EXPORTS MultiTrackerTLD : public MultiTracker_Alt
14411440
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
14421441
missing from the frame (say, out of sight)
14431442
*/
1444-
bool update_opt(const Mat& image);
1443+
bool update_opt(InputArray image);
14451444
};
14461445

14471446
//! @}

modules/tracking/perf/perf_Tracker.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ PERF_TEST_P(tracking, mil, testing::Combine(TESTSET_NAMES, SEGMENTS))
155155
bool initialized = false;
156156
vector<Rect> bbs;
157157

158-
Ptr<Tracker> tracker = Tracker::create( "MIL" );
158+
Ptr<Tracker> tracker = TrackerMIL::create();
159159
string folder = TRACKING_DIR + "/" + video + "/" + FOLDER_IMG;
160160
int numSegments = ( sizeof ( SEGMENTS)/sizeof(int) );
161161
int endFrame = 0;
@@ -226,7 +226,7 @@ PERF_TEST_P(tracking, boosting, testing::Combine(TESTSET_NAMES, SEGMENTS))
226226
bool initialized = false;
227227
vector<Rect> bbs;
228228

229-
Ptr<Tracker> tracker = Tracker::create( "BOOSTING" );
229+
Ptr<Tracker> tracker = TrackerBoosting::create();
230230
string folder = TRACKING_DIR + "/" + video + "/" + FOLDER_IMG;
231231
int numSegments = ( sizeof ( SEGMENTS)/sizeof(int) );
232232
int endFrame = 0;
@@ -296,7 +296,7 @@ PERF_TEST_P(tracking, tld, testing::Combine(TESTSET_NAMES, SEGMENTS))
296296
bool initialized = false;
297297
vector<Rect> bbs;
298298

299-
Ptr<Tracker> tracker = Tracker::create( "TLD" );
299+
Ptr<Tracker> tracker = TrackerTLD::create();
300300
string folder = TRACKING_DIR + "/" + video + "/" + FOLDER_IMG;
301301
int numSegments = ( sizeof ( SEGMENTS)/sizeof(int) );
302302
int endFrame = 0;
@@ -366,7 +366,7 @@ PERF_TEST_P(tracking, GOTURN, testing::Combine(TESTSET_NAMES, SEGMENTS))
366366
bool initialized = false;
367367
vector<Rect> bbs;
368368

369-
Ptr<Tracker> tracker = Tracker::create("GOTURN");
369+
Ptr<Tracker> tracker = TrackerGOTURN::create();
370370
string folder = TRACKING_DIR + "/" + video + "/" + FOLDER_IMG;
371371
int numSegments = (sizeof(SEGMENTS) / sizeof(int));
372372
int endFrame = 0;

modules/tracking/samples/benchmark.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "opencv2/tracking.hpp"
44
#include "opencv2/videoio.hpp"
55
#include "opencv2/plot.hpp"
6+
#include "samples_utility.hpp"
67
#include <fstream>
78
#include <iomanip>
89
#include <iostream>
@@ -75,10 +76,11 @@ const int LTRC_COUNT = 100;
7576
struct AlgoWrap
7677
{
7778
AlgoWrap(const string &name_)
78-
: tracker(Tracker::create(name_)), lastState(NotFound), name(name_), color(getNextColor()),
79+
: lastState(NotFound), name(name_), color(getNextColor()),
7980
numTotal(0), numResponse(0), numPresent(0), numCorrect_0(0), numCorrect_0_5(0),
8081
timeTotal(0), auc(LTRC_COUNT + 1, 0)
8182
{
83+
tracker = createTrackerByName(name);
8284
}
8385

8486
enum State

modules/tracking/samples/goturnTracker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int main(int argc, char *argv[])
140140
setMouseCallback("GOTURN Tracking", onMouse, 0);
141141

142142
//Create GOTURN tracker
143-
Ptr<Tracker> tracker = Tracker::create("GOTURN");
143+
Ptr<Tracker> tracker = TrackerGOTURN::create();
144144

145145
//Load and init full ALOV300++ dataset with a given datasetID, as alternative you can use loadAnnotatedOnly(..)
146146
//to load only frames with labled ground truth ~ every 5-th frame

modules/tracking/samples/kcf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int main( int argc, char** argv ){
5252
BoxExtractor box;
5353

5454
// create the tracker
55-
Ptr<Tracker> tracker = Tracker::create( "KCF" );
55+
Ptr<Tracker> tracker = TrackerKCF::create();
5656

5757
// set input video
5858
std::string video = argv[1];

modules/tracking/samples/multiTracker_dataset.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <opencv2/tracking.hpp>
4949
#include <opencv2/videoio.hpp>
5050
#include <opencv2/highgui.hpp>
51+
#include "samples_utility.hpp"
5152
#include <iostream>
5253

5354
using namespace std;
@@ -184,7 +185,7 @@ int main(int argc, char *argv[])
184185
//Initialize the tracker and add targets
185186
for (int i = 0; i < (int)boundingBoxes.size(); i++)
186187
{
187-
if (!mt.addTarget(frame, boundingBoxes[i], tracker_algorithm))
188+
if (!mt.addTarget(frame, boundingBoxes[i], createTrackerByName(tracker_algorithm)))
188189
{
189190
cout << "Trackers Init Error!!!";
190191
return 0;

0 commit comments

Comments
 (0)