forked from snavely/bundler_sfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBundle2Vis.cpp
217 lines (171 loc) · 5.43 KB
/
Bundle2Vis.cpp
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
/* Bundle2Vis.cpp */
/* Convert a bundle file to a PMVS vis.dat file */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include "sfm.h"
typedef std::pair<int,int> ImageKey;
typedef struct
{
double pos[3];
double color[3];
std::vector<ImageKey> views;
} point_t;
void ReadBundleFile(const char *bundle_file,
std::vector<camera_params_t> &cameras,
std::vector<point_t> &points, double &bundle_version)
{
FILE *f = fopen(bundle_file, "r");
if (f == NULL) {
printf("Error opening file %s for reading\n", bundle_file);
return;
}
int num_images, num_points;
char first_line[256];
fgets(first_line, 256, f);
if (first_line[0] == '#') {
double version;
sscanf(first_line, "# Bundle file v%lf", &version);
bundle_version = version;
printf("[ReadBundleFile] Bundle version: %0.3f\n", version);
fscanf(f, "%d %d\n", &num_images, &num_points);
} else if (first_line[0] == 'v') {
double version;
sscanf(first_line, "v%lf", &version);
bundle_version = version;
printf("[ReadBundleFile] Bundle version: %0.3f\n", version);
fscanf(f, "%d %d\n", &num_images, &num_points);
} else {
bundle_version = 0.1;
sscanf(first_line, "%d %d\n", &num_images, &num_points);
}
printf("[ReadBundleFile] Reading %d images and %d points...\n",
num_images, num_points);
// int *map = new int[num_images];
/* Read cameras */
// int count = 0;
for (int i = 0; i < num_images; i++) {
double focal_length, k0, k1;
double R[9];
double t[3];
if (bundle_version < 0.2) {
/* Focal length */
fscanf(f, "%lf\n", &focal_length);
} else {
fscanf(f, "%lf %lf %lf\n", &focal_length, &k0, &k1);
}
/* Rotation */
fscanf(f, "%lf %lf %lf\n%lf %lf %lf\n%lf %lf %lf\n",
R+0, R+1, R+2, R+3, R+4, R+5, R+6, R+7, R+8);
/* Translation */
fscanf(f, "%lf %lf %lf\n", t+0, t+1, t+2);
// if (focal_length == 0.0)
// continue;
camera_params_t cam;
cam.f = focal_length;
memcpy(cam.R, R, sizeof(double) * 9);
memcpy(cam.t, t, sizeof(double) * 3);
// map[i] = count;
// count++;
cameras.push_back(cam);
}
/* Read points */
int total_num_visible = 0;
for (int i = 0; i < num_points; i++) {
point_t pt;
/* Position */
fscanf(f, "%lf %lf %lf\n",
pt.pos + 0, pt.pos + 1, pt.pos + 2);
/* Color */
fscanf(f, "%lf %lf %lf\n",
pt.color + 0, pt.color + 1, pt.color + 2);
int num_visible;
fscanf(f, "%d", &num_visible);
total_num_visible += num_visible;
for (int j = 0; j < num_visible; j++) {
int view, key;
fscanf(f, "%d %d", &view, &key);
// pt.views.push_back(ImageKey(map[view],key));
assert(view >= 0 && view < num_images);
pt.views.push_back(ImageKey(view, key));
double x, y;
if (bundle_version >= 0.3)
fscanf(f, "%lf %lf", &x, &y);
}
if (num_visible > 0) {
points.push_back(pt);
}
}
printf("Num visible: %d\n", total_num_visible);
fclose(f);
}
void WriteVisFile(const char *vis_file,
std::vector<camera_params_t> &cameras,
std::vector<point_t> &points)
{
size_t nCameras = cameras.size();
size_t nPoints = points.size();
printf("Num cameras: %lu\n", nCameras);
FILE *f = fopen(vis_file, "w");
if (!f) {
printf("[WriteVisFile] Error opening "
"file %s for writing\n", vis_file);
return;
}
/* Fill in the matches matrix */
unsigned int *matches = new unsigned int[nCameras * nCameras];
for (size_t i = 0; i < nCameras; i++) {
for (size_t j = 0; j < nCameras; j++) {
matches[i * nCameras + j] = 0;
}
}
for (size_t i = 0; i < nPoints; i++) {
// bool seen = false;
size_t nViews = points[i].views.size();
for (size_t j = 0; j < nViews; j++) {
int i1 = points[i].views[j].first;
for (size_t k = j+1; k < nViews; k++) {
if (j == k) continue;
int i2 = points[i].views[k].first;
matches[i1 * nCameras + i2]++;
matches[i2 * nCameras + i1]++;
}
}
}
fprintf(f, "VISDATA\n");
fprintf(f, "%lu\n", nCameras);
// write camera rows
const unsigned int MATCH_THRESHOLD = 32;
for (size_t i = 0; i < nCameras; i++) {
std::vector<int> vis;
for (size_t j = 0; j < nCameras; j++) {
if (matches[i * nCameras + j] >= MATCH_THRESHOLD)
vis.push_back(j);
}
size_t nVis = vis.size();
fprintf(f, "%lu %lu", i, nVis);
for (size_t j = 0; j < nVis; j++) {
fprintf(f, " %d", vis[j]);
}
fprintf(f, "\n");
}
fclose(f);
}
int main(int argc, char **argv)
{
if (argc != 3) {
printf("Usage: %s <bundle.out> <vis.dat>\n", argv[0]);
return 1;
}
char *bundle_file = argv[1];
char *vis_file = argv[2];
/* Read the bundle file */
std::vector<camera_params_t> cameras;
std::vector<point_t> points;
double bundle_version;
ReadBundleFile(bundle_file, cameras, points, bundle_version);
WriteVisFile(vis_file, cameras, points);
return 0;
}