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 << " \t frame : " << 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