Debian Codegraph @

src/pycolmap/scene/image.cc

raw
1 #include "colmap/scene/image.h"
2
3 #include "colmap/geometry/rigid3.h"
4 #include "colmap/scene/point2d.h"
5 #include "colmap/util/logging.h"
6 #include "colmap/util/misc.h"
7 #include "colmap/util/types.h"
8
9 #include "pycolmap/helpers.h"
10 #include "pycolmap/pybind11_extension.h"
11 #include "pycolmap/scene/types.h"
12
13 #include <memory>
14 #include <optional>
15 #include <sstream>
16
17 #include <pybind11/eigen.h>
18 #include <pybind11/pybind11.h>
19 #include <pybind11/stl.h>
20 #include <pybind11/stl_bind.h>
21
22 using namespace colmap;
23 using namespace pybind11::literals;
24 namespace py = pybind11;
25
26 template <typename T>
27 std::shared_ptr<Image> MakeImage(const std::string& name,
28 const std::vector<T>& points2D,
29 camera_t camera_id,
30 image_t image_id) {
31 auto image = std::make_shared<Image>();
32 image->SetName(name);
33 image->SetPoints2D(points2D);
34 if (camera_id != kInvalidCameraId) {
35 image->SetCameraId(camera_id);
36 }
37 image->SetImageId(image_id);
38 return image;
39 }
40
41 void BindSceneImage(py::module& m) {
42 py::classh<Image> PyImage(m, "Image");
43 PyImage.def(py::init<>())
44 .def(py::init(&MakeImage<Point2D>),
45 "name"_a = "",
46 py::arg_v("points2D", Point2DVector(), "Point2DList()"),
47 py::arg_v(
48 "camera_id", kInvalidCameraId, "pycolmap.INVALID_CAMERA_ID"),
49 py::arg_v("image_id", kInvalidImageId, "pycolmap.INVALID_IMAGE_ID"))
50 .def(py::init(&MakeImage<Eigen::Vector2d>),
51 "name"_a = "",
52 "keypoints"_a = std::vector<Eigen::Vector2d>(),
53 py::arg_v(
54 "camera_id", kInvalidCameraId, "pycolmap.INVALID_CAMERA_ID"),
55 py::arg_v("image_id", kInvalidImageId, "pycolmap.INVALID_IMAGE_ID"))
56 .def_property("image_id",
57 &Image::ImageId,
58 &Image::SetImageId,
59 "Unique identifier of the image.")
60 .def_property("camera_id",
61 &Image::CameraId,
62 &Image::SetCameraId,
63 "Unique identifier of the camera.")
64 .def_property("frame_id",
65 &Image::FrameId,
66 &Image::SetFrameId,
67 "Unique identifier of the frame.")
68 .def_property_readonly(
69 "data_id", &Image::DataId, "Unique identifier of the data.")
70 .def_property(
71 "camera",
72 [](Image& self) -> py::typing::Optional<Camera> {
73 if (self.HasCameraPtr()) {
74 return py::cast(self.CameraPtr());
75 } else {
76 return py::none();
77 }
78 },
79 &Image::SetCameraPtr,
80 "The associated camera object.")
81 .def_property(
82 "frame",
83 [](Image& self) -> py::typing::Optional<Frame> {
84 if (self.HasFramePtr()) {
85 return py::cast(self.FramePtr());
86 } else {
87 return py::none();
88 }
89 },
90 &Image::SetFramePtr,
91 "The associated frame object.")
92 .def_property("name",
93 py::overload_cast<>(&Image::Name),
94 &Image::SetName,
95 "Name of the image.")
96 .def("cam_from_world",
97 &Image::CamFromWorld,
98 "The pose of the image, defined as the transformation from world to "
99 "camera space. This method is read-only and support non-trivial "
100 "frame (rig).")
101 .def_property_readonly(
102 "has_pose", &Image::HasPose, "Whether the image has a valid pose.")
103 .def_property("points2D",
104 py::overload_cast<>(&Image::Points2D),
105 py::overload_cast<Point2DVector>(&Image::SetPoints2D),
106 py::return_value_policy::reference_internal,
107 "Array of Points2D (=keypoints).")
108 .def("point2D",
109 py::overload_cast<camera_t>(&Image::Point2D),
110 py::return_value_policy::reference_internal,
111 "point2D_idx"_a,
112 "Direct accessor for a point2D.")
113 .def(
114 "set_point3D_for_point2D",
115 &Image::SetPoint3DForPoint2D,
116 "point2D_Idx"_a,
117 "point3D_id"_a,
118 "Set the point as triangulated, i.e. it is part of a 3D point track.")
119 .def("reset_point3D_for_point2D",
120 &Image::ResetPoint3DForPoint2D,
121 "point2D_idx"_a,
122 "Set the point as not triangulated, i.e. it is not part of a 3D "
123 "point track")
124 .def("has_point3D",
125 &Image::HasPoint3D,
126 "point3D_id"_a,
127 "Check whether one of the image points is part of a 3D point track.")
128 .def("projection_center",
129 &Image::ProjectionCenter,
130 "Extract the projection center in world space.")
131 .def("viewing_direction",
132 &Image::ViewingDirection,
133 "Extract the viewing direction of the image.")
134 .def("project_point",
135 &Image::ProjectPoint,
136 "Project 3D point onto the image",
137 "point3D"_a)
138 .def("has_camera_id",
139 &Image::HasCameraId,
140 "Check whether identifier of camera has been set.")
141 .def("has_camera_ptr",
142 &Image::HasCameraPtr,
143 "Check whether the camera pointer has been set.")
144 .def("reset_camera_ptr",
145 &Image::ResetCameraPtr,
146 "Make the camera pointer a nullptr.")
147 .def("has_frame_id",
148 &Image::HasFrameId,
149 "Check whether identifier of frame has been set.")
150 .def("has_frame_ptr",
151 &Image::HasFramePtr,
152 "Check whether the frame pointer has been set.")
153 .def("reset_frame_ptr",
154 &Image::ResetFramePtr,
155 "Make the frame pointer a nullptr.")
156 .def(
157 "is_ref_in_frame",
158 &Image::IsRefInFrame,
159 "Check if the image was captured by the reference sensor in the rig.")
160 .def("num_points2D",
161 &Image::NumPoints2D,
162 "Get the number of image points (keypoints).")
163 .def_property_readonly(
164 "num_points3D",
165 &Image::NumPoints3D,
166 "Get the number of triangulations, i.e. the number of points that\n"
167 "are part of a 3D point track.")
168 .def(
169 "get_observation_point2D_idxs",
170 [](const Image& self) {
171 std::vector<point2D_t> point2D_idxs;
172 for (point2D_t point2D_idx = 0; point2D_idx < self.NumPoints2D();
173 ++point2D_idx) {
174 if (self.Point2D(point2D_idx).HasPoint3D()) {
175 point2D_idxs.push_back(point2D_idx);
176 }
177 }
178 return point2D_idxs;
179 },
180 "Get the indices of 2D points that observe a 3D point.")
181 .def(
182 "get_observation_points2D",
183 [](const Image& self) {
184 Point2DVector points2D;
185 for (const auto& point2D : self.Points2D()) {
186 if (point2D.HasPoint3D()) {
187 points2D.push_back(point2D);
188 }
189 }
190 return points2D;
191 },
192 "Get the 2D points that observe a 3D point.");
193 MakeDataclass(PyImage);
194
195 py::bind_map<ImageMap>(m, "ImageMap");
196 }