forked from bikong2/stc_tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runTracker.cpp
247 lines (222 loc) · 5.28 KB
/
runTracker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include "STCTracker.h"
/*****************************************************************************/
//command line
//1)input -b init.txt -v david.mpg, choose david video and give initial position
//2)input -v david.mpg, choose david but you should set initial position
//3)no any input, open camera and you should set initial position
/*****************************************************************************/
//-v david.mpg -b david_init.txt
#define STC 1
// Global variables
Rect box;
Rect STCbox;
bool drawing_box = false;
bool gotBB = false;
Rect boxRegion;//STC algorithm display context region
bool fromfile =false;
string video;
static double TestTime=0.f;
int frameCount=1;
void readBB(char* file) // get tracking box from file
{
ifstream tb_file (file);
string line;
getline(tb_file, line);
istringstream linestream(line);
string x1, y1, w1, h1;
getline(linestream, x1, ',');
getline(linestream, y1, ',');
getline(linestream, w1, ',');
getline(linestream, h1, ',');
int x = atoi(x1.c_str());
int y = atoi(y1.c_str());
int w = atoi(w1.c_str());
int h = atoi(h1.c_str());
box = Rect(x, y, w, h);
}
void print_help(void)
{
printf("-v source video\n-b tracking box file\n");
}
void read_options(int argc, char** argv, VideoCapture& capture)
{
for (int i=0; i<argc; i++)
{
if (strcmp(argv[i], "-b") == 0) // read tracking box from file
{
//printf("-b%d\n",i);
if (argc>i)
{
readBB(argv[i+1]);
gotBB = true;
}
else
{
print_help();
}
}
if (strcmp(argv[i], "-v") == 0) // read video from file
{
//printf("-v%d\n",i);
if (argc>i)
{
video = string(argv[i+1]);
capture.open(video);
fromfile = true;
}
else
{
print_help();
}
}
}
}
// bounding box mouse callback
void mouseHandler(int event, int x, int y, int flags, void *param){
switch( event ){
case CV_EVENT_MOUSEMOVE:
if (drawing_box){
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = Rect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( box.width < 0 ){
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 ){
box.y += box.height;
box.height *= -1;
}
gotBB = true;
break;
}
}
int main(int argc, char * argv[])
{
VideoCapture capture;
// Read options
read_options(argc, argv,capture);
Mat frame;
Mat first;
if (argc==1)
{
capture.open(0);
}
// Init camera
if (!capture.isOpened())
{
cout<<"capture device failed to open!"<<endl;
return -1;
}
//display video frame rate
float rate=capture.get(CV_CAP_PROP_FPS);
cout<<"rate="<<rate<<endl;
//
cout<<"totalframenumber="<<capture.get(CV_CAP_PROP_FRAME_COUNT)<<endl;
//Register mouse callback to draw the bounding box
cvNamedWindow("Tracker", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("Tracker", mouseHandler, NULL );
//save img path
string imgFormat=".\\imgs\\img%05d.png";
char image_name[256];
if (fromfile)
{
capture.set(CV_CAP_PROP_POS_FRAMES,0*rate);
capture >> frame;
frameCount++;
frame.copyTo(first);
//show fame num
stringstream buf;
buf << frameCount;
string num = buf.str();
putText(frame, num, Point(15, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(100, 0, 255), 3);
//show
imshow("Tracker", frame);
//save
sprintf(image_name, imgFormat.c_str(), frameCount);
imwrite(image_name,frame);
}
else
{
capture.set(CV_CAP_PROP_FRAME_WIDTH, 320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
}
// Initialization
while(!gotBB)
{
if (!fromfile)
{
capture >> frame;
}
else
{
first.copyTo(frame);
}
rectangle(frame, box, Scalar(0,0,255));
imshow("Tracker", frame);
if (cvWaitKey(33) == 'q')
{ return 0; }
}
//Remove callback
cvSetMouseCallback("Tracker", NULL, NULL );
//display pos of init box
cout<<"init x="<<box.x<<" int y="<<box.y<<endl;
#ifdef STC
STCbox=box;
//STC initialization
STCTracker stcTracker;
stcTracker.init(frame, STCbox,boxRegion);
#endif
Mat last_gray;
cvtColor(frame, last_gray, CV_RGB2GRAY);
// Run-time
Mat current_gray;
while (1)
{
capture >> frame;
if (frame.empty())
break;
#ifdef STC
double t = (double)cvGetTickCount();
// tracking
stcTracker.tracking(frame, STCbox,boxRegion,frameCount);
t = (double)cvGetTickCount() - t;
cout << "cost time: " << t / ((double)cvGetTickFrequency()*1000000.) <<" s,";
cout<<cvRound(((double)cvGetTickFrequency()*1000000.)/t)<<"FPS"<<endl;
cout<<"object size:="<<STCbox.width<<"*"<<STCbox.height<<endl;
#endif
frameCount++;
// show the result
//show frame
stringstream buf;
buf << frameCount;
string num = buf.str();
putText(frame, num, Point(15, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(100, 0, 255), 3);
#ifdef STC
//putText(frame, " STC", Point(80, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 3);
rectangle(frame, STCbox, Scalar(0, 0, 255), 3);
//rectangle(frame, boxRegion, Scalar(255, 200, 255), 3);
#endif
imshow("Tracker", frame);
sprintf(image_name, imgFormat.c_str(), frameCount);
imwrite(image_name,frame);
if ( cvWaitKey(1) == 27 )
break;
}
//wait input
//cin.get();
return 0;
}