| 1 |
#include "colmap/scene/correspondence_graph.h" |
| 2 |
|
| 3 |
#include "colmap/feature/types.h" |
| 4 |
#include "colmap/util/types.h" |
| 5 |
|
| 6 |
#include "pycolmap/helpers.h" |
| 7 |
#include "pycolmap/scene/types.h" |
| 8 |
#include "pycolmap/utils.h" |
| 9 |
|
| 10 |
#include <pybind11/eigen.h> |
| 11 |
#include <pybind11/pybind11.h> |
| 12 |
#include <pybind11/stl.h> |
| 13 |
#include <pybind11/stl_bind.h> |
| 14 |
|
| 15 |
using namespace colmap; |
| 16 |
using namespace pybind11::literals; |
| 17 |
namespace py = pybind11; |
| 18 |
|
| 19 |
void BindCorrespondenceGraph(py::module& m) { |
| 20 |
py::classh<CorrespondenceGraph::Correspondence> PyCorrespondence( |
| 21 |
m, "Correspondence"); |
| 22 |
PyCorrespondence.def(py::init<>()) |
| 23 |
.def(py::init<image_t, point2D_t>(), "image_id"_a, "point2D_idx"_a) |
| 24 |
.def_readwrite("image_id", &CorrespondenceGraph::Correspondence::image_id) |
| 25 |
.def_readwrite("point2D_idx", |
| 26 |
&CorrespondenceGraph::Correspondence::point2D_idx); |
| 27 |
MakeDataclass(PyCorrespondence); |
| 28 |
|
| 29 |
py::class_<CorrespondenceGraph::CorrespondenceRange>(m, "CorrespondenceRange") |
| 30 |
.def_property_readonly( |
| 31 |
"empty", |
| 32 |
[](const CorrespondenceGraph::CorrespondenceRange& self) { |
| 33 |
return self.beg == self.end; |
| 34 |
}, |
| 35 |
"Whether the range is empty.") |
| 36 |
.def( |
| 37 |
"to_list", |
| 38 |
[](const CorrespondenceGraph::CorrespondenceRange& self) { |
| 39 |
return std::vector<CorrespondenceGraph::Correspondence>(self.beg, |
| 40 |
self.end); |
| 41 |
}, |
| 42 |
"Convert range to list of correspondences."); |
| 43 |
|
| 44 |
auto PyCorrespondenceGraph = |
| 45 |
py::classh<CorrespondenceGraph>(m, "CorrespondenceGraph"); |
| 46 |
PyCorrespondenceGraph.def(py::init<>()) |
| 47 |
.def("finalize", &CorrespondenceGraph::Finalize) |
| 48 |
.def("num_images", &CorrespondenceGraph::NumImages) |
| 49 |
.def("num_image_pairs", &CorrespondenceGraph::NumImagePairs) |
| 50 |
.def("num_observations_for_image", |
| 51 |
&CorrespondenceGraph::NumObservationsForImage, |
| 52 |
"image_id"_a) |
| 53 |
.def("num_correspondences_for_image", |
| 54 |
&CorrespondenceGraph::NumCorrespondencesForImage, |
| 55 |
"image_id"_a) |
| 56 |
.def("num_matches_between_images", |
| 57 |
py::overload_cast<image_t, image_t>( |
| 58 |
&CorrespondenceGraph::NumMatchesBetweenImages, py::const_), |
| 59 |
"image_id1"_a, |
| 60 |
"image_id2"_a) |
| 61 |
.def("num_matches_between_all_images", |
| 62 |
py::overload_cast<>(&CorrespondenceGraph::NumMatchesBetweenAllImages, |
| 63 |
py::const_)) |
| 64 |
.def("exists_image", &CorrespondenceGraph::ExistsImage, "image_id"_a) |
| 65 |
.def("image_pairs", &CorrespondenceGraph::ImagePairs) |
| 66 |
.def("add_image", |
| 67 |
&CorrespondenceGraph::AddImage, |
| 68 |
"image_id"_a, |
| 69 |
"num_points2D"_a) |
| 70 |
.def("add_two_view_geometry", |
| 71 |
&CorrespondenceGraph::AddTwoViewGeometry, |
| 72 |
"image_id1"_a, |
| 73 |
"image_id2"_a, |
| 74 |
"two_view_geometry"_a) |
| 75 |
.def( |
| 76 |
"extract_correspondences", |
| 77 |
[](const CorrespondenceGraph& self, |
| 78 |
const image_t image_id, |
| 79 |
const point2D_t point2D_idx) { |
| 80 |
std::vector<CorrespondenceGraph::Correspondence> correspondences; |
| 81 |
self.ExtractCorrespondences( |
| 82 |
image_id, point2D_idx, &correspondences); |
| 83 |
return correspondences; |
| 84 |
}, |
| 85 |
"image_id"_a, |
| 86 |
"point2D_idx"_a) |
| 87 |
.def( |
| 88 |
"extract_transitive_correspondences", |
| 89 |
[](const CorrespondenceGraph& self, |
| 90 |
const image_t image_id, |
| 91 |
const point2D_t point2D_idx, |
| 92 |
const size_t transitivity) { |
| 93 |
std::vector<CorrespondenceGraph::Correspondence> correspondences; |
| 94 |
self.ExtractTransitiveCorrespondences( |
| 95 |
image_id, point2D_idx, transitivity, &correspondences); |
| 96 |
return correspondences; |
| 97 |
}, |
| 98 |
"image_id"_a, |
| 99 |
"point2D_idx"_a, |
| 100 |
"transitivity"_a) |
| 101 |
.def( |
| 102 |
"extract_matches_between_images", |
| 103 |
[](const CorrespondenceGraph& self, |
| 104 |
const image_t image_id1, |
| 105 |
const image_t image_id2) -> FeatureMatchesMatrix { |
| 106 |
FeatureMatches matches; |
| 107 |
self.ExtractMatchesBetweenImages(image_id1, image_id2, matches); |
| 108 |
return MatchesToMatrix(matches); |
| 109 |
}, |
| 110 |
"image_id1"_a, |
| 111 |
"image_id2"_a) |
| 112 |
.def("extract_two_view_geometry", |
| 113 |
&CorrespondenceGraph::ExtractTwoViewGeometry, |
| 114 |
"image_id1"_a, |
| 115 |
"image_id2"_a, |
| 116 |
"extract_inlier_matches"_a) |
| 117 |
.def("has_correspondences", |
| 118 |
&CorrespondenceGraph::HasCorrespondences, |
| 119 |
"image_id"_a, |
| 120 |
"point2D_idx"_a) |
| 121 |
.def("find_correspondences", |
| 122 |
&CorrespondenceGraph::FindCorrespondences, |
| 123 |
"image_id"_a, |
| 124 |
"point2D_idx"_a, |
| 125 |
"Find range of correspondences of an image observation.") |
| 126 |
.def("is_two_view_observation", |
| 127 |
&CorrespondenceGraph::IsTwoViewObservation, |
| 128 |
"image_id"_a, |
| 129 |
"point2D_idx"_a) |
| 130 |
.def("__copy__", |
| 131 |
[](const CorrespondenceGraph& self) { |
| 132 |
return CorrespondenceGraph(self); |
| 133 |
}) |
| 134 |
.def("__deepcopy__", |
| 135 |
[](const CorrespondenceGraph& self, const py::dict&) { |
| 136 |
return CorrespondenceGraph(self); |
| 137 |
}) |
| 138 |
.def("__repr__", &CreateRepresentation<CorrespondenceGraph>); |
| 139 |
DefDeprecation(PyCorrespondenceGraph, |
| 140 |
"num_correspondences_between_images", |
| 141 |
"num_matches_between_images"); |
| 142 |
DefDeprecation(PyCorrespondenceGraph, |
| 143 |
"num_correspondences_between_all_images", |
| 144 |
"num_matches_between_all_images"); |
| 145 |
DefDeprecation(PyCorrespondenceGraph, |
| 146 |
"find_correspondences_between_images", |
| 147 |
"extract_matches_between_images"); |
| 148 |
} |