-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathsfm.cc
217 lines (198 loc) · 9.68 KB
/
sfm.cc
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
// Author: Paul-Edouard Sarlin (skydes)
#include "colmap/exe/sfm.h"
#include "colmap/base/camera_models.h"
#include "colmap/base/reconstruction.h"
#include "colmap/controllers/incremental_mapper.h"
#include "colmap/util/misc.h"
using namespace colmap;
#include <pybind11/iostream.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
namespace py = pybind11;
using namespace pybind11::literals;
#include "helpers.h"
#include "log_exceptions.h"
#include "pipeline/extract_features.cc"
#include "pipeline/images.cc"
#include "pipeline/match_features.cc"
Reconstruction triangulate_points(Reconstruction reconstruction,
const py::object database_path_,
const py::object image_path_,
const py::object output_path_,
const bool clear_points,
const IncrementalMapperOptions& options) {
std::string database_path = py::str(database_path_).cast<std::string>();
THROW_CHECK_FILE_EXISTS(database_path);
std::string image_path = py::str(image_path_).cast<std::string>();
THROW_CHECK_DIR_EXISTS(image_path);
std::string output_path = py::str(output_path_).cast<std::string>();
CreateDirIfNotExists(output_path);
py::gil_scoped_release release;
RunPointTriangulatorImpl(reconstruction,
database_path,
image_path,
output_path,
options,
clear_points);
return reconstruction;
}
// Copied from colmap/exe/sfm.cc
std::map<size_t, Reconstruction> incremental_mapping(
const py::object database_path_,
const py::object image_path_,
const py::object output_path_,
const IncrementalMapperOptions& options) {
std::string database_path = py::str(database_path_).cast<std::string>();
THROW_CHECK_FILE_EXISTS(database_path);
std::string image_path = py::str(image_path_).cast<std::string>();
THROW_CHECK_DIR_EXISTS(image_path);
std::string output_path = py::str(output_path_).cast<std::string>();
CreateDirIfNotExists(output_path);
py::gil_scoped_release release;
ReconstructionManager reconstruction_manager;
IncrementalMapperController mapper(
&options, image_path, database_path, &reconstruction_manager);
// In case a new reconstruction is started, write results of individual sub-
// models to as their reconstruction finishes instead of writing all results
// after all reconstructions finished.
size_t prev_num_reconstructions = 0;
std::map<size_t, Reconstruction> reconstructions;
mapper.AddCallback(
IncrementalMapperController::LAST_IMAGE_REG_CALLBACK, [&]() {
// If the number of reconstructions has not changed, the last model
// was discarded for some reason.
if (reconstruction_manager.Size() > prev_num_reconstructions) {
const std::string reconstruction_path = JoinPaths(
output_path, std::to_string(prev_num_reconstructions));
const auto& reconstruction =
reconstruction_manager.Get(prev_num_reconstructions);
CreateDirIfNotExists(reconstruction_path);
reconstruction.Write(reconstruction_path);
reconstructions[prev_num_reconstructions] = reconstruction;
prev_num_reconstructions = reconstruction_manager.Size();
}
});
PyInterrupt py_interrupt(1.0); // Check for interrupts every 2 seconds
mapper.AddCallback(IncrementalMapperController::NEXT_IMAGE_REG_CALLBACK,
[&]() {
if (py_interrupt.Raised()) {
throw py::error_already_set();
}
});
mapper.Start();
mapper.Wait();
return reconstructions;
}
std::map<size_t, Reconstruction> incremental_mapping(
const py::object database_path_,
const py::object image_path_,
const py::object output_path_,
const int num_threads,
const int min_num_matches) {
IncrementalMapperOptions options;
options.num_threads = num_threads;
options.min_num_matches = min_num_matches;
return incremental_mapping(
database_path_, image_path_, output_path_, options);
}
void init_sfm(py::module& m) {
init_images(m);
init_extract_features(m);
init_match_features(m);
using Opts = IncrementalMapperOptions;
auto PyIncrementalMapperOptions =
py::class_<Opts>(m, "IncrementalMapperOptions")
.def(py::init<>())
.def_readwrite("min_num_matches", &Opts::min_num_matches)
.def_readwrite("ignore_watermarks", &Opts::ignore_watermarks)
.def_readwrite("multiple_models", &Opts::multiple_models)
.def_readwrite("max_num_models", &Opts::max_num_models)
.def_readwrite("max_model_overlap", &Opts::max_model_overlap)
.def_readwrite("min_model_size", &Opts::min_model_size)
.def_readwrite("init_image_id1", &Opts::init_image_id1)
.def_readwrite("init_image_id2", &Opts::init_image_id2)
.def_readwrite("init_num_trials", &Opts::init_num_trials)
.def_readwrite("extract_colors", &Opts::extract_colors)
.def_readwrite("num_threads", &Opts::num_threads)
.def_readwrite("min_focal_length_ratio",
&Opts::min_focal_length_ratio)
.def_readwrite("max_focal_length_ratio",
&Opts::max_focal_length_ratio)
.def_readwrite("max_extra_param", &Opts::max_extra_param)
.def_readwrite("ba_refine_focal_length",
&Opts::ba_refine_focal_length)
.def_readwrite("ba_refine_principal_point",
&Opts::ba_refine_principal_point)
.def_readwrite("ba_refine_extra_params",
&Opts::ba_refine_extra_params)
.def_readwrite("ba_min_num_residuals_for_multi_threading",
&Opts::ba_min_num_residuals_for_multi_threading)
.def_readwrite("ba_local_num_images", &Opts::ba_local_num_images)
.def_readwrite("ba_local_function_tolerance",
&Opts::ba_local_function_tolerance)
.def_readwrite("ba_local_max_num_iterations",
&Opts::ba_local_max_num_iterations)
.def_readwrite("ba_global_use_pba", &Opts::ba_global_use_pba)
.def_readwrite("ba_global_pba_gpu_index",
&Opts::ba_global_pba_gpu_index)
.def_readwrite("ba_global_images_ratio",
&Opts::ba_global_images_ratio)
.def_readwrite("ba_global_points_ratio",
&Opts::ba_global_points_ratio)
.def_readwrite("ba_global_images_freq",
&Opts::ba_global_images_freq)
.def_readwrite("ba_global_points_freq",
&Opts::ba_global_points_freq)
.def_readwrite("ba_global_function_tolerance",
&Opts::ba_global_function_tolerance)
.def_readwrite("ba_global_max_num_iterations",
&Opts::ba_global_max_num_iterations)
.def_readwrite("ba_local_max_refinements",
&Opts::ba_local_max_refinements)
.def_readwrite("ba_local_max_refinement_change",
&Opts::ba_local_max_refinement_change)
.def_readwrite("ba_global_max_refinements",
&Opts::ba_global_max_refinements)
.def_readwrite("ba_global_max_refinement_change",
&Opts::ba_global_max_refinement_change)
.def_readwrite("snapshot_path", &Opts::snapshot_path)
.def_readwrite("snapshot_images_freq", &Opts::snapshot_images_freq)
.def_readwrite("image_names", &Opts::image_names)
.def_readwrite("fix_existing_images", &Opts::fix_existing_images);
make_dataclass(PyIncrementalMapperOptions);
auto mapper_options = PyIncrementalMapperOptions().cast<Opts>();
m.def("triangulate_points",
&triangulate_points,
py::arg("reconstruction"),
py::arg("database_path"),
py::arg("image_path"),
py::arg("output_path"),
py::arg("clear_points") = true,
py::arg("options") = mapper_options,
"Triangulate 3D points from known camera poses");
m.def("incremental_mapping",
static_cast<std::map<size_t, Reconstruction> (*)(
const py::object,
const py::object,
const py::object,
const IncrementalMapperOptions&)>(&incremental_mapping),
py::arg("database_path"),
py::arg("image_path"),
py::arg("output_path"),
py::arg("options") = mapper_options,
"Triangulate 3D points from known poses");
m.def("incremental_mapping",
static_cast<std::map<size_t, Reconstruction> (*)(const py::object,
const py::object,
const py::object,
const int,
const int)>(
&incremental_mapping),
py::arg("database_path"),
py::arg("image_path"),
py::arg("output_path"),
py::arg("num_threads") = mapper_options.num_threads,
py::arg("min_num_matches") = mapper_options.min_num_matches,
"Triangulate 3D points from known poses");
}