| 1 |
#include "colmap/sfm/incremental_mapper.h" |
| 2 |
|
| 3 |
#include "colmap/controllers/incremental_pipeline.h" |
| 4 |
|
| 5 |
#include "pycolmap/helpers.h" |
| 6 |
#include "pycolmap/pybind11_extension.h" |
| 7 |
|
| 8 |
#include <pybind11/functional.h> |
| 9 |
#include <pybind11/pybind11.h> |
| 10 |
#include <pybind11/stl.h> |
| 11 |
#include <pybind11/stl_bind.h> |
| 12 |
|
| 13 |
using namespace colmap; |
| 14 |
using namespace pybind11::literals; |
| 15 |
namespace py = pybind11; |
| 16 |
|
| 17 |
void BindIncrementalPipeline(py::module& m) { |
| 18 |
using Opts = IncrementalPipelineOptions; |
| 19 |
auto PyOpts = py::classh<Opts>(m, "IncrementalPipelineOptions"); |
| 20 |
PyOpts.def(py::init<>()) |
| 21 |
.def_readwrite( |
| 22 |
"min_num_matches", |
| 23 |
&Opts::min_num_matches, |
| 24 |
"The minimum number of matches for inlier matches to be considered.") |
| 25 |
.def_readwrite( |
| 26 |
"ignore_watermarks", |
| 27 |
&Opts::ignore_watermarks, |
| 28 |
"Whether to ignore the inlier matches of watermark image pairs.") |
| 29 |
.def_readwrite("multiple_models", |
| 30 |
&Opts::multiple_models, |
| 31 |
"Whether to reconstruct multiple sub-models.") |
| 32 |
.def_readwrite("max_num_models", |
| 33 |
&Opts::max_num_models, |
| 34 |
"The number of sub-models to reconstruct.") |
| 35 |
.def_readwrite( |
| 36 |
"max_model_overlap", |
| 37 |
&Opts::max_model_overlap, |
| 38 |
"The maximum number of overlapping images between sub-models. If the " |
| 39 |
"current sub-models shares more than this number of images with " |
| 40 |
"another model, then the reconstruction is stopped.") |
| 41 |
.def_readwrite("min_model_size", |
| 42 |
&Opts::min_model_size, |
| 43 |
"The minimum number of registered images of a sub-model, " |
| 44 |
"otherwise the sub-model is discarded. Note that the " |
| 45 |
"first sub-model is always kept independent of size. If " |
| 46 |
"the model contains at least half of the total number of " |
| 47 |
"images, we also always keep it.") |
| 48 |
.def_readwrite("init_image_id1", |
| 49 |
&Opts::init_image_id1, |
| 50 |
"The image identifier of the first image used to " |
| 51 |
"initialize the reconstruction.") |
| 52 |
.def_readwrite( |
| 53 |
"init_image_id2", |
| 54 |
&Opts::init_image_id2, |
| 55 |
"The image identifier of the second image used to initialize the " |
| 56 |
"reconstruction. Determined automatically if left unspecified.") |
| 57 |
.def_readwrite("init_num_trials", |
| 58 |
&Opts::init_num_trials, |
| 59 |
"The number of trials to initialize the reconstruction.") |
| 60 |
.def_readwrite("structure_less_registration_fallback", |
| 61 |
&Opts::structure_less_registration_fallback, |
| 62 |
"Enable fallback to structure-less image registration " |
| 63 |
"using 2D-2D correspondences, if structured-based " |
| 64 |
"registration fails using 2D-3D correspondences.") |
| 65 |
.def_readwrite("structure_less_registration_only", |
| 66 |
&Opts::structure_less_registration_only, |
| 67 |
"Only use structure-less and skip structure-based image " |
| 68 |
"registration.") |
| 69 |
.def_readwrite("extract_colors", |
| 70 |
&Opts::extract_colors, |
| 71 |
"Whether to extract colors for reconstructed points.") |
| 72 |
.def_readwrite("num_threads", |
| 73 |
&Opts::num_threads, |
| 74 |
"The number of threads to use during reconstruction.") |
| 75 |
.def_readwrite( |
| 76 |
"random_seed", |
| 77 |
&Opts::random_seed, |
| 78 |
"PRNG seed for all stochastic methods during reconstruction.") |
| 79 |
.def_readwrite("min_focal_length_ratio", |
| 80 |
&Opts::min_focal_length_ratio, |
| 81 |
"The threshold used to filter and ignore images with " |
| 82 |
"degenerate intrinsics.") |
| 83 |
.def_readwrite("max_focal_length_ratio", |
| 84 |
&Opts::max_focal_length_ratio, |
| 85 |
"The threshold used to filter and ignore images with " |
| 86 |
"degenerate intrinsics.") |
| 87 |
.def_readwrite("max_extra_param", |
| 88 |
&Opts::max_extra_param, |
| 89 |
"The threshold used to filter and ignore images with " |
| 90 |
"degenerate intrinsics.") |
| 91 |
.def_readwrite( |
| 92 |
"ba_refine_focal_length", |
| 93 |
&Opts::ba_refine_focal_length, |
| 94 |
"Whether to refine the focal length during the reconstruction.") |
| 95 |
.def_readwrite( |
| 96 |
"ba_refine_principal_point", |
| 97 |
&Opts::ba_refine_principal_point, |
| 98 |
"Whether to refine the principal point during the reconstruction.") |
| 99 |
.def_readwrite( |
| 100 |
"ba_refine_extra_params", |
| 101 |
&Opts::ba_refine_extra_params, |
| 102 |
"Whether to refine extra parameters during the reconstruction.") |
| 103 |
.def_readwrite("ba_refine_sensor_from_rig", |
| 104 |
&Opts::ba_refine_sensor_from_rig, |
| 105 |
"Whether to refine rig poses during the reconstruction.") |
| 106 |
.def_readwrite( |
| 107 |
"ba_min_num_residuals_for_cpu_multi_threading", |
| 108 |
&Opts::ba_min_num_residuals_for_cpu_multi_threading, |
| 109 |
"The minimum number of residuals per bundle adjustment problem to " |
| 110 |
"enable multi-threading solving of the problems.") |
| 111 |
.def_readwrite( |
| 112 |
"ba_local_function_tolerance", |
| 113 |
&Opts::ba_local_function_tolerance, |
| 114 |
"Ceres solver function tolerance for local bundle adjustment.") |
| 115 |
.def_readwrite( |
| 116 |
"ba_local_max_num_iterations", |
| 117 |
&Opts::ba_local_max_num_iterations, |
| 118 |
"The maximum number of local bundle adjustment iterations. If -1, " |
| 119 |
"the default of the configured bundle adjustment backend is used.") |
| 120 |
.def_readwrite( |
| 121 |
"ba_global_frames_ratio", |
| 122 |
&Opts::ba_global_frames_ratio, |
| 123 |
"The growth rates after which to perform global bundle adjustment.") |
| 124 |
.def_readwrite( |
| 125 |
"ba_global_points_ratio", |
| 126 |
&Opts::ba_global_points_ratio, |
| 127 |
"The growth rates after which to perform global bundle adjustment.") |
| 128 |
.def_readwrite( |
| 129 |
"ba_global_frames_freq", |
| 130 |
&Opts::ba_global_frames_freq, |
| 131 |
"The growth rates after which to perform global bundle adjustment.") |
| 132 |
.def_readwrite( |
| 133 |
"ba_global_points_freq", |
| 134 |
&Opts::ba_global_points_freq, |
| 135 |
"The growth rates after which to perform global bundle adjustment.") |
| 136 |
.def_readwrite( |
| 137 |
"ba_global_function_tolerance", |
| 138 |
&Opts::ba_global_function_tolerance, |
| 139 |
"Ceres solver function tolerance for global bundle adjustment.") |
| 140 |
.def_readwrite( |
| 141 |
"ba_global_max_num_iterations", |
| 142 |
&Opts::ba_global_max_num_iterations, |
| 143 |
"The maximum number of global bundle adjustment iterations. If -1, " |
| 144 |
"the default of the configured bundle adjustment backend is used.") |
| 145 |
.def_readwrite( |
| 146 |
"ba_local_max_refinements", |
| 147 |
&Opts::ba_local_max_refinements, |
| 148 |
"The thresholds for iterative bundle adjustment refinements.") |
| 149 |
.def_readwrite( |
| 150 |
"ba_local_max_refinement_change", |
| 151 |
&Opts::ba_local_max_refinement_change, |
| 152 |
"The thresholds for iterative bundle adjustment refinements.") |
| 153 |
.def_readwrite( |
| 154 |
"ba_global_max_refinements", |
| 155 |
&Opts::ba_global_max_refinements, |
| 156 |
"The thresholds for iterative bundle adjustment refinements.") |
| 157 |
.def_readwrite( |
| 158 |
"ba_global_max_refinement_change", |
| 159 |
&Opts::ba_global_max_refinement_change, |
| 160 |
"The thresholds for iterative bundle adjustment refinements.") |
| 161 |
.def_readwrite("ba_use_gpu", |
| 162 |
&IncrementalPipelineOptions::ba_use_gpu, |
| 163 |
"Whether to use Ceres' CUDA sparse linear algebra " |
| 164 |
"library, if available.") |
| 165 |
.def_readwrite("ba_gpu_index", |
| 166 |
&IncrementalPipelineOptions::ba_gpu_index, |
| 167 |
"Index of CUDA GPU to use for BA, if available.") |
| 168 |
.def_readwrite("ba_local_backend", |
| 169 |
&Opts::ba_local_backend, |
| 170 |
"Bundle adjustment solver backend for local bundle " |
| 171 |
"adjustment.") |
| 172 |
.def_readwrite("ba_global_backend", |
| 173 |
&Opts::ba_global_backend, |
| 174 |
"Bundle adjustment solver backend for global bundle " |
| 175 |
"adjustment.") |
| 176 |
.def_readwrite("use_prior_position", |
| 177 |
&Opts::use_prior_position, |
| 178 |
"Whether to use priors on the camera positions.") |
| 179 |
.def_readwrite("use_robust_loss_on_prior_position", |
| 180 |
&Opts::use_robust_loss_on_prior_position, |
| 181 |
"Whether to use a robust loss on prior camera positions.") |
| 182 |
.def_readwrite("prior_position_loss_scale", |
| 183 |
&Opts::prior_position_loss_scale, |
| 184 |
"Threshold on the residual for the robust position prior " |
| 185 |
"loss (chi2 for 3DOF at 95% = 7.815).") |
| 186 |
.def_readwrite("snapshot_path", |
| 187 |
&Opts::snapshot_path, |
| 188 |
"Path to a folder in which reconstruction snapshots will " |
| 189 |
"be saved during incremental reconstruction.") |
| 190 |
.def_readwrite("snapshot_frames_freq", |
| 191 |
&Opts::snapshot_frames_freq, |
| 192 |
"Frequency of registered images according to which " |
| 193 |
"reconstruction snapshots will be saved.") |
| 194 |
.def_readwrite( |
| 195 |
"image_path", |
| 196 |
&Opts::image_path, |
| 197 |
"The image path at which to find the images to extract point colors.") |
| 198 |
.def_readwrite( |
| 199 |
"image_names", |
| 200 |
&Opts::image_names, |
| 201 |
"Optional list of image names to reconstruct. If no images are " |
| 202 |
"specified, all images will be reconstructed by default.") |
| 203 |
.def_readwrite( |
| 204 |
"load_all_images", |
| 205 |
&Opts::load_all_images, |
| 206 |
"Whether to load all images from the database, including those " |
| 207 |
"without correspondences. Only useful for triangulation.") |
| 208 |
.def_readwrite("fix_existing_frames", |
| 209 |
&Opts::fix_existing_frames, |
| 210 |
"If reconstruction is provided as input, fix the existing " |
| 211 |
"frame poses.") |
| 212 |
.def_readwrite( |
| 213 |
"constant_rigs", |
| 214 |
&Opts::constant_rigs, |
| 215 |
"List of rigs for which to fix the sensor_from_rig transformation, " |
| 216 |
"independent of ba_refine_sensor_from_rig.") |
| 217 |
.def_readwrite("constant_cameras", |
| 218 |
&Opts::constant_cameras, |
| 219 |
"List of cameras for which to fix the camera parameters " |
| 220 |
"independent of refine_focal_length, " |
| 221 |
"refine_principal_point, and refine_extra_params.") |
| 222 |
.def_readwrite( |
| 223 |
"max_runtime_seconds", |
| 224 |
&Opts::max_runtime_seconds, |
| 225 |
"Maximum runtime in seconds for the reconstruction process. If set " |
| 226 |
"to a non-positive value, the process will run until completion.") |
| 227 |
.def_readwrite( |
| 228 |
"mapper", &Opts::mapper, "Options of the IncrementalMapper.") |
| 229 |
.def_readwrite("triangulation", |
| 230 |
&Opts::triangulation, |
| 231 |
"Options of the IncrementalTriangulator.") |
| 232 |
.def("get_mapper", |
| 233 |
&Opts::Mapper, |
| 234 |
"Get mapper options with shared settings applied.") |
| 235 |
.def("get_triangulation", |
| 236 |
&Opts::Triangulation, |
| 237 |
"Get triangulation options with shared settings applied.") |
| 238 |
.def("get_local_bundle_adjustment", |
| 239 |
&Opts::LocalBundleAdjustment, |
| 240 |
"Get local bundle adjustment options.") |
| 241 |
.def("get_global_bundle_adjustment", |
| 242 |
&Opts::GlobalBundleAdjustment, |
| 243 |
"Get global bundle adjustment options.") |
| 244 |
.def("is_initial_pair_provided", |
| 245 |
&Opts::IsInitialPairProvided, |
| 246 |
"Check whether both initial image identifiers are provided.") |
| 247 |
.def("check", &Opts::Check); |
| 248 |
MakeDataclass(PyOpts); |
| 249 |
|
| 250 |
using CallbackType = IncrementalPipeline::CallbackType; |
| 251 |
auto PyCallbackType = |
| 252 |
py::enum_<CallbackType>(m, "IncrementalPipelineCallback") |
| 253 |
.value("INITIAL_IMAGE_PAIR_REG_CALLBACK", |
| 254 |
CallbackType::INITIAL_IMAGE_PAIR_REG_CALLBACK) |
| 255 |
.value("NEXT_IMAGE_REG_CALLBACK", |
| 256 |
CallbackType::NEXT_IMAGE_REG_CALLBACK) |
| 257 |
.value("LAST_IMAGE_REG_CALLBACK", |
| 258 |
CallbackType::LAST_IMAGE_REG_CALLBACK); |
| 259 |
AddStringToEnumConstructor(PyCallbackType); |
| 260 |
|
| 261 |
using Status = IncrementalPipeline::Status; |
| 262 |
auto PyStatus = |
| 263 |
py::enum_<Status>(m, "IncrementalPipelineStatus") |
| 264 |
.value("SUCCESS", Status::SUCCESS) |
| 265 |
.value("INTERRUPTED", Status::INTERRUPTED) |
| 266 |
.value("CONTINUE", Status::CONTINUE) |
| 267 |
.value("STOP", Status::STOP) |
| 268 |
.value("UNKNOWN_SENSOR_FROM_RIG", Status::UNKNOWN_SENSOR_FROM_RIG) |
| 269 |
.value("NO_INITIAL_PAIR", Status::NO_INITIAL_PAIR) |
| 270 |
.value("BAD_INITIAL_PAIR", Status::BAD_INITIAL_PAIR); |
| 271 |
AddStringToEnumConstructor(PyStatus); |
| 272 |
|
| 273 |
py::classh<IncrementalPipeline>( |
| 274 |
m, |
| 275 |
"IncrementalPipeline", |
| 276 |
"Class that controls the incremental mapping procedure by iteratively " |
| 277 |
"initializing reconstructions from the same scene graph.") |
| 278 |
.def(py::init<std::shared_ptr<IncrementalPipelineOptions>, |
| 279 |
std::shared_ptr<Database>, |
| 280 |
std::shared_ptr<ReconstructionManager>>(), |
| 281 |
"options"_a, |
| 282 |
"database"_a, |
| 283 |
"reconstruction_manager"_a) |
| 284 |
.def(py::init<std::shared_ptr<IncrementalPipelineOptions>, |
| 285 |
std::shared_ptr<DatabaseCache>, |
| 286 |
std::shared_ptr<ReconstructionManager>>(), |
| 287 |
"options"_a, |
| 288 |
"database_cache"_a, |
| 289 |
"reconstruction_manager"_a) |
| 290 |
.def_property_readonly("options", &IncrementalPipeline::Options) |
| 291 |
.def_property_readonly("reconstruction_manager", |
| 292 |
&IncrementalPipeline::ReconstructionManager) |
| 293 |
.def_property_readonly("database_cache", |
| 294 |
&IncrementalPipeline::DatabaseCache) |
| 295 |
.def("add_callback", |
| 296 |
&IncrementalPipeline::AddCallback, |
| 297 |
"id"_a, |
| 298 |
"func"_a, |
| 299 |
"Add a callback function for the given callback type.") |
| 300 |
.def("callback", |
| 301 |
&IncrementalPipeline::Callback, |
| 302 |
"id"_a, |
| 303 |
"Invoke the callback for the given callback type.") |
| 304 |
.def("reconstruct", |
| 305 |
&IncrementalPipeline::Reconstruct, |
| 306 |
"mapper"_a, |
| 307 |
"mapper_options"_a, |
| 308 |
"continue_reconstruction"_a, |
| 309 |
"Reconstruct the scene using the given mapper and options.") |
| 310 |
.def("reconstruct_sub_model", |
| 311 |
&IncrementalPipeline::ReconstructSubModel, |
| 312 |
"mapper"_a, |
| 313 |
"mapper_options"_a, |
| 314 |
"reconstruction"_a, |
| 315 |
"Reconstruct a sub-model using the given mapper and options.") |
| 316 |
.def("initialize_reconstruction", |
| 317 |
&IncrementalPipeline::InitializeReconstruction, |
| 318 |
"mapper"_a, |
| 319 |
"mapper_options"_a, |
| 320 |
"reconstruction"_a, |
| 321 |
"Initialize the reconstruction by finding and registering an " |
| 322 |
"initial image pair.") |
| 323 |
.def("run", |
| 324 |
&IncrementalPipeline::Run, |
| 325 |
"Run the full incremental mapping pipeline.") |
| 326 |
.def("check_run_global_refinement", |
| 327 |
&IncrementalPipeline::CheckRunGlobalRefinement, |
| 328 |
"reconstruction"_a, |
| 329 |
"ba_prev_num_reg_images"_a, |
| 330 |
"ba_prev_num_points"_a, |
| 331 |
"Check whether global bundle adjustment should be run based on " |
| 332 |
"the growth of registered images and points.") |
| 333 |
.def("check_reached_max_runtime", |
| 334 |
&IncrementalPipeline::CheckReachedMaxRuntime, |
| 335 |
"Check whether the maximum runtime has been reached."); |
| 336 |
} |
| 337 |
|
| 338 |
void BindIncrementalMapperOptions(py::module& m) { |
| 339 |
using ImageSelection = IncrementalMapper::Options::ImageSelectionMethod; |
| 340 |
auto PyImageSelectionMethod = |
| 341 |
py::enum_<ImageSelection>(m, "ImageSelectionMethod") |
| 342 |
.value("MAX_VISIBLE_POINTS_NUM", |
| 343 |
ImageSelection::MAX_VISIBLE_POINTS_NUM) |
| 344 |
.value("MAX_VISIBLE_POINTS_RATIO", |
| 345 |
ImageSelection::MAX_VISIBLE_POINTS_RATIO) |
| 346 |
.value("MIN_UNCERTAINTY", ImageSelection::MIN_UNCERTAINTY); |
| 347 |
AddStringToEnumConstructor(PyImageSelectionMethod); |
| 348 |
|
| 349 |
using Opts = IncrementalMapper::Options; |
| 350 |
auto PyOpts = py::classh<Opts>(m, "IncrementalMapperOptions"); |
| 351 |
PyOpts.def(py::init<>()) |
| 352 |
.def_readwrite("init_min_num_inliers", |
| 353 |
&Opts::init_min_num_inliers, |
| 354 |
"Minimum number of inliers for initial image pair.") |
| 355 |
.def_readwrite("init_max_error", |
| 356 |
&Opts::init_max_error, |
| 357 |
"Maximum error in pixels for two-view geometry estimation " |
| 358 |
"for initial image pair.") |
| 359 |
.def_readwrite("init_max_forward_motion", |
| 360 |
&Opts::init_max_forward_motion, |
| 361 |
"Maximum forward motion for initial image pair.") |
| 362 |
.def_readwrite("init_min_tri_angle", |
| 363 |
&Opts::init_min_tri_angle, |
| 364 |
"Minimum triangulation angle for initial image pair.") |
| 365 |
.def_readwrite( |
| 366 |
"init_max_reg_trials", |
| 367 |
&Opts::init_max_reg_trials, |
| 368 |
"Maximum number of trials to use an image for initialization.") |
| 369 |
.def_readwrite("abs_pose_max_error", |
| 370 |
&Opts::abs_pose_max_error, |
| 371 |
"Maximum reprojection error in absolute pose estimation.") |
| 372 |
.def_readwrite("abs_pose_min_num_inliers", |
| 373 |
&Opts::abs_pose_min_num_inliers, |
| 374 |
"Minimum number of inliers in absolute pose estimation.") |
| 375 |
.def_readwrite("abs_pose_min_inlier_ratio", |
| 376 |
&Opts::abs_pose_min_inlier_ratio, |
| 377 |
"Minimum inlier ratio in absolute pose estimation.") |
| 378 |
.def_readwrite( |
| 379 |
"abs_pose_refine_focal_length", |
| 380 |
&Opts::abs_pose_refine_focal_length, |
| 381 |
"Whether to estimate the focal length in absolute pose estimation.") |
| 382 |
.def_readwrite("abs_pose_refine_extra_params", |
| 383 |
&Opts::abs_pose_refine_extra_params, |
| 384 |
"Whether to estimate the extra parameters in absolute " |
| 385 |
"pose estimation.") |
| 386 |
.def_readwrite("ba_local_num_images", |
| 387 |
&Opts::ba_local_num_images, |
| 388 |
"Number of images to optimize in local bundle adjustment.") |
| 389 |
.def_readwrite("ba_local_min_tri_angle", |
| 390 |
&Opts::ba_local_min_tri_angle, |
| 391 |
"Minimum triangulation for images to be chosen in local " |
| 392 |
"bundle adjustment.") |
| 393 |
.def_readwrite( |
| 394 |
"ba_global_ignore_redundant_points3D", |
| 395 |
&Opts::ba_global_ignore_redundant_points3D, |
| 396 |
"Whether to ignore redundant 3D points in bundle adjustment when " |
| 397 |
"jointly optimizing all parameters. If this is enabled, then the " |
| 398 |
"bundle adjustment problem is first solved with a reduced set of 3D " |
| 399 |
"points and then the remaining 3D points are optimized in a second " |
| 400 |
"step with all other parameters fixed. Points excplicitly configured " |
| 401 |
"as constant or variable are not ignored. This is only activated " |
| 402 |
"when the reconstruction has reached sufficient size with at least " |
| 403 |
"10 registered frames.") |
| 404 |
.def_readwrite( |
| 405 |
"ba_global_prune_points_min_coverage_gain", |
| 406 |
&Opts::ba_global_ignore_redundant_points3D_min_coverage_gain, |
| 407 |
"The minimum coverage gain for any 3D point to be " |
| 408 |
"included in the optimization. A larger value means " |
| 409 |
"more 3D points are ignored.") |
| 410 |
.def_readwrite("min_focal_length_ratio", |
| 411 |
&Opts::min_focal_length_ratio, |
| 412 |
"The threshold used to filter and ignore images with " |
| 413 |
"degenerate intrinsics.") |
| 414 |
.def_readwrite("max_focal_length_ratio", |
| 415 |
&Opts::max_focal_length_ratio, |
| 416 |
"The threshold used to filter and ignore images with " |
| 417 |
"degenerate intrinsics.") |
| 418 |
.def_readwrite("max_extra_param", |
| 419 |
&Opts::max_extra_param, |
| 420 |
"The threshold used to filter and ignore images with " |
| 421 |
"degenerate intrinsics.") |
| 422 |
.def_readwrite("filter_max_reproj_error", |
| 423 |
&Opts::filter_max_reproj_error, |
| 424 |
"Maximum reprojection error in pixels for observations.") |
| 425 |
.def_readwrite( |
| 426 |
"filter_min_tri_angle", |
| 427 |
&Opts::filter_min_tri_angle, |
| 428 |
"Minimum triangulation angle in degrees for stable 3D points.") |
| 429 |
.def_readwrite("max_reg_trials", |
| 430 |
&Opts::max_reg_trials, |
| 431 |
"Maximum number of trials to register an image.") |
| 432 |
.def_readwrite("fix_existing_frames", |
| 433 |
&Opts::fix_existing_frames, |
| 434 |
"If reconstruction is provided as input, fix the existing " |
| 435 |
"frame poses.") |
| 436 |
.def_readwrite( |
| 437 |
"constant_rigs", |
| 438 |
&Opts::constant_rigs, |
| 439 |
"List of rigs for which to fix the sensor_from_rig transformation, " |
| 440 |
"independent of ba_refine_sensor_from_rig.") |
| 441 |
.def_readwrite("constant_cameras", |
| 442 |
&Opts::constant_cameras, |
| 443 |
"List of cameras for which to fix the camera parameters " |
| 444 |
"independent of refine_focal_length, " |
| 445 |
"refine_principal_point, and refine_extra_params.") |
| 446 |
.def_readwrite("num_threads", &Opts::num_threads, "Number of threads.") |
| 447 |
.def_readwrite( |
| 448 |
"random_seed", |
| 449 |
&Opts::random_seed, |
| 450 |
"PRNG seed for all stochastic methods during reconstruction.") |
| 451 |
.def_readwrite("image_selection_method", |
| 452 |
&Opts::image_selection_method, |
| 453 |
"Method to find and select next best image to register.") |
| 454 |
.def("check", &Opts::Check); |
| 455 |
MakeDataclass(PyOpts); |
| 456 |
} |
| 457 |
|
| 458 |
void BindIncrementalMapperImpl(py::module& m) { |
| 459 |
BindIncrementalMapperOptions(m); |
| 460 |
|
| 461 |
// bind local bundle adjustment report |
| 462 |
using LocalBAReport = IncrementalMapper::LocalBundleAdjustmentReport; |
| 463 |
auto PyLocalBAReport = |
| 464 |
py::classh<LocalBAReport>(m, "LocalBundleAdjustmentReport"); |
| 465 |
PyLocalBAReport.def(py::init<>()) |
| 466 |
.def_readwrite("num_merged_observations", |
| 467 |
&LocalBAReport::num_merged_observations) |
| 468 |
.def_readwrite("num_completed_observations", |
| 469 |
&LocalBAReport::num_completed_observations) |
| 470 |
.def_readwrite("num_filtered_observations", |
| 471 |
&LocalBAReport::num_filtered_observations) |
| 472 |
.def_readwrite("num_adjusted_observations", |
| 473 |
&LocalBAReport::num_adjusted_observations); |
| 474 |
MakeDataclass(PyLocalBAReport); |
| 475 |
|
| 476 |
// bind incremental mapper |
| 477 |
py::classh<IncrementalMapper>( |
| 478 |
m, |
| 479 |
"IncrementalMapper", |
| 480 |
"Class that provides all functionality for the incremental " |
| 481 |
"reconstruction procedure.") |
| 482 |
.def(py::init<std::shared_ptr<const DatabaseCache>>(), |
| 483 |
"database_cache"_a, |
| 484 |
"Create incremental mapper. The database cache must live for the " |
| 485 |
"entire life-time of the incremental mapper.") |
| 486 |
.def("begin_reconstruction", |
| 487 |
&IncrementalMapper::BeginReconstruction, |
| 488 |
"reconstruction"_a, |
| 489 |
"Prepare the mapper for a new reconstruction, which might have " |
| 490 |
"existing registered images (in which case register_next_image " |
| 491 |
"must be called) or which is empty (in which case " |
| 492 |
"register_initial_image_pair must be called).") |
| 493 |
.def("end_reconstruction", |
| 494 |
&IncrementalMapper::EndReconstruction, |
| 495 |
"discard"_a, |
| 496 |
"Cleanup the mapper after the current reconstruction is done. If " |
| 497 |
"the model is discarded, the number of total and shared registered " |
| 498 |
"images will be updated accordingly.") |
| 499 |
.def( |
| 500 |
"find_initial_image_pair", |
| 501 |
[](IncrementalMapper& self, |
| 502 |
const IncrementalMapper::Options& options, |
| 503 |
int image_id1, |
| 504 |
int image_id2) |
| 505 |
-> py::typing::Optional< |
| 506 |
py::typing::Tuple<py::typing::Tuple<image_t, image_t>, |
| 507 |
Rigid3d>> { |
| 508 |
// Explicitly handle the conversion |
| 509 |
// from -1 (int) to kInvalidImageId (uint32_t). |
| 510 |
image_t image_id1_cast = image_id1; |
| 511 |
image_t image_id2_cast = image_id2; |
| 512 |
Rigid3d cam2_from_cam1; |
| 513 |
const bool success = self.FindInitialImagePair( |
| 514 |
options, image_id1_cast, image_id2_cast, cam2_from_cam1); |
| 515 |
if (success) { |
| 516 |
const auto pair = std::make_pair(image_id1_cast, image_id2_cast); |
| 517 |
return py::cast(std::make_pair(pair, cam2_from_cam1)); |
| 518 |
} else { |
| 519 |
return py::none(); |
| 520 |
} |
| 521 |
}, |
| 522 |
"options"_a, |
| 523 |
"image_id1"_a, |
| 524 |
"image_id2"_a, |
| 525 |
"Find initial image pair to seed the incremental reconstruction. " |
| 526 |
"Returns a tuple of ((image_id1, image_id2), cam2_from_cam1) on " |
| 527 |
"success, or None on failure. This function automatically ignores " |
| 528 |
"image pairs that failed to register previously.") |
| 529 |
.def( |
| 530 |
"estimate_initial_two_view_geometry", |
| 531 |
[](IncrementalMapper& self, |
| 532 |
const IncrementalMapper::Options& options, |
| 533 |
const image_t image_id1, |
| 534 |
const image_t image_id2) -> py::typing::Optional<Rigid3d> { |
| 535 |
Rigid3d cam2_from_cam1; |
| 536 |
const bool success = self.EstimateInitialTwoViewGeometry( |
| 537 |
options, image_id1, image_id2, cam2_from_cam1); |
| 538 |
if (success) |
| 539 |
return py::cast(cam2_from_cam1); |
| 540 |
else |
| 541 |
return py::none(); |
| 542 |
}, |
| 543 |
"options"_a, |
| 544 |
"image_id1"_a, |
| 545 |
"image_id2"_a, |
| 546 |
"Estimate two-view geometry and check if it is suitable for " |
| 547 |
"initialization. Returns the relative pose on success, or None " |
| 548 |
"on failure.") |
| 549 |
.def("register_initial_image_pair", |
| 550 |
&IncrementalMapper::RegisterInitialImagePair, |
| 551 |
"options"_a, |
| 552 |
"two_view_geometry"_a, |
| 553 |
"image_id1"_a, |
| 554 |
"image_id2"_a, |
| 555 |
"Attempt to seed the reconstruction from an image pair.") |
| 556 |
.def("find_next_images", |
| 557 |
&IncrementalMapper::FindNextImages, |
| 558 |
"options"_a, |
| 559 |
"structure_less"_a, |
| 560 |
"Find best next images to register in the incremental " |
| 561 |
"reconstruction. This function automatically ignores images that " |
| 562 |
"failed to register for max_reg_trials.") |
| 563 |
.def("register_next_image", |
| 564 |
&IncrementalMapper::RegisterNextImage, |
| 565 |
"options"_a, |
| 566 |
"image_id"_a, |
| 567 |
"Attempt to register image to the existing model. This requires " |
| 568 |
"that a previous call to register_initial_image_pair was " |
| 569 |
"successful.") |
| 570 |
.def("register_next_structure_less_image", |
| 571 |
&IncrementalMapper::RegisterNextStructureLessImage, |
| 572 |
"options"_a, |
| 573 |
"image_id"_a, |
| 574 |
"Attempt to register image using structure-less resectioning.") |
| 575 |
.def("triangulate_image", |
| 576 |
&IncrementalMapper::TriangulateImage, |
| 577 |
"tri_options"_a, |
| 578 |
"image_id"_a, |
| 579 |
"Triangulate observations of image.") |
| 580 |
.def("retriangulate", |
| 581 |
&IncrementalMapper::Retriangulate, |
| 582 |
"tri_options"_a, |
| 583 |
"Retriangulate image pairs that should have common observations " |
| 584 |
"according to the scene graph but don't due to drift, etc.") |
| 585 |
.def("complete_tracks", |
| 586 |
&IncrementalMapper::CompleteTracks, |
| 587 |
"tri_options"_a, |
| 588 |
"Complete tracks by transitively following the scene graph " |
| 589 |
"correspondences. This is especially effective after bundle " |
| 590 |
"adjustment, since many cameras and point locations might have " |
| 591 |
"improved.") |
| 592 |
.def("merge_tracks", |
| 593 |
&IncrementalMapper::MergeTracks, |
| 594 |
"tri_options"_a, |
| 595 |
"Merge tracks by using scene graph correspondences. Similar to " |
| 596 |
"complete_tracks, this is effective after bundle adjustment and " |
| 597 |
"improves the redundancy in subsequent bundle adjustments.") |
| 598 |
.def("complete_and_merge_tracks", |
| 599 |
&IncrementalMapper::CompleteAndMergeTracks, |
| 600 |
"tri_options"_a, |
| 601 |
"Globally complete and merge tracks.") |
| 602 |
.def("adjust_local_bundle", |
| 603 |
&IncrementalMapper::AdjustLocalBundle, |
| 604 |
"options"_a, |
| 605 |
"ba_options"_a, |
| 606 |
"tri_options"_a, |
| 607 |
"image_id"_a, |
| 608 |
"point3D_ids"_a, |
| 609 |
"Adjust locally connected images and points of a reference image. " |
| 610 |
"In addition, refine the provided 3D points. Only images connected " |
| 611 |
"to the reference image are optimized. If the provided 3D points " |
| 612 |
"are not locally connected to the reference image, their observing " |
| 613 |
"images are set as constant in the adjustment.") |
| 614 |
.def("iterative_local_refinement", |
| 615 |
&IncrementalMapper::IterativeLocalRefinement, |
| 616 |
"max_num_refinements"_a, |
| 617 |
"max_refinement_change"_a, |
| 618 |
"options"_a, |
| 619 |
"ba_options"_a, |
| 620 |
"tri_options"_a, |
| 621 |
"image_id"_a, |
| 622 |
"Perform multiple rounds of local bundle adjustment.") |
| 623 |
.def("find_local_bundle", |
| 624 |
&IncrementalMapper::FindLocalBundle, |
| 625 |
"options"_a, |
| 626 |
"image_id"_a, |
| 627 |
"Find local bundle for given image in the reconstruction. The " |
| 628 |
"local bundle is defined as the images that are most connected, " |
| 629 |
"i.e. maximum number of shared 3D points, to the given image.") |
| 630 |
.def("adjust_global_bundle", |
| 631 |
&IncrementalMapper::AdjustGlobalBundle, |
| 632 |
"options"_a, |
| 633 |
"ba_options"_a, |
| 634 |
"Global bundle adjustment using Ceres Solver.") |
| 635 |
.def("iterative_global_refinement", |
| 636 |
&IncrementalMapper::IterativeGlobalRefinement, |
| 637 |
"max_num_refinements"_a, |
| 638 |
"max_refinement_change"_a, |
| 639 |
"options"_a, |
| 640 |
"ba_options"_a, |
| 641 |
"tri_options"_a, |
| 642 |
"normalize_reconstruction"_a = true, |
| 643 |
"Perform multiple rounds of global bundle adjustment.") |
| 644 |
.def( |
| 645 |
"filter_frames", |
| 646 |
&IncrementalMapper::FilterFrames, |
| 647 |
"options"_a, |
| 648 |
"Filter frames with degenerate camera parameters or no observations.") |
| 649 |
.def("filter_points", |
| 650 |
&IncrementalMapper::FilterPoints, |
| 651 |
"options"_a, |
| 652 |
"Filter points with large reprojection errors or small " |
| 653 |
"triangulation angles.") |
| 654 |
.def_property_readonly("reconstruction", |
| 655 |
&IncrementalMapper::Reconstruction) |
| 656 |
.def_property_readonly("observation_manager", |
| 657 |
&IncrementalMapper::ObservationManager) |
| 658 |
.def_property_readonly("triangulator", &IncrementalMapper::Triangulator) |
| 659 |
.def_property_readonly("filtered_frames", |
| 660 |
&IncrementalMapper::FilteredFrames) |
| 661 |
.def_property_readonly("existing_frame_ids", |
| 662 |
&IncrementalMapper::ExistingFrameIds) |
| 663 |
.def("reset_initialization_stats", |
| 664 |
&IncrementalMapper::ResetInitializationStats, |
| 665 |
"Reset registration statistics for initialization. This can be " |
| 666 |
"used when relaxing the initialization thresholds, such that " |
| 667 |
"previously tried pairs will be tried again.") |
| 668 |
.def_property_readonly("num_reg_frames_per_rig", |
| 669 |
&IncrementalMapper::NumRegFramesPerRig) |
| 670 |
.def_property_readonly("num_reg_images_per_camera", |
| 671 |
&IncrementalMapper::NumRegImagesPerCamera) |
| 672 |
.def("num_total_reg_images", |
| 673 |
&IncrementalMapper::NumTotalRegImages, |
| 674 |
"Number of images that are registered in at least one " |
| 675 |
"reconstruction.") |
| 676 |
.def("num_shared_reg_images", |
| 677 |
&IncrementalMapper::NumSharedRegImages, |
| 678 |
"Number of shared images between current reconstruction and all " |
| 679 |
"other previous reconstructions.") |
| 680 |
.def("get_modified_points3D", |
| 681 |
&IncrementalMapper::GetModifiedPoints3D, |
| 682 |
"Get changed 3D points, since the last call to " |
| 683 |
"clear_modified_points3D.") |
| 684 |
.def("clear_modified_points3D", |
| 685 |
&IncrementalMapper::ClearModifiedPoints3D, |
| 686 |
"Clear the collection of changed 3D points."); |
| 687 |
} |
| 688 |
|
| 689 |
void BindIncrementalMapper(py::module& m) { |
| 690 |
BindIncrementalMapperImpl(m); |
| 691 |
BindIncrementalPipeline(m); |
| 692 |
} |