10 double calcAccuracy(std::string line, cv::Rect bb_rect, cv::Rect &groundtruth_rect)
12 std::vector<float> numbers;
13 std::istringstream s(line);
21 double x1 = std::min(numbers[0], std::min(numbers[2], std::min(numbers[4], numbers[6])));
22 double x2 = std::max(numbers[0], std::max(numbers[2], std::max(numbers[4], numbers[6])));
23 double y1 = std::min(numbers[1], std::min(numbers[3], std::min(numbers[5], numbers[7])));
24 double y2 = std::max(numbers[1], std::max(numbers[3], std::max(numbers[5], numbers[7])));
26 groundtruth_rect = cv::Rect(x1, y1, x2 - x1, y2 - y1);
28 double rects_intersection = (groundtruth_rect & bb_rect).area();
29 double rects_union = (groundtruth_rect | bb_rect).area();
30 double accuracy = rects_intersection / rects_union;
35 int main(int argc, char *argv[])
37 //load region, images and prepare for output
38 std::string region, images, output;
39 int visualize_delay = -1, fit_size_x = -1, fit_size_y = -1;
44 static struct option long_options[] = {
45 {"debug", no_argument, 0, 'd' },
46 {"visual_debug", optional_argument, 0, 'p'},
47 {"help", no_argument, 0, 'h' },
48 {"output", required_argument, 0, 'o' },
49 {"visualize", optional_argument, 0, 'v' },
50 {"fit", optional_argument, 0, 'f' },
54 int c = getopt_long(argc, argv, "dp::hv::f::o:", long_options, &option_index);
60 tracker.m_debug = true;
63 if (!optarg || *optarg == 'p')
64 tracker.m_visual_debug = KCF_Tracker::vd::PATCH;
65 else if (optarg && *optarg == 'r')
66 tracker.m_visual_debug = KCF_Tracker::vd::RESPONSE;
68 fprintf(stderr, "Unknown visual debug mode: %c", *optarg);
73 std::cerr << "Usage: \n"
74 << argv[0] << " [options]\n"
75 << argv[0] << " [options] <directory>\n"
76 << argv[0] << " [options] <path/to/region.txt or groundtruth.txt> <path/to/images.txt> [path/to/output.txt]\n"
78 << " --visualize | -v[delay_ms]\n"
79 << " --output | -o <output.txt>\n"
80 << " --fit | -f[W[xH]]\n"
82 << " --visual_debug | -p [p|r]\n";
89 visualize_delay = optarg ? atol(optarg) : 1;
93 fit_size_x = fit_size_y = 0;
96 if (sscanf(optarg, "%d%c", &fit_size_x, &tail) == 1) {
97 fit_size_y = fit_size_x;
98 } else if (sscanf(optarg, "%dx%d%c", &fit_size_x, &fit_size_y, &tail) != 2) {
99 fprintf(stderr, "Cannot parse -f argument: %s\n", optarg);
107 switch (argc - optind) {
109 if (chdir(argv[optind]) == -1) {
110 perror(argv[optind]);
115 region = access("groundtruth.txt", F_OK) == 0 ? "groundtruth.txt" : "region.txt";
116 images = "images.txt";
118 output = "output.txt";
123 region = std::string(argv[optind + 0]);
124 images = std::string(argv[optind + 1]);
125 if (output.empty()) {
126 if ((argc - optind) == 3)
127 output = std::string(argv[optind + 2]);
129 output = std::string(dirname(argv[optind + 0])) + "/output.txt";
133 std::cerr << "Too many arguments\n";
136 VOT vot_io(region, images, output);
138 // if groundtruth.txt is used use intersection over union (IOU) to calculate tracker accuracy
139 std::ifstream groundtruth_stream;
140 if (region.compare("groundtruth.txt") == 0) {
141 groundtruth_stream.open(region.c_str());
143 std::getline(groundtruth_stream, line);
148 //img = firts frame, initPos = initial position in the first frame
149 cv::Rect init_rect = vot_io.getInitRectangle();
150 vot_io.outputBoundingBox(init_rect);
151 vot_io.getNextImage(image);
153 tracker.init(image, init_rect, fit_size_x, fit_size_y);
157 double avg_time = 0., sum_accuracy = 0.;
160 std::cout << std::fixed << std::setprecision(2);
162 while (vot_io.getNextImage(image) == 1){
163 double time_profile_counter = cv::getCPUTickCount();
164 tracker.track(image);
165 time_profile_counter = cv::getCPUTickCount() - time_profile_counter;
166 std::cout << " -> speed : " << time_profile_counter/((double)cvGetTickFrequency()*1000) << "ms per frame, "
167 "response : " << tracker.getFilterResponse();
168 avg_time += time_profile_counter/((double)cvGetTickFrequency()*1000);
171 bb = tracker.getBBox();
172 bb_rect = cv::Rect(bb.cx - bb.w/2., bb.cy - bb.h/2., bb.w, bb.h);
173 vot_io.outputBoundingBox(bb_rect);
175 if (groundtruth_stream.is_open()) {
177 std::getline(groundtruth_stream, line);
179 cv::Rect groundtruthRect;
180 double accuracy = calcAccuracy(line, bb_rect, groundtruthRect);
181 if (visualize_delay >= 0)
182 cv::rectangle(image, groundtruthRect, CV_RGB(255, 0,0), 1);
183 std::cout << ", accuracy: " << accuracy;
184 sum_accuracy += accuracy;
187 std::cout << std::endl;
189 if (visualize_delay >= 0) {
190 cv::Point pt(bb.cx, bb.cy);
191 cv::Size size(bb.w, bb.h);
192 cv::RotatedRect rotatedRectangle(pt, size, bb.a);
194 cv::Point2f vertices[4];
195 rotatedRectangle.points(vertices);
197 for (int i = 0; i < 4; i++)
198 cv::line(image, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 2);
199 cv::imshow("KCF output", image);
200 int ret = cv::waitKey(visualize_delay);
201 if ((visualize_delay > 0 && ret != -1 && ret < 128) ||
202 (visualize_delay == 0 && (ret == 27 /*esc*/ || ret == 'q')))
207 // std::stringstream s;
209 // int countTmp = frames;
210 // s << "imgs" << "/img" << (countTmp/10000);
211 // countTmp = countTmp%10000;
212 // s << (countTmp/1000);
213 // countTmp = countTmp%1000;
214 // s << (countTmp/100);
215 // countTmp = countTmp%100;
216 // s << (countTmp/10);
217 // countTmp = countTmp%10;
221 // //set image output parameters
222 // std::vector<int> compression_params;
223 // compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
224 // compression_params.push_back(90);
225 // cv::imwrite(ss.c_str(), image, compression_params);
228 std::cout << "Average processing speed: " << avg_time / frames << "ms (" << 1. / (avg_time / frames) * 1000 << " fps)";
229 if (groundtruth_stream.is_open()) {
230 std::cout << "; Average accuracy: " << sum_accuracy/frames << std::endl;
231 groundtruth_stream.close();
233 std::cout << std::endl;