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/opencv_modules.hpp"
43 #include "opencv2/core.hpp"
44 #ifdef HAVE_OPENCV_DATASETS
45
46 #include "opencv2/datasets/track_vot.hpp"
47 #include <opencv2/core/utility.hpp>
48 #include <opencv2/tracking.hpp>
49 #include <opencv2/videoio.hpp>
50 #include <opencv2/highgui.hpp>
51 #include "samples_utility.hpp"
52 #include <iostream>
53
54 using namespace std;
55 using namespace cv;
56 using namespace cv::datasets;
57
58 #define NUM_TEST_FRAMES 1000
59
60 static Mat image;
61 static bool paused;
62 static bool selectObjects = false;
63 static bool startSelection = false;
64 vector<Rect2d> boundingBoxes;
65 int targetsCnt = 0;
66 int targetsNum = 0;
67 Rect2d boundingBox;
68
69 static const char* keys =
70 { "{@tracker_algorithm | | Tracker algorithm }"
71 "{@target_num |1| Number of targets }"
72 "{@dataset_path |true| Dataset path }"
73 "{@dataset_id |1| Dataset ID }"
74 };
75
onMouse(int event,int x,int y,int,void *)76 static void onMouse(int event, int x, int y, int, void*)
77 {
78 if (!selectObjects)
79 {
80 switch (event)
81 {
82 case EVENT_LBUTTONDOWN:
83 //set origin of the bounding box
84 startSelection = true;
85 boundingBox.x = x;
86 boundingBox.y = y;
87 boundingBox.width = boundingBox.height = 0;
88 break;
89 case EVENT_LBUTTONUP:
90 //sei with and height of the bounding box
91 boundingBox.width = std::abs(x - boundingBox.x);
92 boundingBox.height = std::abs(y - boundingBox.y);
93 boundingBoxes.push_back(boundingBox);
94 targetsCnt++;
95 if (targetsCnt == targetsNum)
96 {
97 paused = false;
98 selectObjects = true;
99 }
100 startSelection = false;
101 break;
102 case EVENT_MOUSEMOVE:
103
104 if (startSelection && !selectObjects)
105 {
106 //draw the bounding box
107 Mat currentFrame;
108 image.copyTo(currentFrame);
109 for (int i = 0; i < (int)boundingBoxes.size(); i++)
110 rectangle(currentFrame, boundingBoxes[i], Scalar(255, 0, 0), 2, 1);
111 rectangle(currentFrame, Point((int)boundingBox.x, (int)boundingBox.y), Point(x, y), Scalar(255, 0, 0), 2, 1);
112 imshow("Tracking API", currentFrame);
113 }
114 break;
115 }
116 }
117 }
118
help()119 static void help()
120 {
121 cout << "\nThis example shows the functionality of \"Long-term optical tracking API\""
122 "TLD dataset ID: 1~10, VOT2015 dataset ID: 1~60\n"
123 "-- pause video [p] and draw a bounding boxes around the targets to start the tracker\n"
124 "Example:\n"
125 "./example_tracking_multiTracker_dataset<tracker_algorithm> <number_of_targets> <dataset_path> <dataset_id>\n"
126 << endl;
127
128 cout << "\n\nHot keys: \n"
129 "\tq - quit the program\n"
130 "\tp - pause video\n";
131 }
132
main(int argc,char * argv[])133 int main(int argc, char *argv[])
134 {
135 CommandLineParser parser(argc, argv, keys);
136 string tracker_algorithm = parser.get<string>(0);
137 targetsNum = parser.get<int>(1);
138 string datasetRootPath = parser.get<string>(2);
139 int datasetID = parser.get<int>(3);
140 if (tracker_algorithm.empty() || datasetRootPath.empty() || targetsNum < 1)
141 {
142 help();
143 return -1;
144 }
145
146 Mat frame;
147 paused = false;
148 namedWindow("Tracking API", 0);
149 setMouseCallback("Tracking API", onMouse, 0);
150
151 legacy::MultiTrackerTLD mt;
152 //Init Dataset
153 Ptr<TRACK_vot> dataset = TRACK_vot::create();
154 dataset->load(datasetRootPath);
155 dataset->initDataset(datasetID);
156
157 //Read first frame
158 dataset->getNextFrame(frame);
159 frame.copyTo(image);
160 for (int i = 0; i < (int)boundingBoxes.size(); i++)
161 rectangle(image, boundingBoxes[i], Scalar(255, 0, 0), 2, 1);
162 imshow("Tracking API", image);
163
164 bool initialized = false;
165 paused = true;
166 int frameCounter = 0;
167
168 //Time measurment
169 int64 e3 = getTickCount();
170
171 for (;;)
172 {
173 if (!paused)
174 {
175 //Time measurment
176 int64 e1 = getTickCount();
177 if (initialized){
178 if (!dataset->getNextFrame(frame))
179 break;
180 frame.copyTo(image);
181 }
182
183 if (!initialized && selectObjects)
184 {
185 //Initialize the tracker and add targets
186 for (int i = 0; i < (int)boundingBoxes.size(); i++)
187 {
188 if (!mt.addTarget(frame, boundingBoxes[i], createTrackerByName_legacy(tracker_algorithm)))
189 {
190 cout << "Trackers Init Error!!!";
191 return 0;
192 }
193 rectangle(frame, boundingBoxes[i], mt.colors[0], 2, 1);
194 }
195 initialized = true;
196 }
197 else if (initialized)
198 {
199 //Update all targets
200 if (mt.update(frame))
201 {
202 for (int i = 0; i < mt.targetNum; i++)
203 {
204 rectangle(frame, mt.boundingBoxes[i], mt.colors[i], 2, 1);
205 }
206 }
207 }
208 imshow("Tracking API", frame);
209 frameCounter++;
210 //Time measurment
211 int64 e2 = getTickCount();
212 double t1 = (e2 - e1) / getTickFrequency();
213 cout << frameCounter << "\tframe : " << t1 * 1000.0 << "ms" << endl;
214 }
215
216 char c = (char)waitKey(2);
217 if (c == 'q')
218 break;
219 if (c == 'p')
220 paused = !paused;
221
222 //waitKey(0);
223 }
224
225 //Time measurment
226 int64 e4 = getTickCount();
227 double t2 = (e4 - e3) / getTickFrequency();
228 cout << "Average Time for Frame: " << t2 * 1000.0 / frameCounter << "ms" << endl;
229 cout << "Average FPS: " << 1.0 / t2*frameCounter << endl;
230
231
232 waitKey(0);
233
234 return 0;
235 }
236
237 #else // ! HAVE_OPENCV_DATASETS
238 #include <opencv2/core.hpp>
main()239 int main() {
240 CV_Error(cv::Error::StsNotImplemented , "this sample needs to be built with opencv_datasets !");
241 return -1;
242 }
243 #endif // HAVE_OPENCV_DATASETS
244