]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - vot.hpp
Remove debug printf
[hercules2020/kcf.git] / vot.hpp
1 /* 
2  *  Author : Tomas Vojir
3  *  Date   : 2013-06-05
4  *  Desc   : Simple class for parsing VOT inputs and providing 
5  *           interface for image loading and storing output.
6  */ 
7
8 #ifndef CPP_VOT_H
9 #define CPP_VOT_H
10
11 #include <string>
12 #include <fstream>
13 #include <iostream>
14 #include <opencv2/opencv.hpp>
15
16
17 // Bounding box type
18 /* format:
19     2 3
20     1 4
21 */
22 typedef struct {
23     float x1;   //bottom left
24     float y1;
25     float x2;   //top left
26     float y2;
27     float x3;   //top right
28     float y3;
29     float x4;   //bottom right
30     float y4;
31 } VOTPolygon;
32
33 class VOT
34 {
35 public:
36     VOT(const std::string & region_file, const std::string & images, const std::string & ouput)
37     {
38         _images = images;
39         p_region_stream.open(region_file.c_str());
40         VOTPolygon p;
41         if (p_region_stream.is_open()){
42             std::string line;
43             std::getline(p_region_stream, line);
44             std::vector<float> numbers;
45             std::istringstream s( line );
46             float x;
47             char ch;
48             while (s >> x){
49                 numbers.push_back(x);
50                 s >> ch;
51             }
52             if (numbers.size() == 4) {
53                 float x = numbers[0], y = numbers[1], w = numbers[2], h = numbers[3];
54                 p.x1 = x;
55                 p.y1 = y + h;
56                 p.x2 = x;
57                 p.y2 = y;
58                 p.x3 = x + w;
59                 p.y3 = y;
60                 p.x4 = x + w;
61                 p.y4 = y + h;
62             } else if (numbers.size() == 8) {
63                 p.x1 = numbers[0];
64                 p.y1 = numbers[1];
65                 p.x2 = numbers[2];
66                 p.y2 = numbers[3];
67                 p.x3 = numbers[4];
68                 p.y3 = numbers[5];
69                 p.x4 = numbers[6];
70                 p.y4 = numbers[7];
71             } else {
72                 std::cerr << "Error loading initial region in file - unknow format " << region_file << "!" << std::endl;
73                 p.x1=0;
74                 p.y1=0;
75                 p.x2=0;
76                 p.y2=0;
77                 p.x3=0;
78                 p.y3=0;
79                 p.x4=0;
80                 p.y4=0;
81             }
82         }else{
83             std::cerr << "Error loading initial region in file " << region_file << "!" << std::endl;
84             p.x1=0;
85             p.y1=0;
86             p.x2=0;
87             p.y2=0;
88             p.x3=0;
89             p.y3=0;
90             p.x4=0;
91             p.y4=0;
92         }
93
94         p_init_polygon = p;
95
96         p_images_stream.open(images.c_str());
97         if (!p_images_stream.is_open())
98             std::cerr << "Error loading image file " << images << "!" << std::endl;
99
100         p_output_stream.open(ouput.c_str());
101         if (!p_output_stream.is_open())
102             std::cerr << "Error opening output file " << ouput << "!" << std::endl;
103         p_region_stream.close();
104     }
105
106     ~VOT()
107     {
108         p_images_stream.close();
109         p_output_stream.close();
110     }
111
112     inline cv::Rect getInitRectangle() const 
113     {   
114         // read init box from ground truth file
115         VOTPolygon initPolygon = getInitPolygon();
116         float x1 = std::min(initPolygon.x1, std::min(initPolygon.x2, std::min(initPolygon.x3, initPolygon.x4)));
117         float x2 = std::max(initPolygon.x1, std::max(initPolygon.x2, std::max(initPolygon.x3, initPolygon.x4)));
118         float y1 = std::min(initPolygon.y1, std::min(initPolygon.y2, std::min(initPolygon.y3, initPolygon.y4)));
119         float y2 = std::max(initPolygon.y1, std::max(initPolygon.y2, std::max(initPolygon.y3, initPolygon.y4)));
120         return cv::Rect(x1, y1, x2-x1, y2-y1);
121     }
122
123     inline VOTPolygon getInitPolygon() const 
124     {   return p_init_polygon;    }
125
126
127     inline void outputBoundingBox(const cv::Rect & bbox)
128     {
129         p_output_stream << bbox.x << "," << bbox.y << ",";
130         p_output_stream << bbox.width << "," << bbox.height << std::endl;
131     }
132
133     inline void outputPolygon(const VOTPolygon & poly)
134     {
135       p_output_stream << poly.x1 << "," << poly.y1 << ",";
136       p_output_stream << poly.x2 << "," << poly.y2 << ",";
137       p_output_stream << poly.x3 << "," << poly.y3 << ",";
138       p_output_stream << poly.x4 << "," << poly.y4 << std::endl;
139     }
140
141     inline int getNextFileName(char * fName)
142     {
143         if (p_images_stream.eof() || !p_images_stream.is_open())
144             return -1;
145         std::string line;
146         std::getline (p_images_stream, line);
147         strcpy(fName, line.c_str());
148         return 1;
149     }
150
151     inline int getNextImage(cv::Mat & img)
152     {
153         if (p_images_stream.eof() || !p_images_stream.is_open())
154                 return -1;
155
156         std::string line;
157         std::getline (p_images_stream, line);
158         if (line.empty() && p_images_stream.eof()) return -1;
159         img = cv::imread(line, CV_LOAD_IMAGE_COLOR);
160
161         return 1;
162     }
163
164 private:
165     std::string _images;
166     VOTPolygon p_init_polygon;
167     std::ifstream p_region_stream;
168     std::ifstream p_images_stream;
169     std::ofstream p_output_stream;
170
171 };
172
173 #endif //CPP_VOT_H