| 1 |
#include "colmap/estimators/two_view_geometry.h" |
| 2 |
|
| 3 |
#include "colmap/feature/types.h" |
| 4 |
#include "colmap/geometry/essential_matrix.h" |
| 5 |
#include "colmap/geometry/homography_matrix.h" |
| 6 |
#include "colmap/geometry/normalization.h" |
| 7 |
#include "colmap/scene/camera.h" |
| 8 |
#include "colmap/scene/two_view_geometry.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 |
void BindTwoViewGeometryEstimator(py::module& m) { |
| 24 |
py::classh<TwoViewGeometryOptions> PyTwoViewGeometryOptions( |
| 25 |
m, "TwoViewGeometryOptions"); |
| 26 |
PyTwoViewGeometryOptions.def(py::init<>()) |
| 27 |
.def_readwrite("min_num_inliers", |
| 28 |
&TwoViewGeometryOptions::min_num_inliers) |
| 29 |
.def_readwrite("min_inlier_ratio", |
| 30 |
&TwoViewGeometryOptions::min_inlier_ratio) |
| 31 |
.def_readwrite("min_E_F_inlier_ratio", |
| 32 |
&TwoViewGeometryOptions::min_E_F_inlier_ratio) |
| 33 |
.def_readwrite("max_H_inlier_ratio", |
| 34 |
&TwoViewGeometryOptions::max_H_inlier_ratio) |
| 35 |
.def_readwrite("watermark_min_inlier_ratio", |
| 36 |
&TwoViewGeometryOptions::watermark_min_inlier_ratio) |
| 37 |
.def_readwrite("watermark_border_size", |
| 38 |
&TwoViewGeometryOptions::watermark_border_size) |
| 39 |
.def_readwrite("detect_watermark", |
| 40 |
&TwoViewGeometryOptions::detect_watermark) |
| 41 |
.def_readwrite("multiple_ignore_watermark", |
| 42 |
&TwoViewGeometryOptions::multiple_ignore_watermark) |
| 43 |
.def_readwrite("watermark_detection_max_error", |
| 44 |
&TwoViewGeometryOptions::watermark_detection_max_error) |
| 45 |
.def_readwrite("filter_stationary_matches", |
| 46 |
&TwoViewGeometryOptions::filter_stationary_matches) |
| 47 |
.def_readwrite("stationary_matches_max_error", |
| 48 |
&TwoViewGeometryOptions::stationary_matches_max_error) |
| 49 |
.def_readwrite("force_H_use", &TwoViewGeometryOptions::force_H_use) |
| 50 |
.def_readwrite("use_degensac", &TwoViewGeometryOptions::use_degensac) |
| 51 |
.def_readwrite("use_sampson_refinement", |
| 52 |
&TwoViewGeometryOptions::use_sampson_refinement) |
| 53 |
.def_readwrite("compute_relative_pose", |
| 54 |
&TwoViewGeometryOptions::compute_relative_pose) |
| 55 |
.def_readwrite("multiple_models", |
| 56 |
&TwoViewGeometryOptions::multiple_models) |
| 57 |
.def_readwrite("ransac", &TwoViewGeometryOptions::ransac_options); |
| 58 |
MakeDataclass(PyTwoViewGeometryOptions); |
| 59 |
|
| 60 |
m.def( |
| 61 |
"estimate_calibrated_two_view_geometry", |
| 62 |
[](const Camera& camera1, |
| 63 |
const std::vector<Eigen::Vector2d>& points1, |
| 64 |
const Camera& camera2, |
| 65 |
const std::vector<Eigen::Vector2d>& points2, |
| 66 |
const FeatureMatchesMatrix* matches_ptr, |
| 67 |
const TwoViewGeometryOptions& options) { |
| 68 |
py::gil_scoped_release release; |
| 69 |
FeatureMatches matches; |
| 70 |
if (matches_ptr != nullptr) { |
| 71 |
matches = MatchesFromMatrix(*matches_ptr); |
| 72 |
} else { |
| 73 |
THROW_CHECK_EQ(points1.size(), points2.size()); |
| 74 |
matches.reserve(points1.size()); |
| 75 |
for (size_t i = 0; i < points1.size(); i++) { |
| 76 |
matches.emplace_back(i, i); |
| 77 |
} |
| 78 |
} |
| 79 |
return EstimateCalibratedTwoViewGeometry( |
| 80 |
camera1, points1, camera2, points2, matches, options); |
| 81 |
}, |
| 82 |
"camera1"_a, |
| 83 |
"points1"_a, |
| 84 |
"camera2"_a, |
| 85 |
"points2"_a, |
| 86 |
"matches"_a = py::none(), |
| 87 |
py::arg_v( |
| 88 |
"options", TwoViewGeometryOptions(), "TwoViewGeometryOptions()")); |
| 89 |
|
| 90 |
m.def( |
| 91 |
"estimate_two_view_geometry", |
| 92 |
[](const Camera& camera1, |
| 93 |
const std::vector<Eigen::Vector2d>& points1, |
| 94 |
const Camera& camera2, |
| 95 |
const std::vector<Eigen::Vector2d>& points2, |
| 96 |
const FeatureMatchesMatrix* matches_ptr, |
| 97 |
const TwoViewGeometryOptions& options) { |
| 98 |
py::gil_scoped_release release; |
| 99 |
FeatureMatches matches; |
| 100 |
if (matches_ptr != nullptr) { |
| 101 |
matches = MatchesFromMatrix(*matches_ptr); |
| 102 |
} else { |
| 103 |
THROW_CHECK_EQ(points1.size(), points2.size()); |
| 104 |
matches.reserve(points1.size()); |
| 105 |
for (size_t i = 0; i < points1.size(); i++) { |
| 106 |
matches.emplace_back(i, i); |
| 107 |
} |
| 108 |
} |
| 109 |
return EstimateTwoViewGeometry( |
| 110 |
camera1, points1, camera2, points2, std::move(matches), options); |
| 111 |
}, |
| 112 |
"camera1"_a, |
| 113 |
"points1"_a, |
| 114 |
"camera2"_a, |
| 115 |
"points2"_a, |
| 116 |
"matches"_a = py::none(), |
| 117 |
py::arg_v( |
| 118 |
"options", TwoViewGeometryOptions(), "TwoViewGeometryOptions()")); |
| 119 |
|
| 120 |
m.def("estimate_two_view_geometry_pose", |
| 121 |
&EstimateTwoViewGeometryPose, |
| 122 |
"camera1"_a, |
| 123 |
"points1"_a, |
| 124 |
"camera2"_a, |
| 125 |
"points2"_a, |
| 126 |
"geometry"_a); |
| 127 |
|
| 128 |
m.def( |
| 129 |
"compute_squared_sampson_error", |
| 130 |
[](const std::vector<Eigen::Vector2d>& points1, |
| 131 |
const std::vector<Eigen::Vector2d>& points2, |
| 132 |
const Eigen::Matrix3d& E) { |
| 133 |
std::vector<double> residuals; |
| 134 |
ComputeSquaredSampsonError(points1, points2, E, &residuals); |
| 135 |
return residuals; |
| 136 |
}, |
| 137 |
"points2D1"_a, |
| 138 |
"points2D2"_a, |
| 139 |
"E"_a, |
| 140 |
"Calculate the squared Sampson error for a given essential or " |
| 141 |
"fundamental matrix.", |
| 142 |
py::call_guard<py::gil_scoped_release>()); |
| 143 |
} |