-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathparse_bundler.cpp
More file actions
422 lines (331 loc) · 13 KB
/
Copy pathparse_bundler.cpp
File metadata and controls
422 lines (331 loc) · 13 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*===========================================================================*\
* *
* ACG Localizer *
* Copyright (C) 2011-2012 by Computer Graphics Group, RWTH Aachen *
* www.rwth-graphics.de *
* *
*---------------------------------------------------------------------------*
* This file is part of ACG Localizer *
* *
* ACG Localizer is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* ACG Localizer is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with ACG Localizer. If not, see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
#include "parse_bundler.hpp"
//------------------------------
parse_bundler::parse_bundler() {
mNbPoints = 0;
mNbCameras = 0;
}
//------------------------------
parse_bundler::~parse_bundler() {
for (uint32_t i = 0; i < mNbPoints; ++i) {
mFeatureInfos[i].view_list.clear();
mFeatureInfos[i].descriptors.clear();
}
mFeatureInfos.clear();
}
//------------------------------
uint32_t parse_bundler::get_number_of_points() {
return mNbPoints;
}
//------------------------------
uint32_t parse_bundler::get_number_of_cameras() {
return mNbCameras;
}
//------------------------------
void parse_bundler::get_points(std::vector<point3D> &points) {
points.reserve(mNbPoints);
points.clear();
for (uint32_t i = 0; i < mNbPoints; ++i)
points.push_back(point3D(mFeatureInfos[i].point));
}
//------------------------------
std::vector<feature_3D_info>& parse_bundler::get_feature_infos() {
return mFeatureInfos;
}
//------------------------------
std::vector<bundler_camera>& parse_bundler::get_cameras() {
return mCameras;
}
//------------------------------
bool parse_bundler::parse_data(const char* bundle_out_filename_,
const char* image_list_filename) {
////
// intialize the points and their views
for (uint32_t i = 0; i < mNbPoints; ++i) {
mFeatureInfos[i].view_list.clear();
mFeatureInfos[i].descriptors.clear();
}
mFeatureInfos.clear();
mNbCameras = mNbPoints = 0;
////
// load the Bundler file
std::cout << " Parsing " << bundle_out_filename_ << std::endl;
std::ifstream instream(bundle_out_filename_, std::ios::in);
if (!instream.is_open()) {
std::cerr << " Could not open the file " << bundle_out_filename_
<< std::endl;
return false;
}
// read the first line (containing only some information about the Bundler version)
char header[4096];
instream.getline(header, 4086, '\n');
std::cout << " header of the file: " << header << std::endl;
// get the number of cameras and the number of 3D points
instream >> mNbCameras >> mNbPoints;
mFeatureInfos.resize(mNbPoints);
mCameras.resize(mNbCameras);
// load the camera data
std::cout << " skipping " << mNbCameras << " cameras" << std::endl;
float camera_data = 0.0f;
for (uint32_t i = 0; i < mNbCameras; ++i) {
instream >> mCameras[i].focal_length >> mCameras[i].kappa_1
>> mCameras[i].kappa_2;
for (int j = 0; j < 3; ++j)
instream >> mCameras[i].rotation(j, 0) >> mCameras[i].rotation(j, 1)
>> mCameras[i].rotation(j, 2);
instream >> mCameras[i].translation[0] >> mCameras[i].translation[1]
>> mCameras[i].translation[2];
mCameras[i].id = i;
}
std::cout << " done " << std::endl;
// load the points ...
std::cout << " starting to load " << mNbPoints << " 3D points" << std::endl;
int r, g, b;
// .. and store for each camera which points ( index of the point in mFeatureInfos and the cameras id in the
// view list) it does see. This is needed to later on load the descriptors from the .key files
std::vector<std::vector<std::pair<uint32_t, uint32_t> > > cam_feature_infos(
mNbCameras);
for (uint32_t i = 0; i < mNbCameras; ++i)
cam_feature_infos[i].clear();
// read the 3D points together with their view lists
for (uint32_t i = 0; i < mNbPoints; ++i) {
// read the position
instream >> mFeatureInfos[i].point.x >> mFeatureInfos[i].point.y
>> mFeatureInfos[i].point.z;
// read the color of the point
instream >> r >> g >> b;
mFeatureInfos[i].point.r = (unsigned char) r;
mFeatureInfos[i].point.g = (unsigned char) g;
mFeatureInfos[i].point.b = (unsigned char) b;
// now, read the view list
uint32_t view_list_length;
instream >> view_list_length;
mFeatureInfos[i].view_list.resize(view_list_length);
mFeatureInfos[i].descriptors.resize(128 * view_list_length, 0);
for (uint32_t j = 0; j < view_list_length; ++j) {
instream >> mFeatureInfos[i].view_list[j].camera
>> mFeatureInfos[i].view_list[j].key
>> mFeatureInfos[i].view_list[j].x
>> mFeatureInfos[i].view_list[j].y;
cam_feature_infos[mFeatureInfos[i].view_list[j].camera].push_back(
std::make_pair(i, j));
}
}
instream.close();
std::cout << " done " << std::endl;
////
// if no file containing the filenames of the images in the reconstruction is specified, the we are done with loading
if (image_list_filename == 0)
return true;
////
// read the file containing the list of images and extract the names of the keyfiles from it
std::cout << " extracting names of the .key files from "
<< image_list_filename << std::endl;
std::vector<std::string> keyfilenames;
keyfilenames.resize(mNbCameras);
{
std::ifstream instream2(image_list_filename, std::ios::in);
if (!instream2.is_open()) {
std::cerr << " Could not open the file " << image_list_filename
<< std::endl;
return false;
}
char buffer[8192];
for (uint32_t i = 0; i < mNbCameras; ++i) {
instream2.getline(buffer, 8192, '\n');
std::string strbuffer(buffer);
std::stringstream sstream(strbuffer);
std::string tmp;
sstream >> tmp;
// replace the .jpg ending with .key
tmp.replace(tmp.size() - 3, 3, "key");
keyfilenames[i] = tmp;
}
instream2.close();
}
std::cout << " done " << std::endl;
////
// now load the key-files containing the SIFT keys one for one
std::cout << " starting to load the keypoints from " << mNbCameras
<< " many images " << std::endl;
// keep track on how many keypoints could not be found
uint32_t missing_keypoints = 0;
for (uint32_t i = 0; i < mNbCameras; ++i) {
// load the .key file for that camera
SIFT_loader key_loader;
key_loader.load_features(keyfilenames[i].c_str(), LOWE);
std::vector<unsigned char*>& descriptors = key_loader.get_descriptors();
std::vector<SIFT_keypoint>& keypoints = key_loader.get_keypoints();
// go through the descriptors and store the ones we are interested in
uint32_t nb_des = (uint32_t) cam_feature_infos[i].size();
for (uint32_t j = 0; j < nb_des; ++j) {
uint32_t feature_id = cam_feature_infos[i][j].first;
uint32_t view_list_id = cam_feature_infos[i][j].second;
uint32_t feature_descriptor_index = view_list_id * 128;
uint32_t feature_id_keyfile =
mFeatureInfos[feature_id].view_list[view_list_id].key;
// copy the descriptor
if (feature_id_keyfile >= (uint32_t) keypoints.size()) {
std::cerr << " Trying to load the descriptor for feature "
<< feature_id << " from camera " << i
<< " viewlist entry " << view_list_id
<< " feature id in the keyfile: " << feature_id_keyfile
<< " but only " << keypoints.size() << " in keyfile "
<< std::endl;
++missing_keypoints;
for (uint32_t k = 0; k < 128; ++k)
mFeatureInfos[feature_id].descriptors[feature_descriptor_index
+ k] = 0;
// set scale and orientation of the keypoint in the view list
mFeatureInfos[feature_id].view_list[view_list_id].scale = -1.0f;
mFeatureInfos[feature_id].view_list[view_list_id].orientation =
0.0f;
} else {
for (uint32_t k = 0; k < 128; ++k)
mFeatureInfos[feature_id].descriptors[feature_descriptor_index
+ k] = descriptors[feature_id_keyfile][k];
// set scale and orientation of the keypoint in the view list
mFeatureInfos[feature_id].view_list[view_list_id].scale =
keypoints[feature_id_keyfile].scale;
mFeatureInfos[feature_id].view_list[view_list_id].orientation =
keypoints[feature_id_keyfile].orientation;
}
}
// clean up the reserved space
uint32_t nb_keys_file = (uint32_t) key_loader.get_nb_features();
for (uint32_t j = 0; j < nb_keys_file; ++j) {
if (descriptors[j] != 0)
delete[] descriptors[j];
descriptors[j] = 0;
}
descriptors.clear();
keypoints.clear();
std::cout << " " << i + 1 << " / " << mNbCameras << std::endl;
}
std::cout << " Could not find " << missing_keypoints << " many keypoints"
<< std::endl;
keyfilenames.clear();
std::cout << " done " << std::endl;
return true;
}
//------------------------------
bool parse_bundler::load_from_binary(const char* filename, const int format) {
////
// initialize the data structure for the 3D points
for (uint32_t i = 0; i < mNbPoints; ++i) {
mFeatureInfos[i].view_list.clear();
mFeatureInfos[i].descriptors.clear();
}
mFeatureInfos.clear();
mNbCameras = mNbPoints = 0;
// open file for reading
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs) {
std::cerr << "Cannot read file " << filename << std::endl;
return false;
}
// read the number of cameras
ifs.read((char*) &mNbCameras, sizeof(uint32_t));
// depending on the format, cameras will be loaded
if (format == 1) {
// load the cameras
mCameras.resize(mNbCameras);
for (uint32_t i = 0; i < mNbCameras; ++i) {
double focal_length, kappa_1, kappa_2;
int32_t width, height;
double *rotation = new double[9];
double *translation = new double[3];
ifs.read((char*) &focal_length, sizeof(double));
ifs.read((char*) &kappa_1, sizeof(double));
ifs.read((char*) &kappa_2, sizeof(double));
ifs.read((char*) &width, sizeof(int32_t));
ifs.read((char*) &height, sizeof(int32_t));
ifs.read((char*) rotation, 9 * sizeof(double));
ifs.read((char*) translation, 3 * sizeof(double));
mCameras[i].focal_length = focal_length;
mCameras[i].kappa_1 = kappa_1;
mCameras[i].kappa_2 = kappa_2;
mCameras[i].width = width;
mCameras[i].height = height;
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k)
mCameras[i].rotation(j, k) = rotation[3 * j + k];
}
for (int j = 0; j < 3; ++j)
mCameras[i].translation[j] = translation[j];
delete[] rotation;
delete[] translation;
}
}
// load the points
ifs.read((char*) &mNbPoints, sizeof(uint32_t));
mFeatureInfos.resize(mNbPoints);
for (uint32_t i = 0; i < mNbPoints; ++i) {
float *pos = new float[3];
ifs.read((char*) pos, 3 * sizeof(float));
mFeatureInfos[i].point.x = pos[0];
mFeatureInfos[i].point.y = pos[1];
mFeatureInfos[i].point.z = pos[2];
delete[] pos;
pos = 0;
uint32_t size_view_list = 0;
ifs.read((char*) &size_view_list, sizeof(uint32_t));
mFeatureInfos[i].view_list.resize(size_view_list);
mFeatureInfos[i].descriptors.resize(128 * size_view_list, 0);
unsigned char *desc = new unsigned char[128];
for (uint32_t j = 0; j < size_view_list; ++j) {
float x, y, scale, orientation;
uint32_t cam_id;
ifs.read((char*) &cam_id, sizeof(uint32_t));
ifs.read((char*) &x, sizeof(float));
ifs.read((char*) &y, sizeof(float));
ifs.read((char*) &scale, sizeof(float));
ifs.read((char*) &orientation, sizeof(float));
mFeatureInfos[i].view_list[j].camera = cam_id;
mFeatureInfos[i].view_list[j].x = x;
mFeatureInfos[i].view_list[j].y = y;
mFeatureInfos[i].view_list[j].scale = scale;
mFeatureInfos[i].view_list[j].orientation = orientation;
ifs.read((char*) desc, 128 * sizeof(unsigned char));
// store the descriptor
for (uint32_t k = 0; k < 128; ++k)
mFeatureInfos[i].descriptors[128 * j + k] = desc[k];
}
delete[] desc;
}
ifs.close();
return true;
}
//------------------------------
void parse_bundler::clear() {
mNbCameras = 0;
mNbPoints = (uint32_t) mFeatureInfos.size();
for (uint32_t i = 0; i < mNbPoints; ++i) {
mFeatureInfos[i].view_list.clear();
mFeatureInfos[i].descriptors.clear();
}
mFeatureInfos.clear();
mCameras.clear();
}