| 1 |
#include "colmap/estimators/solvers/affine_transform.h" |
| 2 |
|
| 3 |
#include "colmap/math/random.h" |
| 4 |
#include "colmap/optim/loransac.h" |
| 5 |
#include "colmap/util/logging.h" |
| 6 |
|
| 7 |
#include "pycolmap/helpers.h" |
| 8 |
#include "pycolmap/pybind11_extension.h" |
| 9 |
#include "pycolmap/utils.h" |
| 10 |
|
| 11 |
#include <pybind11/eigen.h> |
| 12 |
#include <pybind11/pybind11.h> |
| 13 |
#include <pybind11/stl.h> |
| 14 |
|
| 15 |
using namespace colmap; |
| 16 |
using namespace pybind11::literals; |
| 17 |
namespace py = pybind11; |
| 18 |
|
| 19 |
void BindAffineTransformEstimator(py::module& m) { |
| 20 |
auto ransac_options = m.attr("RANSACOptions")().cast<RANSACOptions>(); |
| 21 |
|
| 22 |
m.def( |
| 23 |
"estimate_affine2d", |
| 24 |
[](const std::vector<Eigen::Vector2d>& src, |
| 25 |
const std::vector<Eigen::Vector2d>& tgt) |
| 26 |
-> py::typing::Optional<Eigen::Matrix2x3d> { |
| 27 |
py::gil_scoped_release release; |
| 28 |
Eigen::Matrix2x3d tgt_from_src; |
| 29 |
const bool success = EstimateAffine2d(src, tgt, tgt_from_src); |
| 30 |
py::gil_scoped_acquire acquire; |
| 31 |
if (success) { |
| 32 |
return py::cast(tgt_from_src); |
| 33 |
} else { |
| 34 |
return py::none(); |
| 35 |
} |
| 36 |
}, |
| 37 |
"src"_a, |
| 38 |
"tgt"_a, |
| 39 |
"Estimate the 2D affine transform tgt_from_src."); |
| 40 |
|
| 41 |
m.def( |
| 42 |
"estimate_affine2d_robust", |
| 43 |
[](const std::vector<Eigen::Vector2d>& src, |
| 44 |
const std::vector<Eigen::Vector2d>& tgt, |
| 45 |
const RANSACOptions& options) -> py::typing::Optional<py::dict> { |
| 46 |
py::gil_scoped_release release; |
| 47 |
Eigen::Matrix2x3d tgt_from_src; |
| 48 |
const auto report = |
| 49 |
EstimateAffine2dRobust(src, tgt, options, tgt_from_src); |
| 50 |
py::gil_scoped_acquire acquire; |
| 51 |
if (!report.success) { |
| 52 |
return py::none(); |
| 53 |
} |
| 54 |
return py::dict("tgt_from_src"_a = tgt_from_src, |
| 55 |
"num_inliers"_a = report.support.num_inliers, |
| 56 |
"inlier_mask"_a = ToPythonMask(report.inlier_mask)); |
| 57 |
}, |
| 58 |
"src"_a, |
| 59 |
"tgt"_a, |
| 60 |
py::arg_v("estimation_options", ransac_options, "RANSACOptions()"), |
| 61 |
"Robustly estimate the 2D affine transform tgt_from_src using " |
| 62 |
"LO-RANSAC."); |
| 63 |
} |