Debian Codegraph @

src/pycolmap/pipeline/mvs.cc

raw
1 #include "colmap/exe/mvs.h"
2
3 #include "colmap/mvs/fusion.h"
4 #include "colmap/mvs/patch_match_options.h"
5 #include "colmap/scene/reconstruction.h"
6 #include "colmap/util/controller_thread.h"
7 #include "colmap/util/file.h"
8 #include "colmap/util/misc.h"
9
10 #if defined(COLMAP_CUDA_ENABLED) || defined(COLMAP_HIP_ENABLED)
11 #include "colmap/mvs/patch_match.h"
12 #endif
13
14 #include "colmap/util/logging.h"
15
16 #include "pycolmap/helpers.h"
17 #include "pycolmap/pybind11_extension.h"
18
19 #include <pybind11/pybind11.h>
20 #include <pybind11/stl.h>
21
22 using namespace colmap;
23 using namespace pybind11::literals;
24 namespace py = pybind11;
25
26 void PatchMatchStereo(
27 const std::filesystem::path& workspace_path,
28 std::string workspace_format,
29 const std::string& pmvs_option_name,
30 const mvs::PatchMatchOptions& options,
31 const std::filesystem::path& config_path,
32 const std::shared_ptr<CancellationToken>& cancellation_token) {
33 #if defined(COLMAP_CUDA_ENABLED) || defined(COLMAP_HIP_ENABLED)
34 StringToLower(&workspace_format);
35 THROW_CHECK(workspace_format == "colmap" || workspace_format == "pmvs")
36 << "Invalid `workspace_format` " << workspace_format
37 << " - supported values are 'COLMAP' or 'PMVS'.";
38
39 auto controller = std::make_shared<mvs::PatchMatchController>(
40 options, workspace_path, workspace_format, pmvs_option_name, config_path);
41 ControllerThread<mvs::PatchMatchController> thread(std::move(controller));
42 thread.Start();
43 PyWait(&thread, cancellation_token);
44 #else
45 static_cast<void>(workspace_path);
46 static_cast<void>(workspace_format);
47 static_cast<void>(pmvs_option_name);
48 static_cast<void>(options);
49 static_cast<void>(config_path);
50 static_cast<void>(cancellation_token);
51 LOG(FATAL_THROW) << "Dense stereo reconstruction requires CUDA or HIP, "
52 "neither of which is available on your system.";
53 #endif
54 }
55
56 Reconstruction StereoFusion(
57 const std::filesystem::path& output_path,
58 const std::filesystem::path& workspace_path,
59 std::string workspace_format,
60 const std::string& pmvs_option_name,
61 std::string input_type,
62 const mvs::StereoFusionOptions& options,
63 std::string output_type,
64 const std::shared_ptr<CancellationToken>& cancellation_token) {
65 py::gil_scoped_release release;
66 PyInterruptChecker interrupt_checker(cancellation_token);
67 Reconstruction reconstruction =
68 RunStereoFuserImpl(output_path,
69 workspace_path,
70 std::move(workspace_format),
71 pmvs_option_name,
72 std::move(input_type),
73 options,
74 std::move(output_type),
75 interrupt_checker.Callback());
76 interrupt_checker.CheckAndThrow();
77 return reconstruction;
78 }
79
80 void BindMVS(py::module& m) {
81 using PMOpts = mvs::PatchMatchOptions;
82 auto PyPatchMatchOptions =
83 py::classh<PMOpts>(m, "PatchMatchOptions")
84 .def(py::init<>())
85 .def_readwrite("max_image_size",
86 &PMOpts::max_image_size,
87 "Maximum image size in either dimension.")
88 .def_readwrite(
89 "gpu_index",
90 &PMOpts::gpu_index,
91 "Index of the GPU used for patch match. For multi-GPU usage, "
92 "you should separate multiple GPU indices by comma, e.g., "
93 "\"0,1,2,3\".")
94 .def_readwrite("depth_min", &PMOpts::depth_min)
95 .def_readwrite("depth_max", &PMOpts::depth_max)
96 .def_readwrite(
97 "window_radius",
98 &PMOpts::window_radius,
99 "Half window size to compute NCC photo-consistency cost.")
100 .def_readwrite("window_step",
101 &PMOpts::window_step,
102 "Number of pixels to skip when computing NCC.")
103 .def_readwrite("sigma_spatial",
104 &PMOpts::sigma_spatial,
105 "Spatial sigma for bilaterally weighted NCC.")
106 .def_readwrite("sigma_color",
107 &PMOpts::sigma_color,
108 "Color sigma for bilaterally weighted NCC.")
109 .def_readwrite(
110 "num_samples",
111 &PMOpts::num_samples,
112 "Number of random samples to draw in Monte Carlo sampling.")
113 .def_readwrite("ncc_sigma",
114 &PMOpts::ncc_sigma,
115 "Spread of the NCC likelihood function.")
116 .def_readwrite("min_triangulation_angle",
117 &PMOpts::min_triangulation_angle,
118 "Minimum triangulation angle in degrees.")
119 .def_readwrite("incident_angle_sigma",
120 &PMOpts::incident_angle_sigma,
121 "Spread of the incident angle likelihood function.")
122 .def_readwrite("num_iterations",
123 &PMOpts::num_iterations,
124 "Number of coordinate descent iterations.")
125 .def_readwrite("geom_consistency",
126 &PMOpts::geom_consistency,
127 "Whether to add a regularized geometric consistency "
128 "term to the cost function. If true, the "
129 "`depth_maps` and `normal_maps` must not be null.")
130 .def_readwrite("geom_consistency_regularizer",
131 &PMOpts::geom_consistency_regularizer,
132 "The relative weight of the geometric consistency "
133 "term w.r.t. to the photo-consistency term.")
134 .def_readwrite("geom_consistency_max_cost",
135 &PMOpts::geom_consistency_max_cost,
136 "Maximum geometric consistency cost in terms of the "
137 "forward-backward reprojection error in pixels.")
138 .def_readwrite(
139 "filter", &PMOpts::filter, "Whether to enable filtering.")
140 .def_readwrite(
141 "filter_min_ncc",
142 &PMOpts::filter_min_ncc,
143 "Minimum NCC coefficient for pixel to be photo-consistent.")
144 .def_readwrite("filter_min_triangulation_angle",
145 &PMOpts::filter_min_triangulation_angle,
146 "Minimum triangulation angle to be stable.")
147 .def_readwrite(
148 "filter_min_num_consistent",
149 &PMOpts::filter_min_num_consistent,
150 "Minimum number of source images have to be consistent "
151 "for pixel not to be filtered.")
152 .def_readwrite(
153 "filter_geom_consistency_max_cost",
154 &PMOpts::filter_geom_consistency_max_cost,
155 "Maximum forward-backward reprojection error for pixel "
156 "to be geometrically consistent.")
157 .def_readwrite("cache_size",
158 &PMOpts::cache_size,
159 "Cache size in gigabytes for patch match.")
160 .def_readwrite(
161 "allow_missing_files",
162 &PMOpts::allow_missing_files,
163 "Whether to tolerate missing images/maps in the problem setup")
164 .def_readwrite("write_consistency_graph",
165 &PMOpts::write_consistency_graph,
166 "Whether to write the consistency graph.")
167 .def_readwrite("num_threads",
168 &PMOpts::num_threads,
169 "Number of threads for processing. "
170 "-1 uses all available threads.")
171 .def("check", &PMOpts::Check);
172 MakeDataclass(PyPatchMatchOptions);
173
174 m.def("patch_match_stereo",
175 &PatchMatchStereo,
176 "workspace_path"_a,
177 "workspace_format"_a = "COLMAP",
178 "pmvs_option_name"_a = "option-all",
179 py::arg_v("options", mvs::PatchMatchOptions(), "PatchMatchOptions()"),
180 "config_path"_a = "",
181 "cancellation_token"_a = py::none(),
182 "Runs Patch-Match-Stereo (requires CUDA)",
183 py::call_guard<py::gil_scoped_release>());
184
185 using SFOpts = mvs::StereoFusionOptions;
186 auto PyStereoFusionOptions =
187 py::classh<SFOpts>(m, "StereoFusionOptions")
188 .def(py::init<>())
189 .def_readwrite("mask_path",
190 &SFOpts::mask_path,
191 "Path for PNG masks. Same format expected as "
192 "ImageReaderOptions.")
193 .def_readwrite("num_threads",
194 &SFOpts::num_threads,
195 "The number of threads to use during fusion.")
196 .def_readwrite("max_image_size",
197 &SFOpts::max_image_size,
198 "Maximum image size in either dimension.")
199 .def_readwrite("min_num_pixels",
200 &SFOpts::min_num_pixels,
201 "Minimum number of fused pixels to produce a point.")
202 .def_readwrite(
203 "max_num_pixels",
204 &SFOpts::max_num_pixels,
205 "Maximum number of pixels to fuse into a single point.")
206 .def_readwrite("max_traversal_depth",
207 &SFOpts::max_traversal_depth,
208 "Maximum depth in consistency graph traversal.")
209 .def_readwrite("max_reproj_error",
210 &SFOpts::max_reproj_error,
211 "Maximum relative difference between measured and "
212 "projected pixel.")
213 .def_readwrite("max_depth_error",
214 &SFOpts::max_depth_error,
215 "Maximum relative difference between measured and "
216 "projected depth.")
217 .def_readwrite("max_normal_error",
218 &SFOpts::max_normal_error,
219 "Maximum angular difference in degrees of normals "
220 "of pixels to be fused.")
221 .def_readwrite("check_num_images",
222 &SFOpts::check_num_images,
223 "Number of overlapping images to transitively check "
224 "for fusing points.")
225 .def_readwrite(
226 "use_cache",
227 &SFOpts::use_cache,
228 "Flag indicating whether to use LRU cache or pre-load all data")
229 .def_readwrite("cache_size",
230 &SFOpts::cache_size,
231 "Cache size in gigabytes for fusion.")
232 .def_readwrite("bounding_box",
233 &SFOpts::bounding_box,
234 "Bounding box Tuple[min, max]")
235 .def("check", &SFOpts::Check);
236 MakeDataclass(PyStereoFusionOptions);
237
238 m.def(
239 "stereo_fusion",
240 &StereoFusion,
241 "output_path"_a,
242 "workspace_path"_a,
243 "workspace_format"_a = "COLMAP",
244 "pmvs_option_name"_a = "option-all",
245 "input_type"_a = "geometric",
246 py::arg_v("options", mvs::StereoFusionOptions(), "StereoFusionOptions()"),
247 "output_type"_a = "bin",
248 "cancellation_token"_a = py::none(),
249 "Stereo Fusion");
250 }