| 1 |
#include "colmap/feature/types.h" |
| 2 |
|
| 3 |
#include "pycolmap/feature/types.h" |
| 4 |
#include "pycolmap/helpers.h" |
| 5 |
#include "pycolmap/pybind11_extension.h" |
| 6 |
|
| 7 |
#include <pybind11/eigen.h> |
| 8 |
#include <pybind11/pybind11.h> |
| 9 |
#include <pybind11/stl.h> |
| 10 |
|
| 11 |
using namespace colmap; |
| 12 |
using namespace pybind11::literals; |
| 13 |
namespace py = pybind11; |
| 14 |
|
| 15 |
void BindFeatureTypes(py::module& m) { |
| 16 |
py::enum_<FeatureExtractorType>(m, "FeatureExtractorType") |
| 17 |
.value("UNDEFINED", FeatureExtractorType::UNDEFINED) |
| 18 |
.value("SIFT", FeatureExtractorType::SIFT) |
| 19 |
.value("ALIKED_N16ROT", FeatureExtractorType::ALIKED_N16ROT) |
| 20 |
.value("ALIKED_N32", FeatureExtractorType::ALIKED_N32) |
| 21 |
.value("LOMA_B", FeatureExtractorType::LOMA_B) |
| 22 |
.value("LOMA_B128", FeatureExtractorType::LOMA_B128); |
| 23 |
|
| 24 |
// Define both classes first without cross-referencing methods. |
| 25 |
auto PyFeatureDescriptors = |
| 26 |
py::classh<FeatureDescriptors>(m, "FeatureDescriptors") |
| 27 |
.def(py::init<>()) |
| 28 |
.def(py::init<FeatureExtractorType, FeatureDescriptorsData>(), |
| 29 |
"type"_a, |
| 30 |
"data"_a) |
| 31 |
.def_readwrite("type", &FeatureDescriptors::type) |
| 32 |
.def_readwrite("data", &FeatureDescriptors::data); |
| 33 |
auto PyFeatureDescriptorsFloat = |
| 34 |
py::classh<FeatureDescriptorsFloat>(m, "FeatureDescriptorsFloat") |
| 35 |
.def(py::init<>()) |
| 36 |
.def(py::init<FeatureExtractorType, FeatureDescriptorsFloatData>(), |
| 37 |
"type"_a, |
| 38 |
"data"_a) |
| 39 |
.def_readwrite("type", &FeatureDescriptorsFloat::type) |
| 40 |
.def_readwrite("data", &FeatureDescriptorsFloat::data); |
| 41 |
|
| 42 |
// Add cross-referencing methods after both classes are defined. |
| 43 |
PyFeatureDescriptors |
| 44 |
.def_static("from_float", |
| 45 |
&FeatureDescriptors::FromFloat, |
| 46 |
"float_desc"_a, |
| 47 |
"Create from float descriptors by reinterpreting float32 " |
| 48 |
"data as uint8 bytes.") |
| 49 |
.def("to_float", |
| 50 |
&FeatureDescriptors::ToFloat, |
| 51 |
"Convert to float descriptors by reinterpreting uint8 data as " |
| 52 |
"float32."); |
| 53 |
PyFeatureDescriptorsFloat |
| 54 |
.def_static("from_bytes", |
| 55 |
&FeatureDescriptorsFloat::FromBytes, |
| 56 |
"byte_desc"_a, |
| 57 |
"Create from byte descriptors by reinterpreting uint8 " |
| 58 |
"data as float32.") |
| 59 |
.def("to_bytes", |
| 60 |
&FeatureDescriptorsFloat::ToBytes, |
| 61 |
"Convert to byte descriptors by reinterpreting float32 data as " |
| 62 |
"uint8."); |
| 63 |
|
| 64 |
MakeDataclass(PyFeatureDescriptors); |
| 65 |
MakeDataclass(PyFeatureDescriptorsFloat); |
| 66 |
|
| 67 |
auto PyFeatureKeypoint = |
| 68 |
py::classh<FeatureKeypoint>(m, "FeatureKeypoint") |
| 69 |
.def(py::init<>()) |
| 70 |
.def_readwrite("x", &FeatureKeypoint::x) |
| 71 |
.def_readwrite("y", &FeatureKeypoint::y) |
| 72 |
.def_readwrite("a11", &FeatureKeypoint::a11) |
| 73 |
.def_readwrite("a12", &FeatureKeypoint::a12) |
| 74 |
.def_readwrite("a21", &FeatureKeypoint::a21) |
| 75 |
.def_readwrite("a22", &FeatureKeypoint::a22) |
| 76 |
.def_static("from_shape_parameters", |
| 77 |
&FeatureKeypoint::FromShapeParameters) |
| 78 |
.def("rescale", py::overload_cast<float>(&FeatureKeypoint::Rescale)) |
| 79 |
.def("rescale", |
| 80 |
py::overload_cast<float, float>(&FeatureKeypoint::Rescale)) |
| 81 |
.def("compute_scale", &FeatureKeypoint::ComputeScale) |
| 82 |
.def("compute_scale_x", &FeatureKeypoint::ComputeScaleX) |
| 83 |
.def("compute_scale_y", &FeatureKeypoint::ComputeScaleY) |
| 84 |
.def("compute_orientation", &FeatureKeypoint::ComputeOrientation) |
| 85 |
.def("compute_shear", &FeatureKeypoint::ComputeShear) |
| 86 |
.def("__repr__", [](const FeatureKeypoint& keypoint) { |
| 87 |
std::ostringstream ss; |
| 88 |
ss << "FeatureKeypoint(x=" << keypoint.x << ", y=" << keypoint.y |
| 89 |
<< ")"; |
| 90 |
return ss.str(); |
| 91 |
}); |
| 92 |
MakeDataclass(PyFeatureKeypoint); |
| 93 |
py::bind_vector<FeatureKeypoints>(m, "FeatureKeypoints"); |
| 94 |
py::implicitly_convertible<py::iterable, FeatureKeypoints>(); |
| 95 |
|
| 96 |
auto PyFeatureMatch = |
| 97 |
py::classh<FeatureMatch>(m, "FeatureMatch") |
| 98 |
.def(py::init<>()) |
| 99 |
.def(py::init<const point2D_t, const point2D_t>()) |
| 100 |
.def_readwrite("point2D_idx1", &FeatureMatch::point2D_idx1) |
| 101 |
.def_readwrite("point2D_idx2", &FeatureMatch::point2D_idx2) |
| 102 |
.def("__repr__", [](const FeatureMatch& match) { |
| 103 |
std::ostringstream ss; |
| 104 |
ss << "FeatureMatch(idx1=" << match.point2D_idx1 |
| 105 |
<< ", idx2=" << match.point2D_idx2 << ")"; |
| 106 |
return ss.str(); |
| 107 |
}); |
| 108 |
MakeDataclass(PyFeatureMatch); |
| 109 |
py::bind_vector<FeatureMatches>(m, "FeatureMatches"); |
| 110 |
py::implicitly_convertible<py::iterable, FeatureMatches>(); |
| 111 |
|
| 112 |
m.def("keypoints_to_matrix", |
| 113 |
&KeypointsToMatrix, |
| 114 |
"keypoints"_a, |
| 115 |
"Convert FeatureKeypoints to an Nx4 matrix " |
| 116 |
"[x, y, scale, orientation]."); |
| 117 |
|
| 118 |
m.def("keypoints_from_matrix", |
| 119 |
&KeypointsFromMatrix, |
| 120 |
"keypoints"_a, |
| 121 |
"Convert an Nx4 matrix [x, y, scale, orientation] to " |
| 122 |
"FeatureKeypoints."); |
| 123 |
|
| 124 |
m.def("matches_to_matrix", |
| 125 |
&MatchesToMatrix, |
| 126 |
"matches"_a, |
| 127 |
"Convert FeatureMatches to an Nx2 matrix of point2D indices."); |
| 128 |
|
| 129 |
m.def("matches_from_matrix", |
| 130 |
&MatchesFromMatrix, |
| 131 |
"matches"_a, |
| 132 |
"Convert an Nx2 matrix of point2D indices to FeatureMatches."); |
| 133 |
} |