Skip to content

Commit c11abeb

Browse files
committed
Merge pull request #4 from Auron-X/TLD/VOT-2015-Dataset-Support
Tld/vot 2015 dataset support
2 parents c2525ae + 94447d5 commit c11abeb

File tree

12 files changed

+1152
-347
lines changed

12 files changed

+1152
-347
lines changed

modules/tracking/include/opencv2/tracking/tldDataset.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace cv
4848
{
4949
namespace tld
5050
{
51-
CV_EXPORTS cv::Rect2d tld_InitDataset(int datasetInd, const char* rootPath = "TLD_dataset");
51+
CV_EXPORTS cv::Rect2d tld_InitDataset(int videoInd, const char* rootPath = "TLD_dataset", int datasetInd = 0);
5252
CV_EXPORTS cv::Mat tld_getNextDatasetFrame();
5353
}
5454
}

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "onlineBoosting.hpp"
5050
#include <iostream>
5151

52+
5253
#define BOILERPLATE_CODE(name,classname) \
5354
static Ptr<classname> createTracker(const classname::Params &parameters=classname::Params());\
5455
virtual ~classname(){};
@@ -564,6 +565,11 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
564565
virtual void read( const FileNode& fn )=0;
565566
virtual void write( FileStorage& fs ) const=0;
566567

568+
Ptr<TrackerModel> getModel()
569+
{
570+
return model;
571+
}
572+
567573
protected:
568574

569575
virtual bool initImpl( const Mat& image, const Rect2d& boundingBox ) = 0;
@@ -576,6 +582,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
576582
Ptr<TrackerModel> model;
577583
};
578584

585+
579586
/************************************ Specific TrackerStateEstimator Classes ************************************/
580587

581588
/** @brief TrackerStateEstimator based on Boosting
@@ -1243,6 +1250,85 @@ class CV_EXPORTS_W TrackerKCF : public Tracker
12431250
BOILERPLATE_CODE("KCF",TrackerKCF);
12441251
};
12451252

1253+
/************************************ Multi-Tracker Classes ************************************/
1254+
1255+
/** @brief Base abstract class for the long-term Multi Object Trackers:
1256+
1257+
@sa Tracker, MultiTrackerTLD
1258+
*/
1259+
class CV_EXPORTS_W MultiTracker
1260+
{
1261+
public:
1262+
/** @brief Constructor for Multitracker
1263+
*/
1264+
MultiTracker()
1265+
{
1266+
targetNum = 0;
1267+
}
1268+
1269+
/** @brief Add a new target to a tracking-list and initialize the tracker with a know bounding box that surrounding the target
1270+
@param image The initial frame
1271+
@param boundingBox The initial boundig box of target
1272+
@param tracker_algorithm_name Multi-tracker algorithm name
1273+
1274+
@return True if new target initialization went succesfully, false otherwise
1275+
*/
1276+
bool addTarget(const Mat& image, const Rect2d& boundingBox, char* tracker_algorithm_name);
1277+
1278+
/** @brief Update all trackers from the tracking-list, find a new most likely bounding boxes for the targets
1279+
@param image The current frame
1280+
1281+
@return True means that all targets were located and false means that tracker couldn't locate one of the targets in
1282+
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
1283+
missing from the frame (say, out of sight)
1284+
*/
1285+
bool update(const Mat& image);
1286+
1287+
/** @brief Current number of targets in tracking-list
1288+
*/
1289+
int targetNum;
1290+
1291+
/** @brief Trackers list for Multi-Object-Tracker
1292+
*/
1293+
std::vector <Ptr<Tracker> > trackers;
1294+
1295+
/** @brief Bounding Boxes list for Multi-Object-Tracker
1296+
*/
1297+
std::vector <Rect2d> boundingBoxes;
1298+
/** @brief List of randomly generated colors for bounding boxes display
1299+
*/
1300+
std::vector<Scalar> colors;
1301+
};
1302+
1303+
/** @brief Multi Object Tracker for TLD. TLD is a novel tracking framework that explicitly decomposes
1304+
the long-term tracking task into tracking, learning and detection.
1305+
1306+
The tracker follows the object from frame to frame. The detector localizes all appearances that
1307+
have been observed so far and corrects the tracker if necessary. The learning estimates detector’s
1308+
errors and updates it to avoid these errors in the future. The implementation is based on @cite TLD .
1309+
1310+
The Median Flow algorithm (see cv::TrackerMedianFlow) was chosen as a tracking component in this
1311+
implementation, following authors. Tracker is supposed to be able to handle rapid motions, partial
1312+
occlusions, object absence etc.
1313+
1314+
@sa Tracker, MultiTracker, TrackerTLD
1315+
*/
1316+
class CV_EXPORTS_W MultiTrackerTLD : public MultiTracker
1317+
{
1318+
public:
1319+
/** @brief Update all trackers from the tracking-list, find a new most likely bounding boxes for the targets by
1320+
optimized update method using some techniques to speedup calculations specifically for MO TLD. The only limitation
1321+
is that all target bounding boxes should have approximately same aspect ratios. Speed boost is around 20%
1322+
1323+
@param image The current frame.
1324+
1325+
@return True means that all targets were located and false means that tracker couldn't locate one of the targets in
1326+
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
1327+
missing from the frame (say, out of sight)
1328+
*/
1329+
bool update_opt(const Mat& image);
1330+
};
1331+
12461332
//! @}
12471333

12481334
} /* namespace cv */
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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+
42+
#include <opencv2/core/utility.hpp>
43+
#include <opencv2/tracking.hpp>
44+
#include <opencv2/videoio.hpp>
45+
#include <opencv2/highgui.hpp>
46+
#include <iostream>
47+
48+
using namespace std;
49+
using namespace cv;
50+
51+
#define NUM_TEST_FRAMES 100
52+
#define TEST_VIDEO_INDEX 15 //TLD Dataset Video Index from 1-10 for TLD and 1-60 for VOT
53+
//#define RECORD_VIDEO_FLG
54+
55+
static Mat image;
56+
static bool paused;
57+
static bool selectObject = false;
58+
static bool startSelection = false;
59+
Rect2d boundingBox;
60+
61+
static void onMouse(int event, int x, int y, int, void*)
62+
{
63+
if (!selectObject)
64+
{
65+
switch (event)
66+
{
67+
case EVENT_LBUTTONDOWN:
68+
//set origin of the bounding box
69+
startSelection = true;
70+
boundingBox.x = x;
71+
boundingBox.y = y;
72+
boundingBox.width = boundingBox.height = 0;
73+
break;
74+
case EVENT_LBUTTONUP:
75+
//sei with and height of the bounding box
76+
boundingBox.width = std::abs(x - boundingBox.x);
77+
boundingBox.height = std::abs(y - boundingBox.y);
78+
paused = false;
79+
selectObject = true;
80+
break;
81+
case EVENT_MOUSEMOVE:
82+
83+
if (startSelection && !selectObject)
84+
{
85+
//draw the bounding box
86+
Mat currentFrame;
87+
image.copyTo(currentFrame);
88+
rectangle(currentFrame, Point((int)boundingBox.x, (int)boundingBox.y), Point(x, y), Scalar(255, 0, 0), 2, 1);
89+
imshow("Tracking API", currentFrame);
90+
}
91+
break;
92+
}
93+
}
94+
}
95+
96+
int main()
97+
{
98+
//
99+
// "MIL", "BOOSTING", "MEDIANFLOW", "TLD"
100+
//
101+
char* tracker_algorithm_name = (char*)"TLD";
102+
103+
Mat frame;
104+
paused = false;
105+
namedWindow("Tracking API", 0);
106+
setMouseCallback("Tracking API", onMouse, 0);
107+
108+
MultiTrackerTLD mt;
109+
110+
//Get the first frame
111+
////Open the capture
112+
// VideoCapture cap(0);
113+
// if( !cap.isOpened() )
114+
// {
115+
// cout << "Video stream error";
116+
// return;
117+
// }
118+
//cap >> frame;
119+
120+
//From TLD dataset
121+
selectObject = true;
122+
Rect2d boundingBox1 = tld::tld_InitDataset(TEST_VIDEO_INDEX, "D:/opencv/VOT 2015", 1);
123+
Rect2d boundingBox2(470, 490, 50, 120);
124+
125+
frame = tld::tld_getNextDatasetFrame();
126+
frame.copyTo(image);
127+
128+
// Setup output video
129+
#ifdef RECORD_VIDEO_FLG
130+
String outputFilename = "test.avi";
131+
VideoWriter outputVideo;
132+
outputVideo.open(outputFilename, -1, 15, Size(image.cols, image.rows));
133+
134+
if (!outputVideo.isOpened())
135+
{
136+
std::cout << "!!! Output video could not be opened" << std::endl;
137+
getchar();
138+
return 0;
139+
140+
}
141+
#endif
142+
143+
rectangle(image, boundingBox, Scalar(255, 0, 0), 2, 1);
144+
imshow("Tracking API", image);
145+
146+
147+
bool initialized = false;
148+
int frameCounter = 0;
149+
150+
//Time measurment
151+
int64 e3 = getTickCount();
152+
153+
for (;;)
154+
{
155+
//Time measurment
156+
int64 e1 = getTickCount();
157+
//Frame num
158+
frameCounter++;
159+
if (frameCounter == NUM_TEST_FRAMES) break;
160+
161+
char c = (char)waitKey(2);
162+
if (c == 'q' || c == 27)
163+
break;
164+
if (c == 'p')
165+
paused = !paused;
166+
167+
if (!paused)
168+
{
169+
//cap >> frame;
170+
frame = tld::tld_getNextDatasetFrame();
171+
if (frame.empty())
172+
{
173+
break;
174+
}
175+
frame.copyTo(image);
176+
177+
if (selectObject)
178+
{
179+
if (!initialized)
180+
{
181+
//initializes the tracker
182+
mt.addTarget(frame, boundingBox1, tracker_algorithm_name);
183+
rectangle(frame, boundingBox1, mt.colors[0], 2, 1);
184+
185+
186+
mt.addTarget(frame, boundingBox2, tracker_algorithm_name);
187+
rectangle(frame, boundingBox2, mt.colors[1], 2, 1);
188+
initialized = true;
189+
}
190+
else
191+
{
192+
//updates the tracker
193+
if (mt.update(frame))
194+
{
195+
for (int i = 0; i < mt.targetNum; i++)
196+
rectangle(frame, mt.boundingBoxes[i], mt.colors[i], 2, 1);
197+
}
198+
}
199+
}
200+
imshow("Tracking API", frame);
201+
202+
#ifdef RECORD_VIDEO_FLG
203+
outputVideo << frame;
204+
#endif
205+
206+
207+
//Time measurment
208+
int64 e2 = getTickCount();
209+
double t1 = (e2 - e1) / getTickFrequency();
210+
cout << frameCounter << "\tframe : " << t1 * 1000.0 << "ms" << endl;
211+
212+
//waitKey(0);
213+
}
214+
}
215+
216+
//Time measurment
217+
int64 e4 = getTickCount();
218+
double t2 = (e4 - e3) / getTickFrequency();
219+
cout << "Average Time for Frame: " << t2 * 1000.0 / frameCounter << "ms" << endl;
220+
cout << "Average FPS: " << 1.0 / t2*frameCounter << endl;
221+
222+
223+
waitKey(0);
224+
225+
return 0;
226+
}

0 commit comments

Comments
 (0)