| 1 |
#include "colmap/estimators/global_positioning.h" |
| 2 |
#include "colmap/estimators/gravity_refinement.h" |
| 3 |
#include "colmap/estimators/rotation_averaging.h" |
| 4 |
|
| 5 |
#include "pycolmap/helpers.h" |
| 6 |
|
| 7 |
#include <pybind11/eigen.h> |
| 8 |
#include <pybind11/pybind11.h> |
| 9 |
#include <pybind11/stl.h> |
| 10 |
|
| 11 |
using namespace colmap; |
| 12 |
using namespace pybind11::literals; |
| 13 |
namespace py = pybind11; |
| 14 |
|
| 15 |
void BindGlobalPositioner(py::module& m) { |
| 16 |
auto PyGlobalPositionerOptions = |
| 17 |
py::classh<GlobalPositionerOptions>(m, "GlobalPositionerOptions") |
| 18 |
.def(py::init<>()) |
| 19 |
.def_readwrite("generate_random_positions", |
| 20 |
&GlobalPositionerOptions::generate_random_positions, |
| 21 |
"Whether to initialize camera positions randomly.") |
| 22 |
.def_readwrite("generate_random_points", |
| 23 |
&GlobalPositionerOptions::generate_random_points, |
| 24 |
"Whether to initialize 3D point positions randomly.") |
| 25 |
.def_readwrite("generate_scales", |
| 26 |
&GlobalPositionerOptions::generate_scales, |
| 27 |
"Whether to initialize scales to constant 1 or derive " |
| 28 |
"from positions.") |
| 29 |
.def_readwrite("optimize_positions", |
| 30 |
&GlobalPositionerOptions::optimize_positions, |
| 31 |
"Whether to optimize camera positions.") |
| 32 |
.def_readwrite("optimize_points", |
| 33 |
&GlobalPositionerOptions::optimize_points, |
| 34 |
"Whether to optimize 3D point positions.") |
| 35 |
.def_readwrite("optimize_scales", |
| 36 |
&GlobalPositionerOptions::optimize_scales, |
| 37 |
"Whether to optimize scales.") |
| 38 |
.def_readwrite( |
| 39 |
"refine_sensor_from_rig", |
| 40 |
&GlobalPositionerOptions::refine_sensor_from_rig, |
| 41 |
"When False, treat sensor_from_rig as a fixed pre-calibrated " |
| 42 |
"parameter.") |
| 43 |
.def_readwrite("use_gpu", |
| 44 |
&GlobalPositionerOptions::use_gpu, |
| 45 |
"Whether to use GPU for optimization.") |
| 46 |
.def_readwrite("gpu_index", |
| 47 |
&GlobalPositionerOptions::gpu_index, |
| 48 |
"GPU device index (-1 for auto).") |
| 49 |
.def_readwrite("min_num_images_gpu_solver", |
| 50 |
&GlobalPositionerOptions::min_num_images_gpu_solver, |
| 51 |
"Minimum number of images to use GPU solver.") |
| 52 |
.def_readwrite("min_num_view_per_track", |
| 53 |
&GlobalPositionerOptions::min_num_view_per_track, |
| 54 |
"Minimum number of views per track.") |
| 55 |
.def_readwrite("random_seed", |
| 56 |
&GlobalPositionerOptions::random_seed, |
| 57 |
"PRNG seed for random initialization. -1 for " |
| 58 |
"non-deterministic.") |
| 59 |
.def_readwrite("loss_function_scale", |
| 60 |
&GlobalPositionerOptions::loss_function_scale, |
| 61 |
"Scaling factor for the loss function.") |
| 62 |
.def_readwrite("use_parameter_block_ordering", |
| 63 |
&GlobalPositionerOptions::use_parameter_block_ordering, |
| 64 |
"Whether to use custom parameter block ordering."); |
| 65 |
MakeDataclass(PyGlobalPositionerOptions); |
| 66 |
|
| 67 |
m.def( |
| 68 |
"run_global_positioning", |
| 69 |
[](const GlobalPositionerOptions& options, |
| 70 |
const PoseGraph& pose_graph, |
| 71 |
Reconstruction& reconstruction) { |
| 72 |
py::gil_scoped_release release; |
| 73 |
bool success = |
| 74 |
RunGlobalPositioning(options, pose_graph, reconstruction); |
| 75 |
return success; |
| 76 |
}, |
| 77 |
"options"_a, |
| 78 |
"pose_graph"_a, |
| 79 |
"reconstruction"_a, |
| 80 |
"Solve global positioning using point-to-camera constraints. Returns " |
| 81 |
"True if optimization succeeded."); |
| 82 |
} |
| 83 |
|
| 84 |
void BindGravityRefiner(py::module& m) { |
| 85 |
auto PyGravityRefinerOptions = |
| 86 |
py::classh<GravityRefinerOptions>(m, "GravityRefinerOptions") |
| 87 |
.def(py::init<>()) |
| 88 |
.def_readwrite( |
| 89 |
"max_outlier_ratio", |
| 90 |
&GravityRefinerOptions::max_outlier_ratio, |
| 91 |
"Maximum ratio that gravity should be consistent with.") |
| 92 |
.def_readwrite("max_gravity_error", |
| 93 |
&GravityRefinerOptions::max_gravity_error, |
| 94 |
"Maximum allowed angle error in degrees.") |
| 95 |
.def_readwrite("min_num_neighbors", |
| 96 |
&GravityRefinerOptions::min_num_neighbors, |
| 97 |
"Minimum neighbors required for refinement."); |
| 98 |
MakeDataclass(PyGravityRefinerOptions); |
| 99 |
|
| 100 |
m.def( |
| 101 |
"run_gravity_refinement", |
| 102 |
[](const GravityRefinerOptions& options, |
| 103 |
const PoseGraph& pose_graph, |
| 104 |
const Reconstruction& reconstruction, |
| 105 |
std::vector<PosePrior>& pose_priors) { |
| 106 |
py::gil_scoped_release release; |
| 107 |
RunGravityRefinement(options, pose_graph, reconstruction, pose_priors); |
| 108 |
}, |
| 109 |
"options"_a, |
| 110 |
"pose_graph"_a, |
| 111 |
"reconstruction"_a, |
| 112 |
"pose_priors"_a, |
| 113 |
"Refine gravity stored in pose priors using relative rotations from the " |
| 114 |
"pose graph. Modifies pose_priors in-place."); |
| 115 |
} |
| 116 |
|
| 117 |
void BindRotationEstimator(py::module& m) { |
| 118 |
using WeightType = RotationEstimatorOptions::WeightType; |
| 119 |
auto PyWeightType = py::enum_<WeightType>(m, "RotationWeightType") |
| 120 |
.value("GEMAN_MCCLURE", WeightType::GEMAN_MCCLURE) |
| 121 |
.value("HALF_NORM", WeightType::HALF_NORM); |
| 122 |
AddStringToEnumConstructor(PyWeightType); |
| 123 |
|
| 124 |
auto PyRotationAveragingReweighting = |
| 125 |
py::enum_<RotationAveragingReweighting>(m, "RotationAveragingReweighting") |
| 126 |
.value("UNIFORM", RotationAveragingReweighting::UNIFORM) |
| 127 |
.value("INLIER_MATCH_COUNT", |
| 128 |
RotationAveragingReweighting::INLIER_MATCH_COUNT); |
| 129 |
AddStringToEnumConstructor(PyRotationAveragingReweighting); |
| 130 |
|
| 131 |
auto PyRotationEstimatorOptions = |
| 132 |
py::classh<RotationEstimatorOptions>(m, "RotationEstimatorOptions") |
| 133 |
.def(py::init<>()) |
| 134 |
.def_readwrite("random_seed", |
| 135 |
&RotationEstimatorOptions::random_seed, |
| 136 |
"PRNG seed. -1 for non-deterministic, >=0 for " |
| 137 |
"deterministic.") |
| 138 |
.def_readwrite("max_num_l1_iterations", |
| 139 |
&RotationEstimatorOptions::max_num_l1_iterations, |
| 140 |
"Maximum number of L1 minimization iterations.") |
| 141 |
.def_readwrite( |
| 142 |
"l1_step_convergence_threshold", |
| 143 |
&RotationEstimatorOptions::l1_step_convergence_threshold, |
| 144 |
"Average step size threshold to terminate L1 minimization.") |
| 145 |
.def_readwrite("max_num_irls_iterations", |
| 146 |
&RotationEstimatorOptions::max_num_irls_iterations, |
| 147 |
"Number of IRLS iterations to perform.") |
| 148 |
.def_readwrite( |
| 149 |
"irls_step_convergence_threshold", |
| 150 |
&RotationEstimatorOptions::irls_step_convergence_threshold, |
| 151 |
"Average step size threshold to terminate IRLS.") |
| 152 |
.def_readwrite("gravity_dir", |
| 153 |
&RotationEstimatorOptions::gravity_dir, |
| 154 |
"Gravity direction vector.") |
| 155 |
.def_readwrite( |
| 156 |
"irls_loss_parameter_sigma", |
| 157 |
&RotationEstimatorOptions::irls_loss_parameter_sigma, |
| 158 |
"Point where Huber-like cost switches from L1 to L2 (degrees).") |
| 159 |
.def_readwrite( |
| 160 |
"ridge_regularization", |
| 161 |
&RotationEstimatorOptions::ridge_regularization, |
| 162 |
"Tikhonov ridge added to the diagonal of the normal equations " |
| 163 |
"before each Cholesky factorization in the L1 and IRLS phases. " |
| 164 |
"Set to a small positive value (e.g., 1e-9) to stabilize poorly " |
| 165 |
"conditioned systems. Zero disables regularization.") |
| 166 |
.def_readwrite("weight_type", |
| 167 |
&RotationEstimatorOptions::weight_type, |
| 168 |
"Weight type for IRLS: GEMAN_MCCLURE or HALF_NORM.") |
| 169 |
.def_readwrite("skip_initialization", |
| 170 |
&RotationEstimatorOptions::skip_initialization, |
| 171 |
"Skip maximum spanning tree initialization.") |
| 172 |
.def_readwrite("use_gravity", |
| 173 |
&RotationEstimatorOptions::use_gravity, |
| 174 |
"Use gravity priors for rotation averaging.") |
| 175 |
.def_readwrite("use_stratified", |
| 176 |
&RotationEstimatorOptions::use_stratified, |
| 177 |
"Use stratified solving for mixed gravity systems.") |
| 178 |
.def_readwrite( |
| 179 |
"filter_unregistered", |
| 180 |
&RotationEstimatorOptions::filter_unregistered, |
| 181 |
"Only consider frames with existing poses for connected " |
| 182 |
"components.") |
| 183 |
.def_readwrite( |
| 184 |
"max_rotation_error_deg", |
| 185 |
&RotationEstimatorOptions::max_rotation_error_deg, |
| 186 |
"Filter pairs with rotation error exceeding this threshold " |
| 187 |
"(degrees).") |
| 188 |
.def_readwrite( |
| 189 |
"refine_sensor_from_rig", |
| 190 |
&RotationEstimatorOptions::refine_sensor_from_rig, |
| 191 |
"When False, treat each non-ref sensor's cam_from_rig as a " |
| 192 |
"pre-calibrated constant.") |
| 193 |
.def_readwrite("reweighting", |
| 194 |
&RotationEstimatorOptions::reweighting, |
| 195 |
"Reweighting scheme for relative-rotation " |
| 196 |
"constraints: UNIFORM or " |
| 197 |
"INLIER_MATCH_COUNT."); |
| 198 |
MakeDataclass(PyRotationEstimatorOptions); |
| 199 |
|
| 200 |
m.def( |
| 201 |
"run_rotation_averaging", |
| 202 |
[](const RotationEstimatorOptions& options, |
| 203 |
PoseGraph& pose_graph, |
| 204 |
Reconstruction& reconstruction, |
| 205 |
const std::vector<PosePrior>& pose_priors) { |
| 206 |
py::gil_scoped_release release; |
| 207 |
bool success = RunRotationAveraging( |
| 208 |
options, pose_graph, reconstruction, pose_priors); |
| 209 |
return success; |
| 210 |
}, |
| 211 |
"options"_a, |
| 212 |
"pose_graph"_a, |
| 213 |
"reconstruction"_a, |
| 214 |
"pose_priors"_a, |
| 215 |
"High-level rotation averaging solver that handles rig expansion. " |
| 216 |
"Returns True if rotation averaging succeeded."); |
| 217 |
} |
| 218 |
|
| 219 |
void BindMotionAveraging(py::module& m) { |
| 220 |
BindGravityRefiner(m); |
| 221 |
BindRotationEstimator(m); |
| 222 |
BindGlobalPositioner(m); |
| 223 |
} |