9 int main(int argc, char *argv[])
11 //load region, images and prepare for output
12 std::string region, images, output;
13 int visualize_delay = -1, fit_size_x = -1, fit_size_y = -1;
18 static struct option long_options[] = {
19 {"debug", no_argument, 0, 'd' },
20 {"help", no_argument, 0, 'h' },
21 {"output", required_argument, 0, 'o' },
22 {"visualize", optional_argument, 0, 'v' },
23 {"fit", optional_argument, 0, 'f' },
27 int c = getopt_long(argc, argv, "dhv::f::o:",
28 long_options, &option_index);
34 tracker.m_debug = true;
37 std::cerr << "Usage: \n"
38 << argv[0] << " [options]\n"
39 << argv[0] << " [options] <directory>\n"
40 << argv[0] << " [options] <path/to/region.txt or groundtruth.txt> <path/to/images.txt> [path/to/output.txt]\n"
42 << " --visualize | -v[delay_ms]\n"
43 << " --output | -o <output.txt>\n"
45 << " --fit | -f[WxH]\n";
52 visualize_delay = optarg ? atol(optarg) : 1;
55 std::string sizes = optarg ? optarg : "128x128";
56 std::string delimiter = "x";
57 size_t pos = sizes.find(delimiter);
58 std::string first_argument = sizes.substr(0, pos);
59 sizes.erase(0, pos + delimiter.length());
61 fit_size_x = stol(first_argument);
62 fit_size_y = stol(sizes);
67 switch (argc - optind) {
69 if (chdir(argv[optind]) == -1) {
75 region = access("groundtruth.txt", F_OK) == 0 ? "groundtruth.txt" : "region.txt";
76 images = "images.txt";
78 output = "output.txt";
83 region = std::string(argv[optind + 0]);
84 images = std::string(argv[optind + 1]);
86 if ((argc - optind) == 3)
87 output = std::string(argv[optind + 2]);
89 output = std::string(dirname(argv[optind + 0])) + "/output.txt";
93 std::cerr << "Too many arguments\n";
96 VOT vot_io(region, images, output);
98 // if groundtruth.txt is used use intersection over union (IOU) to calculate tracker accuracy
100 std::ifstream groundtruth_stream;
101 if (region.compare("groundtruth.txt") == 0) {
102 std::cout << region << std::endl;
104 groundtruth_stream.open(region.c_str());
106 std::getline(groundtruth_stream, line);
111 //img = firts frame, initPos = initial position in the first frame
112 cv::Rect init_rect = vot_io.getInitRectangle();
113 vot_io.outputBoundingBox(init_rect);
114 vot_io.getNextImage(image);
116 tracker.init(image, init_rect, fit_size_x, fit_size_y);
119 cv::Rect bb_rect, groundtruth_rect;
120 double avg_time = 0., avg_inter = 0.;
122 while (vot_io.getNextImage(image) == 1){
123 double time_profile_counter = cv::getCPUTickCount();
124 tracker.track(image);
125 time_profile_counter = cv::getCPUTickCount() - time_profile_counter;
126 std::cout << " -> speed : " << time_profile_counter/((double)cvGetTickFrequency()*1000) << "ms. per frame" << std::endl;
127 avg_time += time_profile_counter/((double)cvGetTickFrequency()*1000);
130 bb = tracker.getBBox();
131 bb_rect = cv::Rect(bb.cx - bb.w/2., bb.cy - bb.h/2., bb.w, bb.h);
132 vot_io.outputBoundingBox(bb_rect);
136 std::getline(groundtruth_stream, line);
137 std::vector<float> numbers;
138 std::istringstream s( line );
142 numbers.push_back(x);
145 double x1 = std::min(numbers[0], std::min(numbers[2], std::min(numbers[4], numbers[6])));
146 double x2 = std::max(numbers[0], std::max(numbers[2], std::max(numbers[4], numbers[6])));
147 double y1 = std::min(numbers[1], std::min(numbers[3], std::min(numbers[5], numbers[7])));
148 double y2 = std::max(numbers[1], std::max(numbers[3], std::max(numbers[5], numbers[7])));
150 groundtruth_rect = cv::Rect(x1, y1, x2-x1, y2-y1);
151 // cv::rectangle(image, groundtruth_rect, CV_RGB(255,0,0), 2);
153 double rects_intersection = (groundtruth_rect & bb_rect).area();
154 double rects_union = (groundtruth_rect | bb_rect).area();
155 double union_over_inter = rects_intersection/rects_union;
156 std::cout << " ->intersection ratio " << union_over_inter << std::endl;
157 avg_inter +=union_over_inter;
160 if (visualize_delay >= 0) {
161 cv::rectangle(image, bb_rect, CV_RGB(0,255,0), 2);
162 cv::imshow("output", image);
163 int ret = cv::waitKey(visualize_delay);
164 if (visualize_delay > 0 && ret != -1 && ret != 255)
168 // std::stringstream s;
170 // int countTmp = frames;
171 // s << "imgs" << "/img" << (countTmp/10000);
172 // countTmp = countTmp%10000;
173 // s << (countTmp/1000);
174 // countTmp = countTmp%1000;
175 // s << (countTmp/100);
176 // countTmp = countTmp%100;
177 // s << (countTmp/10);
178 // countTmp = countTmp%10;
182 // //set image output parameters
183 // std::vector<int> compression_params;
184 // compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
185 // compression_params.push_back(90);
186 // cv::imwrite(ss.c_str(), image, compression_params);
189 std::cout << "Average processing speed " << avg_time/frames << "ms. (" << 1./(avg_time/frames)*1000 << " fps)" << std::endl;
191 std::cout << "Average intersection ratio " << avg_inter/frames << std::endl;
192 groundtruth_stream.close();