-
Notifications
You must be signed in to change notification settings - Fork 1
/
qing_file_reader.h
258 lines (222 loc) · 6.85 KB
/
qing_file_reader.h
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
248
249
250
251
252
253
254
255
256
257
#ifndef QING_FILE_READER
#define QING_FILE_READER
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
#include "qing_string.h"
//data's memory should be alloc first
inline void qing_read_bin_c (const string filename, float *&data, int total_size) {
FILE *fin_in = fopen(filename.c_str(), "rb");
if (0 == fin_in) {
cerr << "failed to open " << filename << endl;
return;
}
float buf;
int cnt = 0;
while (0 != (fread(&buf, sizeof(float), 1, fin_in))) {
data[cnt++] = buf;
if (cnt >= total_size) break;
}
fclose(fin_in);
}
inline void qing_read_bin (const string filename, float *&data, int total_size) {
ifstream fin(filename.c_str(), ios::in | ios::binary);
if (false == fin.is_open()) {
cerr << "error to open " << filename << endl;
return;
}
fin.read((char *) (data), sizeof(float) * total_size);
# if 0
//read in data one-by-one
for(int i = 0; i < total_size; ++i) {
fin.read((char *)(data + i), sizeof(float));
}
# endif
}
inline void qing_read_txt (const string filename, float *&data, int total_size) {
fstream fin(filename.c_str(), ios::in);
if (false == fin.is_open()) {
cerr << "error to open " << filename << endl;
return;
}
for (int i = 0; i < total_size; ++i) {
fin >> data[i];
}
fin.close();
cout << "reading " << filename << " done." << endl;
}
inline Mat qing_read_disp_txt (const string disp_file, Size disp_size) {
Mat disp_mat = Mat::zeros(disp_size, CV_32FC1);
int disp_h = disp_size.width;
int disp_w = disp_size.height;
fstream fin(disp_file.c_str(), ios::in);
if (fin.is_open() == false) {
cerr << "failed to open " << disp_file << endl;
exit(1);
}
float *ptr = disp_mat.ptr<float>(0);
for (int y = 0, idx = 0; y < disp_h; ++y) {
for (int x = 0; x < disp_w; ++x) {
fin >> ptr[idx++];
}
}
cout << "finish reading " << disp_file << endl;
return disp_mat;
}
inline void qing_read_disp_txt (const string disp_file, const int h, const int w, vector<float> &disp) {
int total_size = h * w;
disp.resize(total_size);
fstream fin(disp_file.c_str(), ios::in);
if (fin.is_open() == false) {
cerr << "failed to open " << disp_file << endl;
exit(1);
}
for (int idx = 0; idx < total_size; ++idx) {
fin >> disp[idx];
}
}
inline void qing_read_txt (const string &filename, vector<string> &contents) {
fstream fin(filename.c_str(), ios::in);
if (fin.is_open() == false) {
cerr << "failed to open " << filename << endl;
return;
}
string s;
while (fin >> s) {
contents.push_back(s);
}
#if 1
cout << "end of reading " << filename << ", " << contents.size() << " lines." << endl;
#endif
}
//read in strings in format xml
inline void qing_read_strings_xml (const string &filename, vector<string> &contents) {
contents.resize(0);
FileStorage fs(filename, FileStorage::READ);
if (!fs.isOpened())
return;
FileNode n = fs.getFirstTopLevelNode();
FileNodeIterator it = n.begin(), it_end = n.end();
for (; it != it_end; it++)
contents.push_back((string) *it);
cout << "end of reading " << filename << ", " << contents.size() << " lines." << endl;
}
inline void qing_read_intrinsic_yml (const string &filename, Mat &camera_matrix, Mat &dist_coeffs) {
FileStorage fs(filename, FileStorage::READ);
fs["Camera_Matrix"] >> camera_matrix;
fs["Distortion_Coefficients"] >> dist_coeffs;
fs.release();
}
//stereoFile, R0, R1, P0, P1, Q, imageSize
inline void qing_read_stereo_yml (const string &filename, Mat &R1, Mat &R2, Mat &P1, Mat &P2, Mat &Q, Size &imageSize) {
FileStorage fs(filename, FileStorage::READ);
if (false == fs.isOpened()) {
cerr << "failed to open " << filename << endl;
return;
}
fs["Q"] >> Q;
fs["R1"] >> R1;
fs["R2"] >> R2;
fs["P1"] >> P1;
fs["P2"] >> P2;
fs["Image_Width"] >> imageSize.width;
fs["Image_Height"] >> imageSize.height;
fs.release();
}
//stereoFile, R0, R1, P0, P1, Q, imageSize
inline void qing_read_stereo_yml_p (const string &filename, Mat &P1, Mat &P2) {
FileStorage fs(filename, FileStorage::READ);
if (false == fs.isOpened()) {
cerr << "failed to open " << filename << endl;
return;
}
fs["P1"] >> P1;
fs["P2"] >> P2;
fs.release();
}
//stereoFile, Q
inline void qing_read_stereo_yml_qmatrix (const string &filename, Mat &Q) {
FileStorage fs(filename, FileStorage::READ);
if (false == fs.isOpened()) {
cerr << "failed to open " << filename << endl;
return;
}
fs["Q"] >> Q;
fs.release();
}
//stereoFile, R, t
inline void qing_read_stereo_yml_rt (const string &filename, Mat &R, Mat &t) {
FileStorage fs(filename, FileStorage::READ);
if (false == fs.isOpened()) {
cerr << "failed to open " << filename << endl;
return;
}
fs["Rotation_Matrix"] >> R;
fs["Translation_Vector"] >> t;
fs.release();
}
//
//void qing_read_crop_infos(const string filename, vector<Point2i>& cxy, vector<Size>& csz, vector<int>& disp_ranges)
//{
//// cout << filename << endl;
// fstream fin(filename.c_str(), ios::in);
// if(fin.is_open() == false)
// {
// cerr << "failed to open " << filename << endl;
// return ;
// }
//
// string s;
// vector<string> words(0);
// while(getline(fin, s))
// {
// if(s[0] == '#') continue;
// words.clear();
// qing_split_a_string_by_space(s, words);
//
// string camName = words[0];
// int cx = qing_string_2_int(words[1]);
// int cy = qing_string_2_int(words[2]);
// int w = qing_string_2_int(words[3]);
// int h = qing_string_2_int(words[4]);
// int d = qing_string_2_int(words[5]);
//
// cxy.push_back(Point2i(cx, cy));
// csz.push_back(Size(w, h));
// disp_ranges.push_back(d);
// }
//}
//
//void qing_read_rect(const string& filename, vector<cv::Rect>& rects) {
// fstream fin(filename.c_str(), ios::in);
// if(fin.is_open() == false)
// {
// cerr << "failed to open " << filename << endl;
// return ;
// }
//
// string s;
// vector<string> words(0);
//
// while(getline(fin, s))
// {
// if(s[0] == '#') continue;
// words.clear();
// qing_split_a_string_by_space(s, words);
//
// int cx = qing_string_2_int(words[0]);
// int cy = qing_string_2_int(words[1]);
// int w = qing_string_2_int(words[2]);
// int h = qing_string_2_int(words[3]);
//
// rects.push_back(cv::Rect(cx, cy, w, h));
// }
//}
#endif // QING_FILE_READER