| 1 |
#include "colmap/estimators/solvers/essential_matrix.h" |
| 2 |
|
| 3 |
#include "colmap/geometry/essential_matrix.h" |
| 4 |
#include "colmap/geometry/rigid3.h" |
| 5 |
#include "colmap/math/random.h" |
| 6 |
#include "colmap/optim/loransac.h" |
| 7 |
#include "colmap/optim/support_measurement.h" |
| 8 |
#include "colmap/scene/camera.h" |
| 9 |
#include "colmap/util/logging.h" |
| 10 |
|
| 11 |
#include "pycolmap/helpers.h" |
| 12 |
#include "pycolmap/pybind11_extension.h" |
| 13 |
#include "pycolmap/utils.h" |
| 14 |
|
| 15 |
#include <pybind11/eigen.h> |
| 16 |
#include <pybind11/pybind11.h> |
| 17 |
#include <pybind11/stl.h> |
| 18 |
|
| 19 |
using namespace colmap; |
| 20 |
using namespace pybind11::literals; |
| 21 |
namespace py = pybind11; |
| 22 |
|
| 23 |
py::typing::Optional<py::dict> PyEstimateAndDecomposeEssentialMatrix( |
| 24 |
const std::vector<Eigen::Vector2d>& points2D1, |
| 25 |
const std::vector<Eigen::Vector2d>& points2D2, |
| 26 |
const Camera& camera1, |
| 27 |
const Camera& camera2, |
| 28 |
const RANSACOptions& options) { |
| 29 |
py::gil_scoped_release release; |
| 30 |
|
| 31 |
THROW_CHECK_EQ(points2D1.size(), points2D2.size()); |
| 32 |
const size_t num_points2D = points2D1.size(); |
| 33 |
|
| 34 |
// Unproject to rays + per-ray Jacobians (for the pixel-unit tangent Sampson |
| 35 |
// score). End users pass camera + 2D points. Jacobians are never part of the |
| 36 |
// interface. Unprojectable points are zeroed -> infinite residual -> |
| 37 |
// rejected. |
| 38 |
std::vector<CamRayWithJac> cam_rays1_with_jac(num_points2D); |
| 39 |
std::vector<CamRayWithJac> cam_rays2_with_jac(num_points2D); |
| 40 |
for (size_t point2D_idx = 0; point2D_idx < num_points2D; ++point2D_idx) { |
| 41 |
cam_rays1_with_jac[point2D_idx] = |
| 42 |
camera1.CamRayFromImgWithJac(points2D1[point2D_idx]) |
| 43 |
.value_or(CamRayWithJac::Zero()); |
| 44 |
cam_rays2_with_jac[point2D_idx] = |
| 45 |
camera2.CamRayFromImgWithJac(points2D2[point2D_idx]) |
| 46 |
.value_or(CamRayWithJac::Zero()); |
| 47 |
} |
| 48 |
|
| 49 |
// The tangent Sampson residual is in pixels, so the pixel threshold is used |
| 50 |
// directly (no CamFromImgThreshold / focal-length conversion). |
| 51 |
LORANSAC<EssentialMatrixTangentSampsonEstimator, |
| 52 |
EssentialMatrixTangentSampsonEstimator, |
| 53 |
MEstimatorSupportMeasurer> |
| 54 |
ransac(options); |
| 55 |
|
| 56 |
// Essential matrix estimation. |
| 57 |
const auto report = ransac.Estimate(cam_rays1_with_jac, cam_rays2_with_jac); |
| 58 |
|
| 59 |
if (!report.success) { |
| 60 |
py::gil_scoped_acquire acquire; |
| 61 |
return py::none(); |
| 62 |
} |
| 63 |
|
| 64 |
// Pose from essential matrix. |
| 65 |
std::vector<Eigen::Vector3d> inlier_cam_rays1; |
| 66 |
inlier_cam_rays1.reserve(report.support.num_inliers); |
| 67 |
std::vector<Eigen::Vector3d> inlier_cam_rays2; |
| 68 |
inlier_cam_rays2.reserve(report.support.num_inliers); |
| 69 |
for (size_t point2D_idx = 0; point2D_idx < num_points2D; ++point2D_idx) { |
| 70 |
if (report.inlier_mask[point2D_idx]) { |
| 71 |
inlier_cam_rays1.push_back(cam_rays1_with_jac[point2D_idx].ray); |
| 72 |
inlier_cam_rays2.push_back(cam_rays2_with_jac[point2D_idx].ray); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
Rigid3d cam2_from_cam1; |
| 77 |
std::vector<int> valid_indices; |
| 78 |
PoseFromEssentialMatrix(report.model, |
| 79 |
inlier_cam_rays1, |
| 80 |
inlier_cam_rays2, |
| 81 |
&cam2_from_cam1, |
| 82 |
&valid_indices); |
| 83 |
|
| 84 |
py::gil_scoped_acquire acquire; |
| 85 |
return py::dict("E"_a = report.model, |
| 86 |
"cam2_from_cam1"_a = cam2_from_cam1, |
| 87 |
"num_inliers"_a = report.support.num_inliers, |
| 88 |
"inlier_mask"_a = ToPythonMask(report.inlier_mask)); |
| 89 |
} |
| 90 |
|
| 91 |
void BindEssentialMatrixEstimator(py::module& m) { |
| 92 |
auto ransac_options = m.attr("RANSACOptions")().cast<RANSACOptions>(); |
| 93 |
|
| 94 |
m.def("estimate_essential_matrix", |
| 95 |
&PyEstimateAndDecomposeEssentialMatrix, |
| 96 |
"points2D1"_a, |
| 97 |
"points2D2"_a, |
| 98 |
"camera1"_a, |
| 99 |
"camera2"_a, |
| 100 |
py::arg_v("estimation_options", ransac_options, "RANSACOptions()"), |
| 101 |
"Robustly estimate essential matrix with LO-RANSAC and decompose it " |
| 102 |
"using the cheirality check."); |
| 103 |
} |