Debian Codegraph @

src/pycolmap/estimators/fundamental_matrix.cc

raw
1 #include "colmap/estimators/solvers/fundamental_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/scene/camera.h"
7 #include "colmap/util/logging.h"
8
9 #include "pycolmap/helpers.h"
10 #include "pycolmap/pybind11_extension.h"
11 #include "pycolmap/utils.h"
12
13 #include <pybind11/eigen.h>
14 #include <pybind11/pybind11.h>
15 #include <pybind11/stl.h>
16
17 using namespace colmap;
18 using namespace pybind11::literals;
19 namespace py = pybind11;
20
21 py::typing::Optional<py::dict> PyEstimateFundamentalMatrix(
22 const std::vector<Eigen::Vector2d>& points2D1,
23 const std::vector<Eigen::Vector2d>& points2D2,
24 const RANSACOptions& options,
25 const bool use_sampson_refinement) {
26 py::gil_scoped_release release;
27 THROW_CHECK_EQ(points2D1.size(), points2D2.size());
28 const auto report = use_sampson_refinement
29 ? LORANSAC<FundamentalMatrixSevenPointEstimator,
30 FundamentalMatrixSampsonEstimator,
31 MEstimatorSupportMeasurer>(options)
32 .Estimate(points2D1, points2D2)
33 : LORANSAC<FundamentalMatrixSevenPointEstimator,
34 FundamentalMatrixEightPointEstimator,
35 MEstimatorSupportMeasurer>(options)
36 .Estimate(points2D1, points2D2);
37 py::gil_scoped_acquire acquire;
38 if (!report.success) {
39 return py::none();
40 }
41
42 return py::dict("F"_a = report.model,
43 "num_inliers"_a = report.support.num_inliers,
44 "inlier_mask"_a = ToPythonMask(report.inlier_mask));
45 }
46
47 void BindFundamentalMatrixEstimator(py::module& m) {
48 auto ransac_options = m.attr("RANSACOptions")().cast<RANSACOptions>();
49
50 m.def("estimate_fundamental_matrix",
51 &PyEstimateFundamentalMatrix,
52 "points2D1"_a,
53 "points2D2"_a,
54 py::arg_v("estimation_options", ransac_options, "RANSACOptions()"),
55 "use_sampson_refinement"_a = true,
56 "Robustly estimate fundamental matrix with LO-RANSAC.");
57 }