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 500
52+ #define TEST_VIDEO_INDEX 1 // TLD Dataset Video Index from 1-10
53+ // #define RECORD_VIDEO_FLG
54+
55+ static Mat image;
56+ static Rect2d boundingBox;
57+ static bool paused;
58+ static bool selectObject = false ;
59+ static bool startSelection = false ;
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+ string tracker_algorithm_name = " TLD" ;
102+
103+ Mat frame;
104+ paused = false ;
105+ namedWindow (" Tracking API" , 0 );
106+ setMouseCallback (" Tracking API" , onMouse, 0 );
107+
108+ Ptr<Tracker> tracker = Tracker::create (tracker_algorithm_name);
109+ if (tracker == NULL )
110+ {
111+ cout << " ***Error in the instantiation of the tracker...***\n " ;
112+ getchar ();
113+ return 0 ;
114+ }
115+
116+ // Get the first frame
117+ // //Open the capture
118+ // VideoCapture cap(0);
119+ // if( !cap.isOpened() )
120+ // {
121+ // cout << "Video stream error";
122+ // return;
123+ // }
124+ // cap >> frame;
125+
126+ // From TLD dataset
127+ selectObject = true ;
128+ boundingBox = tld::tld_InitDataset (TEST_VIDEO_INDEX, " D:/opencv/TLD_dataset" );
129+
130+ frame = tld::tld_getNextDatasetFrame ();
131+ frame.copyTo (image);
132+
133+ // Setup output video
134+ #ifdef RECORD_VIDEO_FLG
135+ String outputFilename = " test.avi" ;
136+ VideoWriter outputVideo;
137+ outputVideo.open (outputFilename, -1 , 30 , Size (image.cols , image.rows ));
138+
139+ if (!outputVideo.isOpened ())
140+ {
141+ std::cout << " !!! Output video could not be opened" << std::endl;
142+ getchar ();
143+ return ;
144+ }
145+ #endif
146+
147+ rectangle (image, boundingBox, Scalar (255 , 0 , 0 ), 2 , 1 );
148+ imshow (" Tracking API" , image);
149+
150+
151+ bool initialized = false ;
152+ int frameCounter = 0 ;
153+
154+ // Time measurment
155+ int64 e3 = getTickCount ();
156+
157+ for (;;)
158+ {
159+ // Time measurment
160+ int64 e1 = getTickCount ();
161+ // Frame num
162+ frameCounter++;
163+ if (frameCounter == NUM_TEST_FRAMES) break ;
164+
165+ char c = (char )waitKey (2 );
166+ if (c == ' q' || c == 27 )
167+ break ;
168+ if (c == ' p' )
169+ paused = !paused;
170+
171+ if (!paused)
172+ {
173+ // cap >> frame;
174+ frame = tld::tld_getNextDatasetFrame ();
175+ if (frame.empty ())
176+ {
177+ break ;
178+ }
179+ frame.copyTo (image);
180+
181+ if (selectObject)
182+ {
183+ if (!initialized)
184+ {
185+ // initializes the tracker
186+ if (!tracker->init (frame, boundingBox))
187+ {
188+ cout << " ***Could not initialize tracker...***\n " ;
189+ return 0 ;
190+ }
191+ initialized = true ;
192+ rectangle (image, boundingBox, Scalar (255 , 0 , 0 ), 2 , 1 );
193+ }
194+ else
195+ {
196+ // updates the tracker
197+ if (tracker->update (frame, boundingBox))
198+ {
199+ rectangle (image, boundingBox, Scalar (255 , 0 , 0 ), 2 , 1 );
200+ }
201+ }
202+ }
203+ imshow (" Tracking API" , image);
204+
205+ #ifdef RECORD_VIDEO_FLG
206+ outputVideo << image;
207+ #endif
208+
209+
210+ // Time measurment
211+ int64 e2 = getTickCount ();
212+ double t1 = (e2 - e1 ) / getTickFrequency ();
213+ cout << frameCounter << " \t frame : " << t1 * 1000.0 << " ms" << endl;
214+
215+ // waitKey(0);
216+ }
217+ }
218+
219+ // Time measurment
220+ int64 e4 = getTickCount ();
221+ double t2 = (e4 - e3 ) / getTickFrequency ();
222+ cout << " Average Time for Frame: " << t2 * 1000.0 / frameCounter << " ms" << endl;
223+ cout << " Average FPS: " << 1.0 / t2*frameCounter << endl;
224+
225+
226+ waitKey (0 );
227+
228+ return 0 ;
229+ }
0 commit comments