| 1 |
#include "colmap/estimators/pose.h" |
| 2 |
|
| 3 |
#include "colmap/geometry/rigid3.h" |
| 4 |
#include "colmap/math/random.h" |
| 5 |
#include "colmap/scene/camera.h" |
| 6 |
#include "colmap/util/logging.h" |
| 7 |
|
| 8 |
#include "pycolmap/helpers.h" |
| 9 |
#include "pycolmap/pybind11_extension.h" |
| 10 |
#include "pycolmap/utils.h" |
| 11 |
|
| 12 |
#include <pybind11/eigen.h> |
| 13 |
#include <pybind11/pybind11.h> |
| 14 |
#include <pybind11/stl.h> |
| 15 |
|
| 16 |
using namespace colmap; |
| 17 |
using namespace pybind11::literals; |
| 18 |
namespace py = pybind11; |
| 19 |
|
| 20 |
py::typing::Optional<py::dict> PyEstimateAbsolutePose( |
| 21 |
const std::vector<Eigen::Vector2d>& points2D, |
| 22 |
const std::vector<Eigen::Vector3d>& points3D, |
| 23 |
Camera& camera, |
| 24 |
const AbsolutePoseEstimationOptions& estimation_options) { |
| 25 |
py::gil_scoped_release release; |
| 26 |
Rigid3d cam_from_world; |
| 27 |
size_t num_inliers; |
| 28 |
std::vector<char> inlier_mask; |
| 29 |
if (!EstimateAbsolutePose(estimation_options, |
| 30 |
points2D, |
| 31 |
points3D, |
| 32 |
&cam_from_world, |
| 33 |
&camera, |
| 34 |
&num_inliers, |
| 35 |
&inlier_mask)) { |
| 36 |
py::gil_scoped_acquire acquire; |
| 37 |
return py::none(); |
| 38 |
} |
| 39 |
|
| 40 |
py::gil_scoped_acquire acquire; |
| 41 |
return py::dict("cam_from_world"_a = cam_from_world, |
| 42 |
"num_inliers"_a = num_inliers, |
| 43 |
"inlier_mask"_a = ToPythonMask(inlier_mask)); |
| 44 |
} |
| 45 |
|
| 46 |
py::typing::Optional<py::dict> PyRefineAbsolutePose( |
| 47 |
const Rigid3d& init_cam_from_world, |
| 48 |
const std::vector<Eigen::Vector2d>& points2D, |
| 49 |
const std::vector<Eigen::Vector3d>& points3D, |
| 50 |
const PyInlierMask& inlier_mask, |
| 51 |
Camera& camera, |
| 52 |
const AbsolutePoseRefinementOptions& refinement_options, |
| 53 |
const bool return_covariance) { |
| 54 |
py::gil_scoped_release release; |
| 55 |
Rigid3d refined_cam_from_world = init_cam_from_world; |
| 56 |
std::vector<char> inlier_mask_char(inlier_mask.size()); |
| 57 |
Eigen::Map<Eigen::Matrix<char, Eigen::Dynamic, 1>>( |
| 58 |
inlier_mask_char.data(), inlier_mask.size()) = inlier_mask.cast<char>(); |
| 59 |
Eigen::Matrix<double, 6, 6> covariance; |
| 60 |
if (!RefineAbsolutePose(refinement_options, |
| 61 |
inlier_mask_char, |
| 62 |
points2D, |
| 63 |
points3D, |
| 64 |
&refined_cam_from_world, |
| 65 |
&camera, |
| 66 |
return_covariance ? &covariance : nullptr)) { |
| 67 |
py::gil_scoped_acquire acquire; |
| 68 |
return py::none(); |
| 69 |
} |
| 70 |
py::gil_scoped_acquire acquire; |
| 71 |
py::dict result("cam_from_world"_a = refined_cam_from_world); |
| 72 |
if (return_covariance) result["covariance"] = covariance; |
| 73 |
return result; |
| 74 |
} |
| 75 |
|
| 76 |
py::typing::Optional<py::dict> PyEstimateAndRefineAbsolutePose( |
| 77 |
const std::vector<Eigen::Vector2d>& points2D, |
| 78 |
const std::vector<Eigen::Vector3d>& points3D, |
| 79 |
Camera& camera, |
| 80 |
const AbsolutePoseEstimationOptions& estimation_options, |
| 81 |
const AbsolutePoseRefinementOptions& refinement_options, |
| 82 |
const bool return_covariance) { |
| 83 |
py::gil_scoped_release release; |
| 84 |
Rigid3d cam_from_world; |
| 85 |
size_t num_inliers; |
| 86 |
std::vector<char> inlier_mask; |
| 87 |
if (!EstimateAbsolutePose(estimation_options, |
| 88 |
points2D, |
| 89 |
points3D, |
| 90 |
&cam_from_world, |
| 91 |
&camera, |
| 92 |
&num_inliers, |
| 93 |
&inlier_mask)) { |
| 94 |
py::gil_scoped_acquire acquire; |
| 95 |
return py::none(); |
| 96 |
} |
| 97 |
|
| 98 |
Eigen::Matrix<double, 6, 6> covariance; |
| 99 |
if (!RefineAbsolutePose(refinement_options, |
| 100 |
inlier_mask, |
| 101 |
points2D, |
| 102 |
points3D, |
| 103 |
&cam_from_world, |
| 104 |
&camera, |
| 105 |
return_covariance ? &covariance : nullptr)) { |
| 106 |
py::gil_scoped_acquire acquire; |
| 107 |
return py::none(); |
| 108 |
} |
| 109 |
|
| 110 |
py::gil_scoped_acquire acquire; |
| 111 |
py::dict result("cam_from_world"_a = cam_from_world, |
| 112 |
"num_inliers"_a = num_inliers, |
| 113 |
"inlier_mask"_a = ToPythonMask(inlier_mask)); |
| 114 |
if (return_covariance) result["covariance"] = covariance; |
| 115 |
return result; |
| 116 |
} |
| 117 |
|
| 118 |
std::vector<CamRayWithJac> CamRaysWithIdentityJacobians( |
| 119 |
const std::vector<Eigen::Vector3d>& cam_rays) { |
| 120 |
// The identity embedding selects the first two ray coordinates in the |
| 121 |
// tangent Sampson denominator, matching the legacy Sampson error on rays. |
| 122 |
std::vector<CamRayWithJac> cam_rays_with_jac(cam_rays.size()); |
| 123 |
for (size_t i = 0; i < cam_rays.size(); ++i) { |
| 124 |
cam_rays_with_jac[i] = {cam_rays[i], Eigen::Matrix3x2d::Identity()}; |
| 125 |
} |
| 126 |
return cam_rays_with_jac; |
| 127 |
} |
| 128 |
|
| 129 |
py::typing::Optional<py::dict> PyEstimateRelativePoseFromRays( |
| 130 |
const std::vector<Eigen::Vector3d>& cam_rays1, |
| 131 |
const std::vector<Eigen::Vector3d>& cam_rays2, |
| 132 |
const RANSACOptions& estimation_options) { |
| 133 |
py::gil_scoped_release release; |
| 134 |
Rigid3d cam2_from_cam1; |
| 135 |
size_t num_inliers; |
| 136 |
std::vector<char> inlier_mask; |
| 137 |
if (!EstimateRelativePose(estimation_options, |
| 138 |
CamRaysWithIdentityJacobians(cam_rays1), |
| 139 |
CamRaysWithIdentityJacobians(cam_rays2), |
| 140 |
&cam2_from_cam1, |
| 141 |
&num_inliers, |
| 142 |
&inlier_mask)) { |
| 143 |
py::gil_scoped_acquire acquire; |
| 144 |
return py::none(); |
| 145 |
} |
| 146 |
|
| 147 |
py::gil_scoped_acquire acquire; |
| 148 |
return py::dict("cam2_from_cam1"_a = cam2_from_cam1, |
| 149 |
"num_inliers"_a = num_inliers, |
| 150 |
"inlier_mask"_a = ToPythonMask(inlier_mask)); |
| 151 |
} |
| 152 |
|
| 153 |
py::typing::Optional<py::dict> PyEstimateRelativePose( |
| 154 |
const Camera& camera1, |
| 155 |
const std::vector<Eigen::Vector2d>& points2D1, |
| 156 |
const Camera& camera2, |
| 157 |
const std::vector<Eigen::Vector2d>& points2D2, |
| 158 |
const RANSACOptions& estimation_options) { |
| 159 |
py::gil_scoped_release release; |
| 160 |
THROW_CHECK_EQ(points2D1.size(), points2D2.size()); |
| 161 |
const size_t num_points = points2D1.size(); |
| 162 |
// End users pass camera + 2D points. The rays and their unprojection |
| 163 |
// Jacobians (needed for the pixel-unit tangent Sampson score) are built here, |
| 164 |
// never part of the interface. Unprojectable points are zeroed -> infinite |
| 165 |
// residual -> rejected. |
| 166 |
std::vector<CamRayWithJac> cam_rays1_with_jac(num_points); |
| 167 |
std::vector<CamRayWithJac> cam_rays2_with_jac(num_points); |
| 168 |
for (size_t i = 0; i < num_points; ++i) { |
| 169 |
cam_rays1_with_jac[i] = camera1.CamRayFromImgWithJac(points2D1[i]) |
| 170 |
.value_or(CamRayWithJac::Zero()); |
| 171 |
cam_rays2_with_jac[i] = camera2.CamRayFromImgWithJac(points2D2[i]) |
| 172 |
.value_or(CamRayWithJac::Zero()); |
| 173 |
} |
| 174 |
Rigid3d cam2_from_cam1; |
| 175 |
size_t num_inliers; |
| 176 |
std::vector<char> inlier_mask; |
| 177 |
if (!EstimateRelativePose(estimation_options, |
| 178 |
cam_rays1_with_jac, |
| 179 |
cam_rays2_with_jac, |
| 180 |
&cam2_from_cam1, |
| 181 |
&num_inliers, |
| 182 |
&inlier_mask)) { |
| 183 |
py::gil_scoped_acquire acquire; |
| 184 |
return py::none(); |
| 185 |
} |
| 186 |
|
| 187 |
py::gil_scoped_acquire acquire; |
| 188 |
return py::dict("cam2_from_cam1"_a = cam2_from_cam1, |
| 189 |
"num_inliers"_a = num_inliers, |
| 190 |
"inlier_mask"_a = ToPythonMask(inlier_mask)); |
| 191 |
} |
| 192 |
|
| 193 |
py::typing::Optional<py::dict> PyRefineRelativePose( |
| 194 |
const Rigid3d& init_cam2_from_cam1, |
| 195 |
const Camera& camera1, |
| 196 |
const std::vector<Eigen::Vector2d>& points2D1, |
| 197 |
const Camera& camera2, |
| 198 |
const std::vector<Eigen::Vector2d>& points2D2, |
| 199 |
const PyInlierMask& inlier_mask, |
| 200 |
const ceres::Solver::Options& refinement_options) { |
| 201 |
py::gil_scoped_release release; |
| 202 |
THROW_CHECK_EQ(points2D1.size(), points2D2.size()); |
| 203 |
const size_t num_points = points2D1.size(); |
| 204 |
// End users pass camera + 2D points. The rays and their unprojection |
| 205 |
// Jacobians (for the pixel-unit tangent Sampson cost) are built here. |
| 206 |
std::vector<CamRayWithJac> cam_rays1_with_jac(num_points); |
| 207 |
std::vector<CamRayWithJac> cam_rays2_with_jac(num_points); |
| 208 |
for (size_t i = 0; i < num_points; ++i) { |
| 209 |
cam_rays1_with_jac[i] = camera1.CamRayFromImgWithJac(points2D1[i]) |
| 210 |
.value_or(CamRayWithJac::Zero()); |
| 211 |
cam_rays2_with_jac[i] = camera2.CamRayFromImgWithJac(points2D2[i]) |
| 212 |
.value_or(CamRayWithJac::Zero()); |
| 213 |
} |
| 214 |
Rigid3d refined_cam2_from_cam1 = init_cam2_from_cam1; |
| 215 |
std::vector<char> inlier_mask_char(inlier_mask.size()); |
| 216 |
Eigen::Map<Eigen::Matrix<char, Eigen::Dynamic, 1>>( |
| 217 |
inlier_mask_char.data(), inlier_mask.size()) = inlier_mask.cast<char>(); |
| 218 |
if (!RefineRelativePose(refinement_options, |
| 219 |
inlier_mask_char, |
| 220 |
cam_rays1_with_jac, |
| 221 |
cam_rays2_with_jac, |
| 222 |
&refined_cam2_from_cam1)) { |
| 223 |
py::gil_scoped_acquire acquire; |
| 224 |
return py::none(); |
| 225 |
} |
| 226 |
py::gil_scoped_acquire acquire; |
| 227 |
py::dict result("cam2_from_cam1"_a = refined_cam2_from_cam1); |
| 228 |
return result; |
| 229 |
} |
| 230 |
|
| 231 |
py::typing::Optional<py::dict> PyRefineRelativePoseFromRays( |
| 232 |
const Rigid3d& init_cam2_from_cam1, |
| 233 |
const std::vector<Eigen::Vector3d>& cam_rays1, |
| 234 |
const std::vector<Eigen::Vector3d>& cam_rays2, |
| 235 |
const PyInlierMask& inlier_mask, |
| 236 |
const ceres::Solver::Options& refinement_options) { |
| 237 |
py::gil_scoped_release release; |
| 238 |
Rigid3d refined_cam2_from_cam1 = init_cam2_from_cam1; |
| 239 |
std::vector<char> inlier_mask_char(inlier_mask.size()); |
| 240 |
Eigen::Map<Eigen::Matrix<char, Eigen::Dynamic, 1>>( |
| 241 |
inlier_mask_char.data(), inlier_mask.size()) = inlier_mask.cast<char>(); |
| 242 |
if (!RefineRelativePose(refinement_options, |
| 243 |
inlier_mask_char, |
| 244 |
CamRaysWithIdentityJacobians(cam_rays1), |
| 245 |
CamRaysWithIdentityJacobians(cam_rays2), |
| 246 |
&refined_cam2_from_cam1)) { |
| 247 |
py::gil_scoped_acquire acquire; |
| 248 |
return py::none(); |
| 249 |
} |
| 250 |
py::gil_scoped_acquire acquire; |
| 251 |
return py::dict("cam2_from_cam1"_a = refined_cam2_from_cam1); |
| 252 |
} |
| 253 |
|
| 254 |
void BindAbsolutePoseEstimator(py::module& m) { |
| 255 |
auto PyRANSACOptions = m.attr("RANSACOptions"); |
| 256 |
py::classh<AbsolutePoseEstimationOptions> PyEstimationOptions( |
| 257 |
m, "AbsolutePoseEstimationOptions"); |
| 258 |
PyEstimationOptions.def(py::init<>()) |
| 259 |
.def_readwrite("estimate_focal_length", |
| 260 |
&AbsolutePoseEstimationOptions::estimate_focal_length) |
| 261 |
.def_readwrite("ransac", &AbsolutePoseEstimationOptions::ransac_options); |
| 262 |
MakeDataclass(PyEstimationOptions); |
| 263 |
|
| 264 |
py::classh<AbsolutePoseRefinementOptions> PyRefinementOptions( |
| 265 |
m, "AbsolutePoseRefinementOptions"); |
| 266 |
PyRefinementOptions.def(py::init<>()) |
| 267 |
.def_readwrite("gradient_tolerance", |
| 268 |
&AbsolutePoseRefinementOptions::gradient_tolerance) |
| 269 |
.def_readwrite("max_num_iterations", |
| 270 |
&AbsolutePoseRefinementOptions::max_num_iterations) |
| 271 |
.def_readwrite("loss_function_scale", |
| 272 |
&AbsolutePoseRefinementOptions::loss_function_scale) |
| 273 |
.def_readwrite("refine_focal_length", |
| 274 |
&AbsolutePoseRefinementOptions::refine_focal_length) |
| 275 |
.def_readwrite("refine_extra_params", |
| 276 |
&AbsolutePoseRefinementOptions::refine_extra_params) |
| 277 |
.def_readwrite("print_summary", |
| 278 |
&AbsolutePoseRefinementOptions::print_summary) |
| 279 |
.def_readwrite("use_position_prior", |
| 280 |
&AbsolutePoseRefinementOptions::use_position_prior) |
| 281 |
.def_readwrite("position_prior_in_world", |
| 282 |
&AbsolutePoseRefinementOptions::position_prior_in_world) |
| 283 |
.def_readwrite("position_prior_covariance", |
| 284 |
&AbsolutePoseRefinementOptions::position_prior_covariance); |
| 285 |
MakeDataclass(PyRefinementOptions); |
| 286 |
|
| 287 |
m.def("estimate_absolute_pose", |
| 288 |
&PyEstimateAbsolutePose, |
| 289 |
"points2D"_a, |
| 290 |
"points3D"_a, |
| 291 |
"camera"_a, |
| 292 |
py::arg_v("estimation_options", |
| 293 |
AbsolutePoseEstimationOptions(), |
| 294 |
"AbsolutePoseEstimationOptions()"), |
| 295 |
"Robustly estimate absolute pose using LO-RANSAC " |
| 296 |
"without non-linear refinement."); |
| 297 |
|
| 298 |
m.def("refine_absolute_pose", |
| 299 |
&PyRefineAbsolutePose, |
| 300 |
"cam_from_world"_a, |
| 301 |
"points2D"_a, |
| 302 |
"points3D"_a, |
| 303 |
"inlier_mask"_a, |
| 304 |
"camera"_a, |
| 305 |
py::arg_v("refinement_options", |
| 306 |
AbsolutePoseRefinementOptions(), |
| 307 |
"AbsolutePoseRefinementOptions()"), |
| 308 |
"return_covariance"_a = false, |
| 309 |
"Non-linear refinement of absolute pose."); |
| 310 |
|
| 311 |
m.def("estimate_and_refine_absolute_pose", |
| 312 |
&PyEstimateAndRefineAbsolutePose, |
| 313 |
"points2D"_a, |
| 314 |
"points3D"_a, |
| 315 |
"camera"_a, |
| 316 |
py::arg_v("estimation_options", |
| 317 |
AbsolutePoseEstimationOptions(), |
| 318 |
"AbsolutePoseEstimationOptions()"), |
| 319 |
py::arg_v("refinement_options", |
| 320 |
AbsolutePoseRefinementOptions(), |
| 321 |
"AbsolutePoseRefinementOptions()"), |
| 322 |
"return_covariance"_a = false, |
| 323 |
"Robust absolute pose estimation with LO-RANSAC " |
| 324 |
"followed by non-linear refinement."); |
| 325 |
|
| 326 |
m.def( |
| 327 |
"estimate_relative_pose", |
| 328 |
&PyEstimateRelativePose, |
| 329 |
"camera1"_a, |
| 330 |
"points2D1"_a, |
| 331 |
"camera2"_a, |
| 332 |
"points2D2"_a, |
| 333 |
py::arg_v("options", RANSACOptions(), "RANSACOptions()"), |
| 334 |
"Robustly estimate relative pose from 2D-2D correspondences (given as " |
| 335 |
"camera + image points) using LO-RANSAC with the tangent Sampson error. " |
| 336 |
"Local optimization non-linearly refines that error over the current " |
| 337 |
"inlier set, but the returned pose receives no final refinement over its " |
| 338 |
"own inliers; use refine_relative_pose for that."); |
| 339 |
m.def("estimate_relative_pose", |
| 340 |
&PyEstimateRelativePoseFromRays, |
| 341 |
"cam_rays1"_a, |
| 342 |
"cam_rays2"_a, |
| 343 |
py::arg_v("options", RANSACOptions(), "RANSACOptions()"), |
| 344 |
"Robustly estimate relative pose from camera rays using LO-RANSAC. " |
| 345 |
"This overload is retained for backwards compatibility."); |
| 346 |
m.def("refine_relative_pose", |
| 347 |
&PyRefineRelativePose, |
| 348 |
"cam2_from_cam1"_a, |
| 349 |
"camera1"_a, |
| 350 |
"points2D1"_a, |
| 351 |
"camera2"_a, |
| 352 |
"points2D2"_a, |
| 353 |
"inlier_mask"_a, |
| 354 |
py::arg_v("options", ceres::Solver::Options()), |
| 355 |
"Non-linear refinement of relative pose from 2D-2D correspondences " |
| 356 |
"(camera + image points) with the tangent Sampson error."); |
| 357 |
m.def("refine_relative_pose", |
| 358 |
&PyRefineRelativePoseFromRays, |
| 359 |
"cam2_from_cam1"_a, |
| 360 |
"cam_rays1"_a, |
| 361 |
"cam_rays2"_a, |
| 362 |
"inlier_mask"_a, |
| 363 |
py::arg_v("options", ceres::Solver::Options()), |
| 364 |
"Non-linear refinement of relative pose from camera rays. This " |
| 365 |
"overload is retained for backwards compatibility."); |
| 366 |
} |