| 1 |
#include "colmap/estimators/generalized_pose.h" |
| 2 |
|
| 3 |
#include "colmap/estimators/pose.h" |
| 4 |
#include "colmap/geometry/rigid3.h" |
| 5 |
#include "colmap/math/random.h" |
| 6 |
#include "colmap/optim/ransac.h" |
| 7 |
#include "colmap/scene/camera.h" |
| 8 |
#include "colmap/util/logging.h" |
| 9 |
|
| 10 |
#include "pycolmap/helpers.h" |
| 11 |
#include "pycolmap/pybind11_extension.h" |
| 12 |
#include "pycolmap/utils.h" |
| 13 |
|
| 14 |
#include <pybind11/eigen.h> |
| 15 |
#include <pybind11/pybind11.h> |
| 16 |
#include <pybind11/stl.h> |
| 17 |
|
| 18 |
using namespace colmap; |
| 19 |
using namespace pybind11::literals; |
| 20 |
namespace py = pybind11; |
| 21 |
|
| 22 |
py::typing::Optional<py::dict> PyEstimateGeneralizedAbsolutePose( |
| 23 |
const std::vector<Eigen::Vector2d>& points2D, |
| 24 |
const std::vector<Eigen::Vector3d>& points3D, |
| 25 |
const std::vector<size_t>& camera_idxs, |
| 26 |
const std::vector<Rigid3d>& cams_from_rig, |
| 27 |
const std::vector<Camera>& cameras, |
| 28 |
const RANSACOptions& estimation_options) { |
| 29 |
py::gil_scoped_release release; |
| 30 |
Rigid3d rig_from_world; |
| 31 |
size_t num_inliers; |
| 32 |
std::vector<char> inlier_mask; |
| 33 |
if (!EstimateGeneralizedAbsolutePose(estimation_options, |
| 34 |
points2D, |
| 35 |
points3D, |
| 36 |
camera_idxs, |
| 37 |
cams_from_rig, |
| 38 |
cameras, |
| 39 |
&rig_from_world, |
| 40 |
&num_inliers, |
| 41 |
&inlier_mask)) { |
| 42 |
py::gil_scoped_acquire acquire; |
| 43 |
return py::none(); |
| 44 |
} |
| 45 |
|
| 46 |
py::gil_scoped_acquire acquire; |
| 47 |
return py::dict("rig_from_world"_a = rig_from_world, |
| 48 |
"num_inliers"_a = num_inliers, |
| 49 |
"inlier_mask"_a = ToPythonMask(inlier_mask)); |
| 50 |
} |
| 51 |
|
| 52 |
py::typing::Optional<py::dict> PyRefineGeneralizedAbsolutePose( |
| 53 |
const Rigid3d& init_rig_from_world, |
| 54 |
const std::vector<Eigen::Vector2d>& points2D, |
| 55 |
const std::vector<Eigen::Vector3d>& points3D, |
| 56 |
const PyInlierMask& inlier_mask, |
| 57 |
const std::vector<size_t>& camera_idxs, |
| 58 |
const std::vector<Rigid3d>& cams_from_rig, |
| 59 |
std::vector<Camera>& cameras, |
| 60 |
const AbsolutePoseRefinementOptions& refinement_options, |
| 61 |
const bool return_covariance) { |
| 62 |
py::gil_scoped_release release; |
| 63 |
Rigid3d refined_rig_from_world = init_rig_from_world; |
| 64 |
std::vector<char> inlier_mask_char(inlier_mask.size()); |
| 65 |
Eigen::Map<Eigen::Matrix<char, Eigen::Dynamic, 1>>( |
| 66 |
inlier_mask_char.data(), inlier_mask.size()) = inlier_mask.cast<char>(); |
| 67 |
Eigen::Matrix<double, 6, 6> covariance; |
| 68 |
if (!RefineGeneralizedAbsolutePose( |
| 69 |
refinement_options, |
| 70 |
inlier_mask_char, |
| 71 |
points2D, |
| 72 |
points3D, |
| 73 |
camera_idxs, |
| 74 |
cams_from_rig, |
| 75 |
&refined_rig_from_world, |
| 76 |
&cameras, |
| 77 |
return_covariance ? &covariance : nullptr)) { |
| 78 |
py::gil_scoped_acquire acquire; |
| 79 |
return py::none(); |
| 80 |
} |
| 81 |
py::gil_scoped_acquire acquire; |
| 82 |
py::dict result("rig_from_world"_a = refined_rig_from_world); |
| 83 |
if (return_covariance) result["covariance"] = covariance; |
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
py::typing::Optional<py::dict> PyEstimateAndRefineGeneralizedAbsolutePose( |
| 88 |
const std::vector<Eigen::Vector2d>& points2D, |
| 89 |
const std::vector<Eigen::Vector3d>& points3D, |
| 90 |
const std::vector<size_t>& camera_idxs, |
| 91 |
const std::vector<Rigid3d>& cams_from_rig, |
| 92 |
std::vector<Camera>& cameras, |
| 93 |
const RANSACOptions& ransac_options, |
| 94 |
const AbsolutePoseRefinementOptions& refinement_options, |
| 95 |
const bool return_covariance) { |
| 96 |
py::gil_scoped_release release; |
| 97 |
Rigid3d rig_from_world; |
| 98 |
size_t num_inliers; |
| 99 |
std::vector<char> inlier_mask; |
| 100 |
if (!EstimateGeneralizedAbsolutePose(ransac_options, |
| 101 |
points2D, |
| 102 |
points3D, |
| 103 |
camera_idxs, |
| 104 |
cams_from_rig, |
| 105 |
cameras, |
| 106 |
&rig_from_world, |
| 107 |
&num_inliers, |
| 108 |
&inlier_mask)) { |
| 109 |
py::gil_scoped_acquire acquire; |
| 110 |
return py::none(); |
| 111 |
} |
| 112 |
|
| 113 |
Eigen::Matrix<double, 6, 6> covariance; |
| 114 |
if (!RefineGeneralizedAbsolutePose( |
| 115 |
refinement_options, |
| 116 |
inlier_mask, |
| 117 |
points2D, |
| 118 |
points3D, |
| 119 |
camera_idxs, |
| 120 |
cams_from_rig, |
| 121 |
&rig_from_world, |
| 122 |
&cameras, |
| 123 |
return_covariance ? &covariance : nullptr)) { |
| 124 |
py::gil_scoped_acquire acquire; |
| 125 |
return py::none(); |
| 126 |
} |
| 127 |
|
| 128 |
py::gil_scoped_acquire acquire; |
| 129 |
py::dict dict("rig_from_world"_a = rig_from_world, |
| 130 |
"num_inliers"_a = num_inliers, |
| 131 |
"inlier_mask"_a = ToPythonMask(inlier_mask)); |
| 132 |
if (return_covariance) dict["covariance"] = covariance; |
| 133 |
return dict; |
| 134 |
} |
| 135 |
|
| 136 |
py::typing::Optional<py::dict> PyEstimateGeneralizedRelativePose( |
| 137 |
const std::vector<Eigen::Vector2d>& points2D1, |
| 138 |
const std::vector<Eigen::Vector2d>& points2D2, |
| 139 |
const std::vector<size_t>& camera_idxs1, |
| 140 |
const std::vector<size_t>& camera_idxs2, |
| 141 |
const std::vector<Rigid3d>& cams_from_rig, |
| 142 |
const std::vector<Camera>& cameras, |
| 143 |
const RANSACOptions& estimation_options) { |
| 144 |
py::gil_scoped_release release; |
| 145 |
std::optional<Rigid3d> rig2_from_rig1; |
| 146 |
std::optional<Rigid3d> pano2_from_pano1; |
| 147 |
size_t num_inliers; |
| 148 |
std::vector<char> inlier_mask; |
| 149 |
if (!EstimateGeneralizedRelativePose(estimation_options, |
| 150 |
points2D1, |
| 151 |
points2D2, |
| 152 |
camera_idxs1, |
| 153 |
camera_idxs2, |
| 154 |
cams_from_rig, |
| 155 |
cameras, |
| 156 |
&rig2_from_rig1, |
| 157 |
&pano2_from_pano1, |
| 158 |
&num_inliers, |
| 159 |
&inlier_mask)) { |
| 160 |
py::gil_scoped_acquire acquire; |
| 161 |
return py::none(); |
| 162 |
} |
| 163 |
|
| 164 |
py::gil_scoped_acquire acquire; |
| 165 |
py::dict dict("num_inliers"_a = num_inliers, |
| 166 |
"inlier_mask"_a = ToPythonMask(inlier_mask)); |
| 167 |
if (rig2_from_rig1) dict["rig2_from_rig1"] = *rig2_from_rig1; |
| 168 |
if (pano2_from_pano1) dict["pano2_from_pano1"] = *pano2_from_pano1; |
| 169 |
return dict; |
| 170 |
} |
| 171 |
|
| 172 |
void BindGeneralizedAbsolutePoseEstimator(py::module& m) { |
| 173 |
m.def("estimate_generalized_absolute_pose", |
| 174 |
&PyEstimateGeneralizedAbsolutePose, |
| 175 |
"points2D"_a, |
| 176 |
"points3D"_a, |
| 177 |
"camera_idxs"_a, |
| 178 |
"cams_from_rig"_a, |
| 179 |
"cameras"_a, |
| 180 |
py::arg_v("estimation_options", RANSACOptions(), "RANSACOptions()")); |
| 181 |
|
| 182 |
m.def("refine_generalized_absolute_pose", |
| 183 |
&PyRefineGeneralizedAbsolutePose, |
| 184 |
"rig_from_world"_a, |
| 185 |
"points2D"_a, |
| 186 |
"points3D"_a, |
| 187 |
"inlier_mask"_a, |
| 188 |
"camera_idxs"_a, |
| 189 |
"cams_from_rig"_a, |
| 190 |
"cameras"_a, |
| 191 |
py::arg_v("refinement_options", |
| 192 |
AbsolutePoseRefinementOptions(), |
| 193 |
"AbsolutePoseRefinementOptions()"), |
| 194 |
"return_covariance"_a = false, |
| 195 |
"Robustly estimate generalized absolute pose using LO-RANSAC" |
| 196 |
"followed by non-linear refinement."); |
| 197 |
|
| 198 |
m.def("estimate_and_refine_generalized_absolute_pose", |
| 199 |
&PyEstimateAndRefineGeneralizedAbsolutePose, |
| 200 |
"points2D"_a, |
| 201 |
"points3D"_a, |
| 202 |
"camera_idxs"_a, |
| 203 |
"cams_from_rig"_a, |
| 204 |
"cameras"_a, |
| 205 |
py::arg_v("estimation_options", RANSACOptions(), "RANSACOptions()"), |
| 206 |
py::arg_v("refinement_options", |
| 207 |
AbsolutePoseRefinementOptions(), |
| 208 |
"AbsolutePoseRefinementOptions()"), |
| 209 |
"return_covariance"_a = false, |
| 210 |
"Robustly estimate generalized absolute pose using LO-RANSAC" |
| 211 |
"followed by non-linear refinement."); |
| 212 |
|
| 213 |
m.def("estimate_generalized_relative_pose", |
| 214 |
&PyEstimateGeneralizedRelativePose, |
| 215 |
"points2D1"_a, |
| 216 |
"points2D2"_a, |
| 217 |
"camera_idxs1"_a, |
| 218 |
"camera_idxs2"_a, |
| 219 |
"cams_from_rig"_a, |
| 220 |
"cameras"_a, |
| 221 |
py::arg_v("estimation_options", RANSACOptions(), "RANSACOptions()")); |
| 222 |
} |