Debian Codegraph @

src/pycolmap/pipeline/match_features.cc

raw
1 #include "colmap/controllers/feature_matching.h"
2 #include "colmap/controllers/pairing.h"
3 #include "colmap/estimators/two_view_geometry.h"
4 #include "colmap/exe/feature.h"
5 #include "colmap/exe/sfm.h"
6 #include "colmap/feature/sift.h"
7 #include "colmap/util/file.h"
8 #include "colmap/util/logging.h"
9 #include "colmap/util/misc.h"
10
11 #include "pycolmap/helpers.h"
12 #include "pycolmap/pybind11_extension.h"
13 #include "pycolmap/utils.h"
14
15 #include <filesystem>
16 #include <memory>
17
18 #include <pybind11/pybind11.h>
19 #include <pybind11/stl.h>
20
21 using namespace colmap;
22 using namespace pybind11::literals;
23 namespace py = pybind11;
24
25 template <typename PairingOptions,
26 std::unique_ptr<Thread> MatcherFactory(const PairingOptions&,
27 const FeatureMatchingOptions&,
28 const TwoViewGeometryOptions&,
29 const std::filesystem::path&)>
30 void MatchFeatures(
31 const std::filesystem::path& database_path,
32 FeatureMatchingOptions matching_options,
33 const PairingOptions& pairing_options,
34 const TwoViewGeometryOptions& verification_options,
35 const Device device,
36 const std::shared_ptr<CancellationToken>& cancellation_token) {
37 THROW_CHECK_FILE_EXISTS(database_path);
38 try {
39 py::cast(pairing_options).attr("check").attr("__call__")();
40 } catch (py::error_already_set& ex) {
41 // Allow pass if no check function defined.
42 if (!ex.matches(PyExc_AttributeError)) {
43 throw ex;
44 }
45 }
46 matching_options.use_gpu = IsGPU(device);
47 THROW_CHECK(matching_options.Check());
48
49 py::gil_scoped_release release;
50 std::unique_ptr<Thread> matcher = MatcherFactory(
51 pairing_options, matching_options, verification_options, database_path);
52 matcher->Start();
53 PyWait(matcher.get(), cancellation_token);
54 }
55
56 void VerifyMatches(
57 const std::filesystem::path& database_path,
58 const std::filesystem::path& pairs_path,
59 const TwoViewGeometryOptions& verification_options,
60 const std::shared_ptr<CancellationToken>& cancellation_token) {
61 THROW_CHECK_FILE_EXISTS(database_path);
62 THROW_CHECK_FILE_EXISTS(pairs_path);
63 py::gil_scoped_release release; // verification is multi-threaded
64
65 FeatureMatchingOptions matching_options;
66 matching_options.use_gpu = false;
67
68 ImportedPairingOptions pairing_options;
69 pairing_options.match_list_path = pairs_path;
70
71 std::unique_ptr<Thread> matcher = CreateImagePairsFeatureMatcher(
72 pairing_options, matching_options, verification_options, database_path);
73 matcher->Start();
74 PyWait(matcher.get(), cancellation_token);
75 }
76
77 void GeometricVerification(
78 const std::filesystem::path& database_path,
79 const GeometricVerifierOptions& verifier_options,
80 const ExistingMatchedPairingOptions& pairing_options,
81 const TwoViewGeometryOptions& geometry_options,
82 const std::shared_ptr<CancellationToken>& cancellation_token) {
83 THROW_CHECK_FILE_EXISTS(database_path);
84
85 py::gil_scoped_release release; // verification is multi-threaded
86 std::unique_ptr<Thread> verifier = CreateGeometricVerifier(
87 verifier_options, pairing_options, geometry_options, database_path);
88 verifier->Start();
89 PyWait(verifier.get(), cancellation_token);
90 }
91
92 void GuidedGeometricVerification(
93 const Reconstruction& reconstruction,
94 const std::filesystem::path& database_path,
95 const ExistingMatchedPairingOptions& pairing_options,
96 const TwoViewGeometryOptions& geometry_options,
97 int num_threads,
98 const std::shared_ptr<CancellationToken>& cancellation_token) {
99 THROW_CHECK_FILE_EXISTS(database_path);
100
101 py::gil_scoped_release release; // verification is multi-threaded
102 PyInterruptChecker interrupt_checker(cancellation_token);
103 RunGuidedGeometricVerifierImpl(reconstruction,
104 database_path,
105 pairing_options,
106 geometry_options,
107 num_threads,
108 interrupt_checker.Callback());
109 interrupt_checker.CheckAndThrow();
110 }
111
112 void BindMatchFeatures(py::module& m) {
113 auto PyExhaustivePairingOptions =
114 py::classh<ExhaustivePairingOptions>(m, "ExhaustivePairingOptions")
115 .def(py::init<>())
116 .def_readwrite("block_size", &ExhaustivePairingOptions::block_size)
117 .def("check", &ExhaustivePairingOptions::Check);
118 MakeDataclass(PyExhaustivePairingOptions);
119
120 auto PySpatialPairingOptions =
121 py::classh<SpatialPairingOptions>(m, "SpatialPairingOptions")
122 .def(py::init<>())
123 .def_readwrite(
124 "ignore_z",
125 &SpatialPairingOptions::ignore_z,
126 "Whether to ignore the Z-component of the location prior.")
127 .def_readwrite("max_num_neighbors",
128 &SpatialPairingOptions::max_num_neighbors,
129 "The maximum number of nearest neighbors to match.")
130 .def_readwrite(
131 "min_num_neighbors",
132 &SpatialPairingOptions::min_num_neighbors,
133 "The minimum number of nearest neighbors to match. Neighbors "
134 "include those within max_distance or to satisfy "
135 "min_num_neighbors.")
136 .def_readwrite("max_distance",
137 &SpatialPairingOptions::max_distance,
138 "The maximum distance between the query and nearest "
139 "neighbor [meters].")
140 .def_readwrite("num_threads", &SpatialPairingOptions::num_threads)
141 .def("check", &SpatialPairingOptions::Check);
142 MakeDataclass(PySpatialPairingOptions);
143
144 auto PyVocabTreePairingOptions =
145 py::classh<VocabTreePairingOptions>(m, "VocabTreePairingOptions")
146 .def(py::init<>())
147 .def_readwrite("num_images",
148 &VocabTreePairingOptions::num_images,
149 "Number of images to retrieve for each query image.")
150 .def_readwrite(
151 "num_nearest_neighbors",
152 &VocabTreePairingOptions::num_nearest_neighbors,
153 "Number of nearest neighbors to retrieve per query feature.")
154 .def_readwrite(
155 "num_checks",
156 &VocabTreePairingOptions::num_checks,
157 "Number of nearest-neighbor checks to use in retrieval.")
158 .def_readwrite(
159 "num_images_after_verification",
160 &VocabTreePairingOptions::num_images_after_verification,
161 "How many images to return after spatial verification. Set to "
162 "0 to turn off spatial verification.")
163 .def_readwrite(
164 "max_num_features",
165 &VocabTreePairingOptions::max_num_features,
166 "The maximum number of features to use for indexing an image.")
167 .def_readwrite("vocab_tree_path",
168 &VocabTreePairingOptions::vocab_tree_path,
169 "Path to the vocabulary tree.")
170 .def_readwrite(
171 "match_list_path",
172 &VocabTreePairingOptions::match_list_path,
173 "Optional path to file with specific image names to match.")
174 .def_readwrite("num_threads", &VocabTreePairingOptions::num_threads)
175 .def("check", &VocabTreePairingOptions::Check);
176 MakeDataclass(PyVocabTreePairingOptions);
177
178 auto PySequentialPairingOptions =
179 py::classh<SequentialPairingOptions>(m, "SequentialPairingOptions")
180 .def(py::init<>())
181 .def_readwrite("overlap",
182 &SequentialPairingOptions::overlap,
183 "Number of overlapping image pairs.")
184 .def_readwrite(
185 "quadratic_overlap",
186 &SequentialPairingOptions::quadratic_overlap,
187 "Whether to match images against their quadratic neighbors.")
188 .def_readwrite("expand_rig_images",
189 &SequentialPairingOptions::expand_rig_images,
190 "Whether to match an image against all images in "
191 "neighboring rig frames. If no rigs/frames are "
192 "configured in the database, this option is ignored.")
193 .def_readwrite("loop_detection",
194 &SequentialPairingOptions::loop_detection,
195 "Loop detection is invoked every "
196 "`loop_detection_period` images.")
197 .def_readwrite("loop_detection_period",
198 &SequentialPairingOptions::loop_detection_period,
199 "The frequency at which loop detection is triggered, "
200 "in number of images.")
201 .def_readwrite("loop_detection_num_images",
202 &SequentialPairingOptions::loop_detection_num_images,
203 "The number of images to retrieve in loop "
204 "detection. This number should be significantly "
205 "larger than the sequential matching overlap.")
206 .def_readwrite(
207 "loop_detection_min_index_distance",
208 &SequentialPairingOptions::loop_detection_min_index_distance,
209 "The minimum image index distance between a loop detection "
210 "query and a retrieved image. The index is determined by the "
211 "sequential image order. Set to 0 to disable this restriction.")
212 .def_readwrite(
213 "loop_detection_num_nearest_neighbors",
214 &SequentialPairingOptions::loop_detection_num_nearest_neighbors,
215 "Number of nearest neighbors to retrieve per query feature.")
216 .def_readwrite(
217 "loop_detection_num_checks",
218 &SequentialPairingOptions::loop_detection_num_checks,
219 "Number of nearest-neighbor checks to use in retrieval.")
220 .def_readwrite(
221 "loop_detection_num_images_after_verification",
222 &SequentialPairingOptions::
223 loop_detection_num_images_after_verification,
224 "How many images to return after spatial verification. Set to "
225 "0 to turn off spatial verification.")
226 .def_readwrite(
227 "loop_detection_max_num_features",
228 &SequentialPairingOptions::loop_detection_max_num_features,
229 "The maximum number of features to use for indexing "
230 "an image. If an image has more features, only the "
231 "largest-scale features will be indexed.")
232 .def_readwrite("vocab_tree_path",
233 &SequentialPairingOptions::vocab_tree_path,
234 "Path to the vocabulary tree.")
235 .def_readwrite(
236 "num_threads",
237 &SequentialPairingOptions::num_threads,
238 "Number of threads for loop detection indexing and retrieval.")
239 .def("vocab_tree_options",
240 &SequentialPairingOptions::VocabTreeOptions)
241 .def("check", &SequentialPairingOptions::Check);
242 MakeDataclass(PySequentialPairingOptions);
243
244 auto PyImportedPairingOptions =
245 py::classh<ImportedPairingOptions>(m, "ImportedPairingOptions")
246 .def(py::init<>())
247 .def_readwrite("block_size",
248 &ImportedPairingOptions::block_size,
249 "Number of image pairs to match in one batch.")
250 .def_readwrite("match_list_path",
251 &ImportedPairingOptions::match_list_path,
252 "Path to the file with the matches.")
253 .def("check", &ImportedPairingOptions::Check);
254 MakeDataclass(PyImportedPairingOptions);
255
256 auto PyExistingMatchedPairingOptions =
257 py::classh<ExistingMatchedPairingOptions>(m,
258 "ExistingMatchedPairingOptions")
259 .def(py::init<>())
260 .def_readwrite("batch_size",
261 &ExistingMatchedPairingOptions::batch_size)
262 .def("check", &ExistingMatchedPairingOptions::Check);
263 MakeDataclass(PyExistingMatchedPairingOptions);
264
265 auto PyGeometricVerifierOptions =
266 py::classh<GeometricVerifierOptions>(m, "GeometricVerifierOptions")
267 .def(py::init<>())
268 .def_readwrite("num_threads", &GeometricVerifierOptions::num_threads)
269 .def_readwrite("rig_verification",
270 &GeometricVerifierOptions::rig_verification)
271 .def_readwrite("use_existing_relative_pose",
272 &GeometricVerifierOptions::use_existing_relative_pose);
273 MakeDataclass(PyGeometricVerifierOptions);
274
275 m.def(
276 "match_exhaustive",
277 &MatchFeatures<ExhaustivePairingOptions, CreateExhaustiveFeatureMatcher>,
278 "database_path"_a,
279 py::arg_v("matching_options",
280 FeatureMatchingOptions(),
281 "FeatureMatchingOptions()"),
282 py::arg_v("pairing_options",
283 ExhaustivePairingOptions(),
284 "ExhaustivePairingOptions()"),
285 py::arg_v("verification_options",
286 TwoViewGeometryOptions(),
287 "TwoViewGeometryOptions()"),
288 "device"_a = Device::AUTO,
289 "cancellation_token"_a = py::none(),
290 "Exhaustive feature matching");
291
292 m.def("match_spatial",
293 &MatchFeatures<SpatialPairingOptions, CreateSpatialFeatureMatcher>,
294 "database_path"_a,
295 py::arg_v("matching_options",
296 FeatureMatchingOptions(),
297 "FeatureMatchingOptions()"),
298 py::arg_v("pairing_options",
299 SpatialPairingOptions(),
300 "SpatialPairingOptions()"),
301 py::arg_v("verification_options",
302 TwoViewGeometryOptions(),
303 "TwoViewGeometryOptions()"),
304 "device"_a = Device::AUTO,
305 "cancellation_token"_a = py::none(),
306 "Spatial feature matching");
307
308 m.def("match_vocabtree",
309 &MatchFeatures<VocabTreePairingOptions, CreateVocabTreeFeatureMatcher>,
310 "database_path"_a,
311 py::arg_v("matching_options",
312 FeatureMatchingOptions(),
313 "FeatureMatchingOptions()"),
314 py::arg_v("pairing_options",
315 VocabTreePairingOptions(),
316 "VocabTreePairingOptions()"),
317 py::arg_v("verification_options",
318 TwoViewGeometryOptions(),
319 "TwoViewGeometryOptions()"),
320 "device"_a = Device::AUTO,
321 "cancellation_token"_a = py::none(),
322 "Vocab tree feature matching");
323
324 m.def(
325 "match_sequential",
326 &MatchFeatures<SequentialPairingOptions, CreateSequentialFeatureMatcher>,
327 "database_path"_a,
328 py::arg_v("matching_options",
329 FeatureMatchingOptions(),
330 "FeatureMatchingOptions()"),
331 py::arg_v("pairing_options",
332 SequentialPairingOptions(),
333 "SequentialPairingOptions()"),
334 py::arg_v("verification_options",
335 TwoViewGeometryOptions(),
336 "TwoViewGeometryOptions()"),
337 "device"_a = Device::AUTO,
338 "cancellation_token"_a = py::none(),
339 "Sequential feature matching");
340
341 m.def("match_image_pairs",
342 &MatchFeatures<ImportedPairingOptions, CreateImagePairsFeatureMatcher>,
343 "database_path"_a,
344 py::arg_v("matching_options",
345 FeatureMatchingOptions(),
346 "FeatureMatchingOptions()"),
347 py::arg_v("pairing_options",
348 ImportedPairingOptions(),
349 "ImportedPairingOptions()"),
350 py::arg_v("verification_options",
351 TwoViewGeometryOptions(),
352 "TwoViewGeometryOptions()"),
353 "device"_a = Device::AUTO,
354 "cancellation_token"_a = py::none(),
355 "Match features between image pairs specified in a file");
356
357 m.def("verify_matches",
358 &VerifyMatches,
359 "database_path"_a,
360 "pairs_path"_a,
361 py::arg_v(
362 "options", TwoViewGeometryOptions(), "TwoViewGeometryOptions()"),
363 "cancellation_token"_a = py::none(),
364 "Run geometric verification of the matches");
365
366 m.def("geometric_verification",
367 &GeometricVerification,
368 "database_path"_a,
369 py::arg_v("verifier_options",
370 GeometricVerifierOptions(),
371 "GeometricVerifierOptions()"),
372 py::arg_v("pairing_options",
373 ExistingMatchedPairingOptions(),
374 "ExistingMatchedPairingOptions()"),
375 py::arg_v("two_view_geometry_options",
376 TwoViewGeometryOptions(),
377 "TwoViewGeometryOptions()"),
378 "cancellation_token"_a = py::none(),
379 "Run geometric verification on all image pairs in the database");
380
381 m.def("guided_geometric_verification",
382 &GuidedGeometricVerification,
383 "reconstruction"_a,
384 "database_path"_a,
385 py::arg_v("pairing_options",
386 ExistingMatchedPairingOptions(),
387 "ExistingMatchedPairingOptions()"),
388 py::arg_v("two_view_geometry_options",
389 TwoViewGeometryOptions(),
390 "TwoViewGeometryOptions()"),
391 py::arg("num_threads") = -1,
392 "cancellation_token"_a = py::none(),
393 "Run geometric verification given an existing colmap reconstruction on "
394 "all image pairs in the database");
395
396 py::classh<PairGenerator>(m, "PairGenerator")
397 .def("reset", &PairGenerator::Reset)
398 .def("has_finished", &PairGenerator::HasFinished)
399 .def("next", &PairGenerator::Next)
400 .def("all_pairs", &PairGenerator::AllPairs);
401 py::classh<ExhaustivePairGenerator, PairGenerator>(m,
402 "ExhaustivePairGenerator")
403 .def(py::init<const ExhaustivePairingOptions&,
404 const std::shared_ptr<Database>&>(),
405 "options"_a,
406 "database"_a);
407 py::classh<VocabTreePairGenerator, PairGenerator>(m, "VocabTreePairGenerator")
408 .def(py::init<const VocabTreePairingOptions&,
409 const std::shared_ptr<Database>&,
410 const std::vector<image_t>&>(),
411 "options"_a,
412 "database"_a,
413 "query_image_ids"_a = std::vector<image_t>());
414 py::classh<SequentialPairGenerator, PairGenerator>(m,
415 "SequentialPairGenerator")
416 .def(py::init<const SequentialPairingOptions&,
417 const std::shared_ptr<Database>&>(),
418 "options"_a,
419 "database"_a);
420 py::classh<SpatialPairGenerator, PairGenerator>(m, "SpatialPairGenerator")
421 .def(py::init<const SpatialPairingOptions&,
422 const std::shared_ptr<Database>&>(),
423 "options"_a,
424 "database"_a);
425 py::classh<ImportedPairGenerator, PairGenerator>(m, "ImportedPairGenerator")
426 .def(py::init<const ImportedPairingOptions&,
427 const std::shared_ptr<Database>&>(),
428 "options"_a,
429 "database"_a);
430 }