Debian Codegraph @

src/pycolmap/scene/reconstruction.cc

raw
1 #include "colmap/scene/reconstruction.h"
2
3 #include "colmap/scene/correspondence_graph.h"
4 #include "colmap/scene/database_cache.h"
5 #include "colmap/scene/reconstruction_io.h"
6 #include "colmap/sensor/models.h"
7 #include "colmap/util/file.h"
8 #include "colmap/util/logging.h"
9 #include "colmap/util/misc.h"
10 #include "colmap/util/ply.h"
11 #include "colmap/util/types.h"
12
13 #include "pycolmap/helpers.h"
14 #include "pycolmap/pybind11_extension.h"
15 #include "pycolmap/scene/types.h"
16
17 #include <filesystem>
18 #include <memory>
19 #include <sstream>
20
21 #include <pybind11/eigen.h>
22 #include <pybind11/pybind11.h>
23 #include <pybind11/stl.h>
24 #include <pybind11/stl_bind.h>
25
26 using namespace colmap;
27 using namespace pybind11::literals;
28 namespace py = pybind11;
29
30 void BindReconstruction(py::module& m) {
31 py::classh<Reconstruction>(m, "Reconstruction")
32 .def(py::init<>())
33 .def(py::init<const Reconstruction&>(), "reconstruction"_a)
34 .def(py::init([](const std::filesystem::path& path) {
35 auto reconstruction = std::make_shared<Reconstruction>();
36 reconstruction->Read(path);
37 return reconstruction;
38 }),
39 "path"_a)
40 .def("read",
41 &Reconstruction::Read,
42 "path"_a,
43 "Read reconstruction in COLMAP format. Prefer binary.")
44 .def("write",
45 &Reconstruction::Write,
46 "output_dir"_a,
47 "Write reconstruction in COLMAP binary format.")
48 .def("read_text", &Reconstruction::ReadText, "path"_a)
49 .def("read_binary", &Reconstruction::ReadBinary, "path"_a)
50 .def("write_text", &Reconstruction::WriteText, "path"_a)
51 .def("write_binary", &Reconstruction::WriteBinary, "path"_a)
52 .def("num_rigs", &Reconstruction::NumRigs)
53 .def("num_cameras", &Reconstruction::NumCameras)
54 .def("num_frames", &Reconstruction::NumFrames)
55 .def("num_reg_frames", &Reconstruction::NumRegFrames)
56 .def("num_images", &Reconstruction::NumImages)
57 .def("num_reg_images", &Reconstruction::NumRegImages)
58 .def("num_points3D", &Reconstruction::NumPoints3D)
59 .def_property_readonly("rigs",
60 &Reconstruction::Rigs,
61 py::return_value_policy::reference_internal)
62 .def("rig",
63 py::overload_cast<rig_t>(&Reconstruction::Rig),
64 "rig_id"_a,
65 "Direct accessor for a rig.",
66 py::return_value_policy::reference_internal)
67 .def_property_readonly("cameras",
68 &Reconstruction::Cameras,
69 py::return_value_policy::reference_internal)
70 .def("camera",
71 py::overload_cast<camera_t>(&Reconstruction::Camera),
72 "camera_id"_a,
73 "Direct accessor for a camera.",
74 py::return_value_policy::reference_internal)
75 .def_property_readonly("frames",
76 &Reconstruction::Frames,
77 py::return_value_policy::reference_internal)
78 .def("frame",
79 py::overload_cast<frame_t>(&Reconstruction::Frame),
80 "frame_id"_a,
81 "Direct accessor for a frame.",
82 py::return_value_policy::reference_internal)
83 .def_property_readonly("images",
84 &Reconstruction::Images,
85 py::return_value_policy::reference_internal)
86 .def("image",
87 py::overload_cast<image_t>(&Reconstruction::Image),
88 "image_id"_a,
89 "Direct accessor for an image.",
90 py::return_value_policy::reference_internal)
91 .def_property_readonly("points3D",
92 &Reconstruction::Points3D,
93 py::return_value_policy::reference_internal)
94 .def("point3D",
95 py::overload_cast<point3D_t>(&Reconstruction::Point3D),
96 "point3D_id"_a,
97 "Direct accessor for a Point3D.",
98 py::return_value_policy::reference_internal)
99 .def("reg_image_ids", &Reconstruction::RegImageIds)
100 .def("reg_frame_ids", &Reconstruction::RegFrameIds)
101 .def("point3D_ids", &Reconstruction::Point3DIds)
102 .def("exists_rig", &Reconstruction::ExistsRig, "rig_id"_a)
103 .def("exists_camera", &Reconstruction::ExistsCamera, "camera_id"_a)
104 .def("exists_frame", &Reconstruction::ExistsFrame, "frame_id"_a)
105 .def("exists_image", &Reconstruction::ExistsImage, "image_id"_a)
106 .def("exists_point3D", &Reconstruction::ExistsPoint3D, "point3D_id"_a)
107 .def("is_valid",
108 &Reconstruction::IsValid,
109 "Check whether the reconstruction object is internally consistent.")
110 .def("load", &Reconstruction::Load, "database_cache"_a)
111 .def("tear_down", &Reconstruction::TearDown)
112 .def("add_rig", &Reconstruction::AddRig, "rig"_a, "Add new rig.")
113 .def("add_camera",
114 &Reconstruction::AddCamera,
115 "camera"_a,
116 "Add new camera. There is only one camera per image, while multiple "
117 "images might be taken by the same camera.")
118 .def("add_camera_with_trivial_rig",
119 &Reconstruction::AddCameraWithTrivialRig,
120 "camera"_a,
121 "Add a new camera and also create a trivial rig whose rig_id "
122 "matches the "
123 "camera_id. The camera becomes the rig's only sensor.")
124 .def("add_frame", &Reconstruction::AddFrame, "frame"_a, "Add new frame.")
125 .def(
126 "add_image",
127 &Reconstruction::AddImage,
128 "image"_a,
129 "Add new image. Its camera must have been added before. If its "
130 "camera object is unset, it will be automatically populated from the "
131 "added cameras.")
132 .def("add_image_with_trivial_frame",
133 py::overload_cast<Image>(&Reconstruction::AddImageWithTrivialFrame),
134 "image"_a,
135 "Add a new image and create a frame with the same ID (frame_id = "
136 "image_id). "
137 "Assumes a rig exists whose rig_id equals the camera_id of the "
138 "image.")
139 .def("add_image_with_trivial_frame",
140 py::overload_cast<Image, const Rigid3d&>(
141 &Reconstruction::AddImageWithTrivialFrame),
142 "image"_a,
143 "cam_from_world"_a,
144 "Add a new image, create a trivial frame (frame_id = image_id), and "
145 "also "
146 "register the frame with an input pose.")
147
148 .def("add_point3D",
149 py::overload_cast<const Eigen::Vector3d&,
150 Track,
151 const Eigen::Vector3ub&>(
152 &Reconstruction::AddPoint3D),
153 "Add new 3D object, and return its unique ID.",
154 "xyz"_a,
155 "track"_a,
156 "color"_a = Eigen::Vector3ub::Zero())
157 .def("add_point3D_with_id",
158 py::overload_cast<point3D_t, Point3D>(&Reconstruction::AddPoint3D),
159 "point3D_id"_a,
160 "point3D"_a,
161 "Add new 3D point with known ID.")
162 .def("add_observation",
163 &Reconstruction::AddObservation,
164 "point3D_id"_a,
165 "track_element"_a,
166 "Add observation to existing 3D point.")
167 .def(
168 "merge_points3D",
169 &Reconstruction::MergePoints3D,
170 "point3D_id1"_a,
171 "point3D_id2"_a,
172 "Merge two 3D points and return new identifier of new 3D point."
173 "The location of the merged 3D point is a weighted average of the "
174 "two original 3D point's locations according to their track lengths.")
175 .def("delete_point3D",
176 &Reconstruction::DeletePoint3D,
177 "point3D_id"_a,
178 "Delete a 3D point, and all its references in the observed images.")
179 .def("delete_observation",
180 &Reconstruction::DeleteObservation,
181 "image_id"_a,
182 "point2D_idx"_a,
183 "Delete one observation from an image and the corresponding 3D "
184 "point. Note that this deletes the entire 3D point, if the track "
185 "has two elements prior to calling this method.")
186 .def("delete_all_points2D_and_points3D",
187 &Reconstruction::DeleteAllPoints2DAndPoints3D,
188 "Delete all 2D points of all images and all 3D points.")
189 .def("set_rigs_and_frames",
190 &Reconstruction::SetRigsAndFrames,
191 "rigs"_a,
192 "frames"_a,
193 "Set rigs and frames together.")
194 .def("register_frame",
195 &Reconstruction::RegisterFrame,
196 "frame_id"_a,
197 "Register an existing frame, and all its references.")
198 .def("deregister_frame",
199 &Reconstruction::DeRegisterFrame,
200 "frame_id"_a,
201 "De-register an existing frame, and all its references.")
202 .def("normalize",
203 &Reconstruction::Normalize,
204 "fixed_scale"_a = false,
205 "extent"_a = 10.0,
206 "min_percentile"_a = 0.1,
207 "max_percentile"_a = 0.9,
208 "use_images"_a = true,
209 "Normalize scene by scaling and translation to avoid degenerate "
210 "visualization after bundle adjustment and to improve numerical "
211 "stability of algorithms.\n\n"
212 "Translates scene such that the mean of the camera centers or point "
213 "locations are at the origin of the coordinate system.\n\n"
214 "Scales scene such that the minimum and maximum camera centers "
215 "(or points) are at the given `extent`, whereas `min_percentile` "
216 "and `max_percentile` determine the minimum and maximum percentiles "
217 "of the camera centers (or points) considered.")
218 .def("transform",
219 &Reconstruction::Transform,
220 "new_from_old_world"_a,
221 "Apply the 3D similarity transformation to all images and points.")
222 .def("compute_centroid",
223 &Reconstruction::ComputeCentroid,
224 "min_percentile"_a = 0.0,
225 "max_percentile"_a = 1.0,
226 "use_images"_a = false)
227 .def("compute_bounding_box",
228 &Reconstruction::ComputeBoundingBox,
229 "min_percentile"_a = 0.0,
230 "max_percentile"_a = 1.0,
231 "use_images"_a = false)
232 .def("crop", &Reconstruction::Crop, "bbox"_a)
233 .def("find_image_with_name",
234 &Reconstruction::FindImageWithName,
235 py::return_value_policy::reference_internal,
236 "name"_a,
237 "Find image with matching name. Returns None if no match is found.")
238 .def("find_common_reg_image_ids",
239 &Reconstruction::FindCommonRegImageIds,
240 "other"_a,
241 "Find images that are both present in this and the given "
242 "reconstruction.")
243 .def("transcribe_image_ids_to_database",
244 &Reconstruction::TranscribeImageIdsToDatabase,
245 "database"_a,
246 "Update image identifiers to match the database by name.")
247 .def("update_point_3d_errors", &Reconstruction::UpdatePoint3DErrors)
248 .def("compute_num_observations", &Reconstruction::ComputeNumObservations)
249 .def("compute_mean_track_length", &Reconstruction::ComputeMeanTrackLength)
250 .def("compute_mean_observations_per_reg_image",
251 &Reconstruction::ComputeMeanObservationsPerRegImage)
252 .def("compute_mean_reprojection_error",
253 &Reconstruction::ComputeMeanReprojectionError)
254 .def("import_PLY",
255 py::overload_cast<const std::filesystem::path&>(
256 &Reconstruction::ImportPLY),
257 "path"_a,
258 "Import from PLY format. Note that these import functions are "
259 "only intended for visualization of data and not usable for "
260 "reconstruction.")
261 .def("export_PLY",
262 &ExportPLY,
263 "output_path"_a,
264 "Export 3D points to PLY format (.ply).")
265 .def("extract_colors_for_image",
266 &Reconstruction::ExtractColorsForImage,
267 "image_id"_a,
268 "path"_a,
269 "Extract colors for 3D points of given image. Colors will be "
270 "extracted only for 3D points which are completely black. "
271 "Return True if the image could be read at the given path.")
272 .def("extract_colors_for_all_images",
273 &Reconstruction::ExtractColorsForAllImages,
274 "Extract colors for all 3D points by computing the mean color of "
275 "all images.",
276 "path"_a,
277 "num_threads"_a = -1)
278 .def("create_image_dirs",
279 &Reconstruction::CreateImageDirs,
280 "path"_a,
281 "Create all image sub-directories in the given path.")
282 .def("__copy__",
283 [](const Reconstruction& self) { return Reconstruction(self); })
284 .def("__deepcopy__",
285 [](const Reconstruction& self, const py::dict&) {
286 return Reconstruction(self);
287 })
288 .def("__repr__", &CreateRepresentation<Reconstruction>)
289 .def("summary", [](const Reconstruction& self) {
290 std::ostringstream ss;
291 ss << "Reconstruction:"
292 << "\n\tnum_rigs = " << self.NumRigs()
293 << "\n\tnum_cameras = " << self.NumCameras()
294 << "\n\tnum_frames = " << self.NumFrames()
295 << "\n\tnum_reg_frames = " << self.NumRegFrames()
296 << "\n\tnum_images = " << self.NumImages()
297 << "\n\tnum_points3D = " << self.NumPoints3D()
298 << "\n\tnum_observations = " << self.ComputeNumObservations()
299 << "\n\tmean_track_length = " << self.ComputeMeanTrackLength()
300 << "\n\tmean_observations_per_image = "
301 << self.ComputeMeanObservationsPerRegImage()
302 << "\n\tmean_reprojection_error = "
303 << self.ComputeMeanReprojectionError();
304 return ss.str();
305 });
306 }