Debian Codegraph @

src/pycolmap/estimators/homography_matrix.cc

raw
1 #include "colmap/estimators/solvers/homography_matrix.h"
2
3 #include "colmap/math/random.h"
4 #include "colmap/optim/loransac.h"
5 #include "colmap/optim/support_measurement.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> PyEstimateHomographyMatrix(
21 const std::vector<Eigen::Vector2d>& points2D1,
22 const std::vector<Eigen::Vector2d>& points2D2,
23 const RANSACOptions& options) {
24 py::gil_scoped_release release;
25 THROW_CHECK_EQ(points2D1.size(), points2D2.size());
26 LORANSAC<HomographyMatrixEstimator,
27 HomographyMatrixEstimator,
28 MEstimatorSupportMeasurer>
29 ransac(options);
30 const auto report = ransac.Estimate(points2D1, points2D2);
31 py::gil_scoped_acquire acquire;
32 if (!report.success) {
33 return py::none();
34 }
35 return py::dict("H"_a = report.model,
36 "num_inliers"_a = report.support.num_inliers,
37 "inlier_mask"_a = ToPythonMask(report.inlier_mask));
38 }
39
40 void BindHomographyMatrixEstimator(py::module& m) {
41 auto est_options = m.attr("RANSACOptions")().cast<RANSACOptions>();
42
43 m.def("estimate_homography_matrix",
44 &PyEstimateHomographyMatrix,
45 "points2D1"_a,
46 "points2D2"_a,
47 py::arg_v("estimation_options", est_options, "RANSACOptions()"),
48 "Robustly estimate homography matrix using LO-RANSAC.");
49 }