forked from joaofaro/KCFcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntracker.cpp
More file actions
208 lines (170 loc) · 4.69 KB
/
Copy pathruntracker.cpp
File metadata and controls
208 lines (170 loc) · 4.69 KB
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "kcftracker.hpp"
#include <dirent.h>
using namespace std;
using namespace cv;
float xMin = 0;
float yMin = 0;
float height = 0;
float width = 0;
bool flag_first_bottom = true;
bool flag_bottom_finish = false;
Mat image_roi;
//set tracking roi
void onMouse(int event, int x, int y, int, void*);
int main(){
//if (argc > 5) return -1;
bool HOG = true;
bool FIXEDWINDOW = false;
bool MULTISCALE = true;
bool SILENT = true;
bool LAB = false;
// for(int i = 0; i < argc; i++){
// if ( strcmp (argv[i], "hog") == 0 )
// HOG = true;
// if ( strcmp (argv[i], "fixed_window") == 0 )
// FIXEDWINDOW = true;
// if ( strcmp (argv[i], "singlescale") == 0 )
// MULTISCALE = false;
// if ( strcmp (argv[i], "show") == 0 )
// SILENT = false;
// if ( strcmp (argv[i], "lab") == 0 ){
// LAB = true;
// HOG = true;
// }
// if ( strcmp (argv[i], "gray") == 0 )
// HOG = false;
// }
// Create KCFTracker object
KCFTracker tracker(HOG, FIXEDWINDOW, MULTISCALE, LAB);
// Frame readed
//Mat frame;
// Tracker results
Rect result;
//
Mat frame;
// // Path to list.txt
// ifstream listFile;
// string fileName = "images.txt";
// listFile.open(fileName);
// // Read groundtruth for the 1st frame
// ifstream groundtruthFile;
// string groundtruth = "region.txt";
// groundtruthFile.open(groundtruth);
// string firstLine;
// getline(groundtruthFile, firstLine);
// groundtruthFile.close();
// istringstream ss(firstLine);
// // Read groundtruth like a dumb
// float x1, y1, x2, y2, x3, y3, x4, y4;
// char ch;
// ss >> x1;
// ss >> ch;
// ss >> y1;
// ss >> ch;
// ss >> x2;
// ss >> ch;
// ss >> y2;
// ss >> ch;
// ss >> x3;
// ss >> ch;
// ss >> y3;
// ss >> ch;
// ss >> x4;
// ss >> ch;
// ss >> y4;
// Using min and max of X and Y for groundtruth rectangle
// float xMin = min(x1, min(x2, min(x3, x4)));
// float yMin = min(y1, min(y2, min(y3, y4)));
// float width = max(x1, max(x2, max(x3, x4))) - xMin;
// float height = max(y1, max(y2, max(y3, y4))) - yMin;
// // Read Images
// ifstream listFramesFile;
// string listFrames = "images.txt";
// listFramesFile.open(listFrames);
// string frameName;
// // Write Results
// ofstream resultsFile;
// string resultsPath = "output.txt";
// resultsFile.open(resultsPath);
// Frame counter
int nFrames = 0;
// open camera
VideoCapture capture;
capture.open(0);
if(!capture.isOpened()){
printf("can not open camera");
while(1);
}
//set roi xMin, yMin, width, height
//namedWindow("image_roi", 0);
capture>>image_roi;
imshow("image_roi", image_roi);
setMouseCallback("image_roi", onMouse, 0);
while(1){
capture>>image_roi;
imshow("image_roi", image_roi);
char key = waitKey(1);
if(key == 'p'){
//destroyAllWindows();
break;
}
}
printf("finished set roi\n");
while (1){
//frameName = frameName;
capture >> frame;
// Read each frame from the list
//frame = imread(frameName, CV_LOAD_IMAGE_COLOR);
// First frame, give the groundtruth to the tracker
if (nFrames == 0) {
tracker.init( Rect(xMin, yMin, width, height), frame );
rectangle( frame, Point( xMin, yMin ), Point( xMin+width, yMin+height), Scalar( 0, 255, 255 ), 1, 8 );
//resultsFile << xMin << "," << yMin << "," << width << "," << height << endl;
}
// Update
else{
result = tracker.update(frame);
rectangle( frame, Point( result.x, result.y ), Point( result.x+result.width, result.y+result.height), Scalar( 0, 255, 255 ), 1, 8 );
//resultsFile << result.x << "," << result.y << "," << result.width << "," << result.height << endl;
}
nFrames++;
imshow("Image", frame);
char key = waitKey(1);
if(key == 'b'){
break;
}
}
//resultsFile.close();
//listFile.close();
}
void onMouse(int event, int x, int y, int, void *)
{
//std::cout<<"x:"<<x<<" "<<"y:"<<y<<endl;
switch(event){
case EVENT_LBUTTONDOWN:
//set roi x,y
if(flag_first_bottom){
xMin = x;
yMin = y;
flag_first_bottom = false;
std::cout<<"x:"<<xMin<<" "<<"y:"<<yMin<<std::endl;
return;
}
case EVENT_LBUTTONUP:
//set width, height
//and destroy windows
width = x - xMin;
height = y - yMin;
flag_bottom_finish = false;
std::cout<<"width:"<<width<<" "<<"height:"<<height<<std::endl;
return ;
}
//imshow("image_roi", image_roi);
//waitKey(1);
}