| 1 |
#include "colmap/exe/sfm.h" |
| 2 |
|
| 3 |
#include "colmap/controllers/bundle_adjustment.h" |
| 4 |
#include "colmap/controllers/hierarchical_pipeline.h" |
| 5 |
#include "colmap/controllers/incremental_pipeline.h" |
| 6 |
#include "colmap/estimators/view_graph_calibration.h" |
| 7 |
#include "colmap/scene/database.h" |
| 8 |
#include "colmap/scene/reconstruction.h" |
| 9 |
#include "colmap/scene/reconstruction_manager.h" |
| 10 |
#include "colmap/util/file.h" |
| 11 |
#include "colmap/util/misc.h" |
| 12 |
|
| 13 |
#include "pycolmap/helpers.h" |
| 14 |
#include "pycolmap/pybind11_extension.h" |
| 15 |
|
| 16 |
#include <filesystem> |
| 17 |
#include <memory> |
| 18 |
|
| 19 |
#include <pybind11/functional.h> |
| 20 |
#include <pybind11/pybind11.h> |
| 21 |
#include <pybind11/stl.h> |
| 22 |
|
| 23 |
using namespace colmap; |
| 24 |
using namespace pybind11::literals; |
| 25 |
namespace py = pybind11; |
| 26 |
|
| 27 |
namespace { |
| 28 |
|
| 29 |
std::map<size_t, std::shared_ptr<Reconstruction>> ReconstructionManagerToMap( |
| 30 |
const std::shared_ptr<ReconstructionManager>& reconstruction_manager) { |
| 31 |
std::map<size_t, std::shared_ptr<Reconstruction>> reconstructions; |
| 32 |
for (size_t i = 0; i < reconstruction_manager->Size(); ++i) { |
| 33 |
reconstructions[i] = reconstruction_manager->Get(i); |
| 34 |
} |
| 35 |
return reconstructions; |
| 36 |
} |
| 37 |
|
| 38 |
} // namespace |
| 39 |
|
| 40 |
std::shared_ptr<Reconstruction> TriangulatePoints( |
| 41 |
const std::shared_ptr<Reconstruction>& reconstruction, |
| 42 |
const std::filesystem::path& database_path, |
| 43 |
const std::filesystem::path& image_path, |
| 44 |
const std::filesystem::path& output_path, |
| 45 |
const bool clear_points, |
| 46 |
const IncrementalPipelineOptions& options, |
| 47 |
const bool refine_intrinsics, |
| 48 |
const std::shared_ptr<CancellationToken>& cancellation_token) { |
| 49 |
THROW_CHECK_FILE_EXISTS(database_path); |
| 50 |
THROW_CHECK_DIR_EXISTS(image_path); |
| 51 |
CreateDirIfNotExists(output_path); |
| 52 |
|
| 53 |
py::gil_scoped_release release; |
| 54 |
PyInterruptChecker interrupt_checker(cancellation_token); |
| 55 |
RunPointTriangulatorImpl(reconstruction, |
| 56 |
database_path, |
| 57 |
image_path, |
| 58 |
output_path, |
| 59 |
options, |
| 60 |
clear_points, |
| 61 |
refine_intrinsics, |
| 62 |
interrupt_checker.Callback()); |
| 63 |
interrupt_checker.CheckAndThrow(); |
| 64 |
return reconstruction; |
| 65 |
} |
| 66 |
|
| 67 |
std::map<size_t, std::shared_ptr<Reconstruction>> IncrementalMapping( |
| 68 |
const std::filesystem::path& database_path, |
| 69 |
const std::filesystem::path& image_path, |
| 70 |
const std::filesystem::path& output_path, |
| 71 |
const IncrementalPipelineOptions& options, |
| 72 |
const std::filesystem::path& input_path, |
| 73 |
std::function<void()> initial_image_pair_callback, |
| 74 |
std::function<void()> next_image_callback, |
| 75 |
const std::shared_ptr<CancellationToken>& cancellation_token) { |
| 76 |
THROW_CHECK_FILE_EXISTS(database_path); |
| 77 |
THROW_CHECK_DIR_EXISTS(image_path); |
| 78 |
CreateDirIfNotExists(output_path); |
| 79 |
|
| 80 |
py::gil_scoped_release release; |
| 81 |
auto reconstruction_manager = std::make_shared<ReconstructionManager>(); |
| 82 |
if (input_path != "") { |
| 83 |
reconstruction_manager->Read(input_path); |
| 84 |
} |
| 85 |
auto options_ = std::make_shared<IncrementalPipelineOptions>(options); |
| 86 |
|
| 87 |
PyInterruptChecker interrupt_checker(cancellation_token); |
| 88 |
auto next_image_callback_py_interruptible = |
| 89 |
[next_image_callback = std::move(next_image_callback)]() { |
| 90 |
if (next_image_callback) { |
| 91 |
next_image_callback(); |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
const bool success = |
| 96 |
RunIncrementalMapperImpl(database_path, |
| 97 |
image_path, |
| 98 |
output_path, |
| 99 |
options_, |
| 100 |
reconstruction_manager, |
| 101 |
initial_image_pair_callback, |
| 102 |
next_image_callback_py_interruptible, |
| 103 |
interrupt_checker.Callback()); |
| 104 |
|
| 105 |
interrupt_checker.CheckAndThrow(); |
| 106 |
if (!success) { |
| 107 |
return {}; |
| 108 |
} |
| 109 |
|
| 110 |
return ReconstructionManagerToMap(reconstruction_manager); |
| 111 |
} |
| 112 |
|
| 113 |
std::map<size_t, std::shared_ptr<Reconstruction>> GlobalMapping( |
| 114 |
const std::filesystem::path& database_path, |
| 115 |
const std::filesystem::path& image_path, |
| 116 |
const std::filesystem::path& output_path, |
| 117 |
GlobalPipelineOptions options) { |
| 118 |
THROW_CHECK_FILE_EXISTS(database_path); |
| 119 |
THROW_CHECK_DIR_EXISTS(image_path); |
| 120 |
CreateDirIfNotExists(output_path); |
| 121 |
|
| 122 |
py::gil_scoped_release release; |
| 123 |
auto reconstruction_manager = std::make_shared<ReconstructionManager>(); |
| 124 |
auto options_ = std::make_shared<GlobalPipelineOptions>(std::move(options)); |
| 125 |
if (!RunGlobalMapperImpl(database_path, |
| 126 |
image_path, |
| 127 |
output_path, |
| 128 |
options_, |
| 129 |
reconstruction_manager)) { |
| 130 |
return {}; |
| 131 |
} |
| 132 |
|
| 133 |
return ReconstructionManagerToMap(reconstruction_manager); |
| 134 |
} |
| 135 |
|
| 136 |
std::map<size_t, std::shared_ptr<Reconstruction>> HierarchicalMapping( |
| 137 |
const std::filesystem::path& database_path, |
| 138 |
const std::filesystem::path& image_path, |
| 139 |
const std::filesystem::path& output_path, |
| 140 |
HierarchicalPipelineOptions options) { |
| 141 |
THROW_CHECK_FILE_EXISTS(database_path); |
| 142 |
THROW_CHECK_DIR_EXISTS(image_path); |
| 143 |
CreateDirIfNotExists(output_path); |
| 144 |
|
| 145 |
py::gil_scoped_release release; |
| 146 |
auto reconstruction_manager = std::make_shared<ReconstructionManager>(); |
| 147 |
auto options_ = |
| 148 |
std::make_shared<HierarchicalPipelineOptions>(std::move(options)); |
| 149 |
if (!RunHierarchicalMapperImpl(database_path, |
| 150 |
image_path, |
| 151 |
output_path, |
| 152 |
options_, |
| 153 |
reconstruction_manager)) { |
| 154 |
return {}; |
| 155 |
} |
| 156 |
|
| 157 |
return ReconstructionManagerToMap(reconstruction_manager); |
| 158 |
} |
| 159 |
|
| 160 |
void BundleAdjustment( |
| 161 |
const std::shared_ptr<Reconstruction>& reconstruction, |
| 162 |
const BundleAdjustmentOptions& options, |
| 163 |
const std::shared_ptr<CancellationToken>& cancellation_token) { |
| 164 |
py::gil_scoped_release release; |
| 165 |
OptionManager option_manager; |
| 166 |
option_manager.bundle_adjustment = |
| 167 |
std::make_shared<BundleAdjustmentOptions>(options); |
| 168 |
PyInterruptChecker interrupt_checker(cancellation_token); |
| 169 |
BundleAdjustmentController controller(option_manager, reconstruction); |
| 170 |
controller.SetCheckIfStoppedFunc(interrupt_checker.Callback()); |
| 171 |
controller.Run(); |
| 172 |
interrupt_checker.CheckAndThrow(); |
| 173 |
} |
| 174 |
|
| 175 |
bool ViewGraphCalibration(const std::filesystem::path& database_path, |
| 176 |
const ViewGraphCalibrationOptions& options) { |
| 177 |
THROW_CHECK_FILE_EXISTS(database_path); |
| 178 |
py::gil_scoped_release release; |
| 179 |
auto database = Database::Open(database_path); |
| 180 |
return CalibrateViewGraph(options, database.get()); |
| 181 |
} |
| 182 |
|
| 183 |
void BindSfM(py::module& m) { |
| 184 |
// ViewGraphCalibrationOptions |
| 185 |
{ |
| 186 |
using Opts = ViewGraphCalibrationOptions; |
| 187 |
auto PyOpts = |
| 188 |
py::classh<Opts>(m, "ViewGraphCalibrationOptions") |
| 189 |
.def(py::init<>()) |
| 190 |
.def_readwrite("random_seed", &Opts::random_seed) |
| 191 |
.def_readwrite("cross_validate_prior_focal_lengths", |
| 192 |
&Opts::cross_validate_prior_focal_lengths) |
| 193 |
.def_readwrite("min_calibrated_pair_ratio", |
| 194 |
&Opts::min_calibrated_pair_ratio) |
| 195 |
.def_readwrite("reestimate_relative_pose", |
| 196 |
&Opts::reestimate_relative_pose) |
| 197 |
.def_readwrite("min_focal_length_ratio", |
| 198 |
&Opts::min_focal_length_ratio) |
| 199 |
.def_readwrite("max_focal_length_ratio", |
| 200 |
&Opts::max_focal_length_ratio) |
| 201 |
.def_readwrite("max_calibration_error", |
| 202 |
&Opts::max_calibration_error) |
| 203 |
.def_readwrite("loss_function_scale", &Opts::loss_function_scale) |
| 204 |
.def_readwrite("relpose_max_error", &Opts::relpose_max_error) |
| 205 |
.def_readwrite("relpose_min_num_inliers", |
| 206 |
&Opts::relpose_min_num_inliers) |
| 207 |
.def_readwrite("relpose_min_inlier_ratio", |
| 208 |
&Opts::relpose_min_inlier_ratio); |
| 209 |
MakeDataclass(PyOpts); |
| 210 |
} |
| 211 |
|
| 212 |
m.def("triangulate_points", |
| 213 |
&TriangulatePoints, |
| 214 |
"reconstruction"_a, |
| 215 |
"database_path"_a, |
| 216 |
"image_path"_a, |
| 217 |
"output_path"_a, |
| 218 |
"clear_points"_a = true, |
| 219 |
py::arg_v("options", |
| 220 |
IncrementalPipelineOptions(), |
| 221 |
"IncrementalPipelineOptions()"), |
| 222 |
"refine_intrinsics"_a = false, |
| 223 |
"cancellation_token"_a = py::none(), |
| 224 |
"Triangulate 3D points from known camera poses"); |
| 225 |
|
| 226 |
m.def("incremental_mapping", |
| 227 |
&IncrementalMapping, |
| 228 |
"database_path"_a, |
| 229 |
"image_path"_a, |
| 230 |
"output_path"_a, |
| 231 |
py::arg_v("options", |
| 232 |
IncrementalPipelineOptions(), |
| 233 |
"IncrementalPipelineOptions()"), |
| 234 |
"input_path"_a = py::str(""), |
| 235 |
"initial_image_pair_callback"_a = py::none(), |
| 236 |
"next_image_callback"_a = py::none(), |
| 237 |
"cancellation_token"_a = py::none(), |
| 238 |
"Recover 3D points and unknown camera poses"); |
| 239 |
|
| 240 |
m.def( |
| 241 |
"global_mapping", |
| 242 |
&GlobalMapping, |
| 243 |
"database_path"_a, |
| 244 |
"image_path"_a, |
| 245 |
"output_path"_a, |
| 246 |
py::arg_v("options", GlobalPipelineOptions(), "GlobalPipelineOptions()"), |
| 247 |
"Recover 3D points and camera poses using global SfM (GLOMAP)"); |
| 248 |
|
| 249 |
m.def("hierarchical_mapping", |
| 250 |
&HierarchicalMapping, |
| 251 |
"database_path"_a, |
| 252 |
"image_path"_a, |
| 253 |
"output_path"_a, |
| 254 |
py::arg_v("options", |
| 255 |
HierarchicalPipelineOptions(), |
| 256 |
"HierarchicalPipelineOptions()"), |
| 257 |
"Recover 3D points and unknown camera poses by partitioning the scene " |
| 258 |
"into overlapping clusters, reconstructing them separately using " |
| 259 |
"incremental mapping, and merging them into a globally consistent " |
| 260 |
"reconstruction"); |
| 261 |
|
| 262 |
m.def("calibrate_view_graph", |
| 263 |
&ViewGraphCalibration, |
| 264 |
"database_path"_a, |
| 265 |
py::arg_v("options", |
| 266 |
ViewGraphCalibrationOptions(), |
| 267 |
"ViewGraphCalibrationOptions()"), |
| 268 |
"Calibrate focal lengths from fundamental matrices and upgrade " |
| 269 |
"two-view geometries to CALIBRATED in the database. Run before " |
| 270 |
"global_mapping when reliable intrinsics are unavailable."); |
| 271 |
|
| 272 |
m.def("bundle_adjustment", |
| 273 |
&BundleAdjustment, |
| 274 |
"reconstruction"_a, |
| 275 |
py::arg_v( |
| 276 |
"options", BundleAdjustmentOptions(), "BundleAdjustmentOptions()"), |
| 277 |
"cancellation_token"_a = py::none(), |
| 278 |
"Jointly refine 3D points and camera poses"); |
| 279 |
} |