Debian Codegraph @

src/pycolmap/scene/camera.cc

raw
1 #include "colmap/scene/camera.h"
2
3 #include "colmap/scene/point2d.h"
4 #include "colmap/sensor/models.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 <sstream>
15
16 #include <pybind11/eigen.h>
17 #include <pybind11/pybind11.h>
18 #include <pybind11/stl.h>
19 #include <pybind11/stl_bind.h>
20
21 using namespace colmap;
22 using namespace pybind11::literals;
23 namespace py = pybind11;
24
25 void BindCamera(py::module& m) {
26 py::enum_<CameraModelId> PyCameraModelId(m, "CameraModelId");
27 PyCameraModelId.value("INVALID", CameraModelId::kInvalid);
28 #define CAMERA_MODEL_CASE(CameraModel) \
29 PyCameraModelId.value(CameraModel::model_name.c_str(), CameraModel::model_id);
30
31 CAMERA_MODEL_CASES
32
33 #undef CAMERA_MODEL_CASE
34 AddStringToEnumConstructor(PyCameraModelId);
35 py::implicitly_convertible<int, CameraModelId>();
36
37 py::classh<Camera> PyCamera(m, "Camera");
38 PyCamera.def(py::init<>())
39 .def_static("create_from_model_id",
40 &Camera::CreateFromModelId,
41 "camera_id"_a,
42 "model"_a,
43 "focal_length"_a,
44 "width"_a,
45 "height"_a)
46 .def_static("create_from_model_name",
47 &Camera::CreateFromModelName,
48 "camera_id"_a,
49 "model_name"_a,
50 "focal_length"_a,
51 "width"_a,
52 "height"_a,
53 "Create camera from model name string.")
54 .def_readwrite(
55 "camera_id", &Camera::camera_id, "Unique identifier of the camera.")
56 .def_property_readonly(
57 "sensor_id", &Camera::SensorId, "Unique identifier of the sensor.")
58 .def_readwrite("model", &Camera::model_id, "Camera model.")
59 .def_property_readonly(
60 "model_name", &Camera::ModelName, "Camera model name as string.")
61 .def_readwrite("width", &Camera::width, "Width of camera sensor.")
62 .def_readwrite("height", &Camera::height, "Height of camera sensor.")
63 .def("mean_focal_length", &Camera::MeanFocalLength)
64 .def_property(
65 "focal_length", &Camera::FocalLength, &Camera::SetFocalLength)
66 .def_property(
67 "focal_length_x", &Camera::FocalLengthX, &Camera::SetFocalLengthX)
68 .def_property(
69 "focal_length_y", &Camera::FocalLengthY, &Camera::SetFocalLengthY)
70 .def_readwrite("has_prior_focal_length", &Camera::has_prior_focal_length)
71 .def_property("principal_point_x",
72 &Camera::PrincipalPointX,
73 &Camera::SetPrincipalPointX)
74 .def_property("principal_point_y",
75 &Camera::PrincipalPointY,
76 &Camera::SetPrincipalPointY)
77 .def("focal_length_idxs",
78 &Camera::FocalLengthIdxs,
79 "Indices of focal length parameters in params property.")
80 .def("principal_point_idxs",
81 &Camera::PrincipalPointIdxs,
82 "Indices of principal point parameters in params property.")
83 .def("extra_params_idxs",
84 &Camera::ExtraParamsIdxs,
85 "Indices of extra parameters in params property.")
86 .def("metadata_params_idxs",
87 &Camera::MetaDataParamsIdxs,
88 "Indices of metadata parameters in params property (only "
89 "spherical models have these; empty for perspective models).")
90 .def("calibration_matrix",
91 &Camera::CalibrationMatrix,
92 "Compute calibration matrix from params.")
93 .def_property_readonly(
94 "params_info",
95 &Camera::ParamsInfo,
96 "Get human-readable information about the parameter vector "
97 "ordering.")
98 .def_property(
99 "params",
100 [](Camera& self) {
101 // Return a view (via a numpy array) instead of a copy.
102 return Eigen::Map<Eigen::VectorXd>(self.params.data(),
103 self.params.size());
104 },
105 [](Camera& self, const std::vector<double>& params) {
106 self.params = params;
107 },
108 "Camera parameters.")
109 .def("params_to_string",
110 &Camera::ParamsToString,
111 "Concatenate parameters as comma-separated list.")
112 .def("set_params_from_string",
113 &Camera::SetParamsFromString,
114 "params"_a,
115 "Set camera parameters from comma-separated list.")
116 .def("verify_params",
117 &Camera::VerifyParams,
118 "Check whether parameters are valid, i.e. the parameter vector has"
119 " the correct dimensions that match the specified camera model.")
120 .def("has_bogus_params",
121 &Camera::HasBogusParams,
122 "min_focal_length_ratio"_a,
123 "max_focal_length_ratio"_a,
124 "max_extra_param"_a,
125 "Check whether camera has bogus parameters.")
126 .def("is_undistorted",
127 &Camera::IsUndistorted,
128 "Check whether camera is already undistorted.")
129 .def("is_perspective",
130 &Camera::IsPerspective,
131 "Whether the camera model is perspective, i.e. has a focal length "
132 "and a finite pinhole image plane (so positive-depth cheirality "
133 "applies). Omnidirectional models such as EQUIRECTANGULAR are not "
134 "perspective.")
135 .def("is_spherical",
136 &Camera::IsSpherical,
137 "Whether the camera model is spherical (equirectangular "
138 "omnidirectional panorama).")
139 .def("is_perspective_fisheye",
140 &Camera::IsPerspectiveFisheye,
141 "Whether the camera model is perspective and fisheye.")
142 .def("is_perspective_pinhole",
143 &Camera::IsPerspectivePinhole,
144 "Whether the camera model is perspective and not fisheye.")
145 .def("cam_from_img",
146 &Camera::CamFromImg,
147 "image_point"_a,
148 "Unproject point in image plane to camera frame.")
149 .def(
150 "cam_from_img",
151 [](const Camera& self,
152 const py::EigenDRef<const Eigen::MatrixX2d>& image_points) {
153 std::vector<Eigen::Vector2d> cam_points(image_points.rows());
154 for (Eigen::Index i = 0; i < image_points.rows(); ++i) {
155 const std::optional<Eigen::Vector2d> cam_point =
156 self.CamFromImg(image_points.row(i));
157 if (cam_point) {
158 cam_points[i] = *cam_point;
159 } else {
160 cam_points[i].setConstant(
161 std::numeric_limits<double>::quiet_NaN());
162 }
163 }
164 return cam_points;
165 },
166 "image_points"_a,
167 "Unproject list of points in image plane to camera frame.")
168 .def(
169 "cam_from_img",
170 [](const Camera& self, const Point2DVector& image_points) {
171 std::vector<Eigen::Vector2d> cam_points(image_points.size());
172 for (size_t i = 0; i < image_points.size(); ++i) {
173 const std::optional<Eigen::Vector2d> cam_point =
174 self.CamFromImg(image_points[i].xy);
175 if (cam_point) {
176 cam_points[i] = *cam_point;
177 } else {
178 cam_points[i].setConstant(
179 std::numeric_limits<double>::quiet_NaN());
180 }
181 }
182 return cam_points;
183 },
184 "image_points"_a,
185 "Unproject list of points in image plane to camera frame.")
186 .def("cam_ray_from_img",
187 &Camera::CamRayFromImg,
188 "image_point"_a,
189 "Unproject point in image plane to a unit bearing vector in the "
190 "camera frame. Unlike cam_from_img, this supports back-facing rays "
191 "of omnidirectional cameras.")
192 .def(
193 "cam_ray_from_img",
194 [](const Camera& self,
195 const py::EigenDRef<const Eigen::MatrixX2d>& image_points) {
196 std::vector<Eigen::Vector3d> cam_rays(image_points.rows());
197 for (Eigen::Index i = 0; i < image_points.rows(); ++i) {
198 const std::optional<Eigen::Vector3d> cam_ray =
199 self.CamRayFromImg(image_points.row(i));
200 if (cam_ray) {
201 cam_rays[i] = *cam_ray;
202 } else {
203 cam_rays[i].setConstant(
204 std::numeric_limits<double>::quiet_NaN());
205 }
206 }
207 return cam_rays;
208 },
209 "image_points"_a,
210 "Unproject list of points in image plane to unit bearing vectors in "
211 "the camera frame.")
212 .def(
213 "cam_ray_from_img",
214 [](const Camera& self, const Point2DVector& image_points) {
215 std::vector<Eigen::Vector3d> cam_rays(image_points.size());
216 for (size_t i = 0; i < image_points.size(); ++i) {
217 const std::optional<Eigen::Vector3d> cam_ray =
218 self.CamRayFromImg(image_points[i].xy);
219 if (cam_ray) {
220 cam_rays[i] = *cam_ray;
221 } else {
222 cam_rays[i].setConstant(
223 std::numeric_limits<double>::quiet_NaN());
224 }
225 }
226 return cam_rays;
227 },
228 "image_points"_a,
229 "Unproject list of points in image plane to unit bearing vectors in "
230 "the camera frame.")
231 .def("cam_from_img_threshold",
232 &Camera::CamFromImgThreshold,
233 "threshold"_a,
234 "Convert pixel threshold in image plane to world space.")
235 .def("img_from_cam",
236 &Camera::ImgFromCam,
237 "cam_point"_a,
238 "check_cheirality"_a = true,
239 "Project point from camera frame to image plane. Without cheirality "
240 "check, points behind the camera are projected as well.")
241 .def(
242 "img_from_cam",
243 [](const Camera& self,
244 const py::EigenDRef<const Eigen::MatrixX3d>& cam_points,
245 const bool check_cheirality) {
246 const size_t num_points = cam_points.rows();
247 std::vector<Eigen::Vector2d> image_points(num_points);
248 for (size_t i = 0; i < num_points; ++i) {
249 const std::optional<Eigen::Vector2d> image_point =
250 self.ImgFromCam(cam_points.row(i), check_cheirality);
251 if (image_point) {
252 image_points[i] = *image_point;
253 } else {
254 image_points[i].setConstant(
255 std::numeric_limits<double>::quiet_NaN());
256 }
257 }
258 return image_points;
259 },
260 "cam_points"_a,
261 "check_cheirality"_a = true,
262 "Project list of points from camera frame to image plane. Without "
263 "cheirality check, points behind the camera are projected as well.")
264 .def("rescale",
265 py::overload_cast<size_t, size_t>(&Camera::Rescale),
266 "new_width"_a,
267 "new_height"_a,
268 "Rescale the camera dimensions and accordingly the "
269 "focal length and the principal point.")
270 .def("rescale",
271 py::overload_cast<double>(&Camera::Rescale),
272 "scale"_a,
273 "Rescale the camera dimensions and accordingly the "
274 "focal length and the principal point.");
275 DefDeprecation(PyCamera, "create", "create_from_model_id");
276 MakeDataclass(PyCamera,
277 {"camera_id",
278 "model",
279 "width",
280 "height",
281 "params",
282 "has_prior_focal_length"});
283
284 py::bind_map<CameraMap>(m, "CameraMap");
285 }