| 1 |
#include "colmap/feature/matcher.h" |
| 2 |
#include "colmap/feature/sift.h" |
| 3 |
#include "colmap/feature/utils.h" |
| 4 |
#ifdef COLMAP_ONNX_ENABLED |
| 5 |
#include "colmap/feature/aliked.h" |
| 6 |
#include "colmap/feature/loma.h" |
| 7 |
#include "colmap/feature/onnx_matchers.h" |
| 8 |
#endif |
| 9 |
|
| 10 |
#include "pycolmap/feature/types.h" |
| 11 |
#include "pycolmap/helpers.h" |
| 12 |
#include "pycolmap/utils.h" |
| 13 |
|
| 14 |
#include <Eigen/Core> |
| 15 |
#include <pybind11/eigen.h> |
| 16 |
#include <pybind11/pybind11.h> |
| 17 |
#include <pybind11/stl.h> |
| 18 |
|
| 19 |
using namespace colmap; |
| 20 |
using namespace pybind11::literals; |
| 21 |
namespace py = pybind11; |
| 22 |
|
| 23 |
namespace { |
| 24 |
|
| 25 |
class PyFeatureMatcher : public FeatureMatcher, |
| 26 |
py::trampoline_self_life_support { |
| 27 |
public: |
| 28 |
using MatcherImage = FeatureMatcher::Image; |
| 29 |
|
| 30 |
static std::unique_ptr<FeatureMatcher> CreateOnDevice( |
| 31 |
std::optional<FeatureMatchingOptions> options, Device device) { |
| 32 |
if (options) { |
| 33 |
if (options->use_gpu != IsGPU(device)) { |
| 34 |
LOG(WARNING) << "FeatureMatchingOptions::use_gpu does not match " |
| 35 |
"device. FeatureMatchingOptions::use_gpu is ignored."; |
| 36 |
} |
| 37 |
} else { |
| 38 |
options = FeatureMatchingOptions(); |
| 39 |
} |
| 40 |
if (options->sift->cpu_descriptor_index_cache == nullptr) { |
| 41 |
options->sift->cpu_brute_force_matcher = true; |
| 42 |
} |
| 43 |
options->use_gpu = IsGPU(device); |
| 44 |
return THROW_CHECK_NOTNULL(FeatureMatcher::Create(*options)); |
| 45 |
} |
| 46 |
|
| 47 |
void Match(const MatcherImage& image1, |
| 48 |
const MatcherImage& image2, |
| 49 |
FeatureMatches* matches) override { |
| 50 |
PYBIND11_OVERRIDE_PURE( |
| 51 |
void, FeatureMatcher, Match, image1, image2, matches); |
| 52 |
} |
| 53 |
|
| 54 |
void MatchGuided(double max_error, |
| 55 |
const MatcherImage& image1, |
| 56 |
const MatcherImage& image2, |
| 57 |
TwoViewGeometry* two_view_geometry) override { |
| 58 |
PYBIND11_OVERRIDE_PURE(void, |
| 59 |
FeatureMatcher, |
| 60 |
MatchGuided, |
| 61 |
max_error, |
| 62 |
image1, |
| 63 |
image2, |
| 64 |
two_view_geometry); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
} // namespace |
| 69 |
|
| 70 |
void BindFeatureMatching(py::module& m) { |
| 71 |
py::enum_<FeatureMatcherType>(m, "FeatureMatcherType") |
| 72 |
.value("UNDEFINED", FeatureMatcherType::UNDEFINED) |
| 73 |
.value("SIFT_BRUTEFORCE", FeatureMatcherType::SIFT_BRUTEFORCE) |
| 74 |
.value("SIFT_LIGHTGLUE", FeatureMatcherType::SIFT_LIGHTGLUE) |
| 75 |
.value("ALIKED_BRUTEFORCE", FeatureMatcherType::ALIKED_BRUTEFORCE) |
| 76 |
.value("ALIKED_LIGHTGLUE", FeatureMatcherType::ALIKED_LIGHTGLUE) |
| 77 |
.value("LOMA_BRUTEFORCE", FeatureMatcherType::LOMA_BRUTEFORCE) |
| 78 |
.value("LOMA_B", FeatureMatcherType::LOMA_B) |
| 79 |
.value("LOMA_B128", FeatureMatcherType::LOMA_B128) |
| 80 |
.value("LOMA_R", FeatureMatcherType::LOMA_R) |
| 81 |
.value("LOMA_L", FeatureMatcherType::LOMA_L) |
| 82 |
.value("LOMA_G", FeatureMatcherType::LOMA_G); |
| 83 |
|
| 84 |
#ifdef COLMAP_ONNX_ENABLED |
| 85 |
auto PyBruteForceONNXMatchingOptions = |
| 86 |
py::classh<BruteForceONNXMatchingOptions>(m, |
| 87 |
"BruteForceONNXMatchingOptions") |
| 88 |
.def(py::init<>()) |
| 89 |
.def_readwrite("min_cossim", |
| 90 |
&BruteForceONNXMatchingOptions::min_cossim, |
| 91 |
"Minimum cosine similarity for a match to be " |
| 92 |
"considered valid.") |
| 93 |
.def_readwrite("max_ratio", |
| 94 |
&BruteForceONNXMatchingOptions::max_ratio, |
| 95 |
"Maximum ratio for Lowe's ratio test.") |
| 96 |
.def_readwrite("cross_check", |
| 97 |
&BruteForceONNXMatchingOptions::cross_check, |
| 98 |
"Enable cross-checking (mutual nearest neighbor).") |
| 99 |
.def_readwrite("model_path", |
| 100 |
&BruteForceONNXMatchingOptions::model_path, |
| 101 |
"Path to the ONNX model file.") |
| 102 |
.def("check", &BruteForceONNXMatchingOptions::Check); |
| 103 |
MakeDataclass(PyBruteForceONNXMatchingOptions); |
| 104 |
|
| 105 |
auto PyLightGlueONNXMatchingOptions = |
| 106 |
py::classh<LightGlueONNXMatchingOptions>(m, |
| 107 |
"LightGlueONNXMatchingOptions") |
| 108 |
.def(py::init<>()) |
| 109 |
.def_readwrite("min_score", |
| 110 |
&LightGlueONNXMatchingOptions::min_score, |
| 111 |
"Minimum match score threshold.") |
| 112 |
.def_readwrite("model_path", |
| 113 |
&LightGlueONNXMatchingOptions::model_path, |
| 114 |
"Path to the LightGlue ONNX model file.") |
| 115 |
.def("check", &LightGlueONNXMatchingOptions::Check); |
| 116 |
MakeDataclass(PyLightGlueONNXMatchingOptions); |
| 117 |
|
| 118 |
auto PyAlikedMatchingOptions = |
| 119 |
py::classh<AlikedMatchingOptions>(m, "AlikedMatchingOptions") |
| 120 |
.def(py::init<>()) |
| 121 |
.def_readwrite("brute_force", |
| 122 |
&AlikedMatchingOptions::brute_force, |
| 123 |
"Brute-force matching options.") |
| 124 |
.def_readwrite("lightglue", |
| 125 |
&AlikedMatchingOptions::lightglue, |
| 126 |
"LightGlue matching options.") |
| 127 |
.def("check", &AlikedMatchingOptions::Check); |
| 128 |
MakeDataclass(PyAlikedMatchingOptions); |
| 129 |
|
| 130 |
auto PyLomaVariantMatcherOptions = |
| 131 |
py::classh<LomaMatchingOptions::Variant>(m, "LomaVariantMatcherOptions") |
| 132 |
.def(py::init<>()) |
| 133 |
.def_readwrite("model_path", |
| 134 |
&LomaMatchingOptions::Variant::model_path, |
| 135 |
"Path to the fp32 ONNX model file for this variant.") |
| 136 |
.def_readwrite("model_path_bf16", |
| 137 |
&LomaMatchingOptions::Variant::model_path_bf16, |
| 138 |
"Path to the bf16 ONNX model file for this variant."); |
| 139 |
MakeDataclass(PyLomaVariantMatcherOptions); |
| 140 |
|
| 141 |
auto PyLomaMatchingOptions = |
| 142 |
py::classh<LomaMatchingOptions>(m, "LomaMatchingOptions") |
| 143 |
.def(py::init<>()) |
| 144 |
.def_readwrite("min_score", |
| 145 |
&LomaMatchingOptions::min_score, |
| 146 |
"Matching filter, matches LG.") |
| 147 |
.def_readwrite("use_bf16", |
| 148 |
&LomaMatchingOptions::use_bf16, |
| 149 |
"Whether to use the bf16 matcher variant when " |
| 150 |
"supported by the selected ONNX execution provider " |
| 151 |
"(falls back to fp32 otherwise).") |
| 152 |
.def_readwrite("b", &LomaMatchingOptions::b, "LOMA_B model paths.") |
| 153 |
.def_readwrite( |
| 154 |
"b128", &LomaMatchingOptions::b128, "LOMA_B128 model paths.") |
| 155 |
.def_readwrite("r", &LomaMatchingOptions::r, "LOMA_R model paths.") |
| 156 |
.def_readwrite("l", &LomaMatchingOptions::l, "LOMA_L model paths.") |
| 157 |
.def_readwrite("g", &LomaMatchingOptions::g, "LOMA_G model paths.") |
| 158 |
.def_readwrite("brute_force", |
| 159 |
&LomaMatchingOptions::brute_force, |
| 160 |
"Brute-force matching options.") |
| 161 |
.def("check", &LomaMatchingOptions::Check); |
| 162 |
MakeDataclass(PyLomaMatchingOptions); |
| 163 |
#endif |
| 164 |
|
| 165 |
auto PySiftMatchingOptions = |
| 166 |
py::classh<SiftMatchingOptions>(m, "SiftMatchingOptions") |
| 167 |
.def(py::init<>()) |
| 168 |
.def_readwrite( |
| 169 |
"max_ratio", |
| 170 |
&SiftMatchingOptions::max_ratio, |
| 171 |
"Maximum distance ratio between first and second best match.") |
| 172 |
.def_readwrite("max_distance", |
| 173 |
&SiftMatchingOptions::max_distance, |
| 174 |
"Maximum distance to best match.") |
| 175 |
.def_readwrite("cross_check", |
| 176 |
&SiftMatchingOptions::cross_check, |
| 177 |
"Whether to enable cross checking in matching.") |
| 178 |
.def_readwrite( |
| 179 |
"cpu_brute_force_matcher", |
| 180 |
&SiftMatchingOptions::cpu_brute_force_matcher, |
| 181 |
"Whether to use brute-force instead of faiss based CPU matching.") |
| 182 |
#ifdef COLMAP_ONNX_ENABLED |
| 183 |
.def_readwrite("lightglue", |
| 184 |
&SiftMatchingOptions::lightglue, |
| 185 |
"LightGlue matching options.") |
| 186 |
#endif |
| 187 |
.def("check", &SiftMatchingOptions::Check); |
| 188 |
MakeDataclass(PySiftMatchingOptions); |
| 189 |
|
| 190 |
auto PyFeatureMatchingOptions = |
| 191 |
py::classh<FeatureMatchingOptions>(m, "FeatureMatchingOptions") |
| 192 |
.def(py::init<FeatureMatcherType>(), |
| 193 |
"type"_a = FeatureMatcherType::SIFT_BRUTEFORCE) |
| 194 |
.def_readwrite("type", &FeatureMatchingOptions::type) |
| 195 |
.def_readwrite("num_threads", &FeatureMatchingOptions::num_threads) |
| 196 |
.def_readwrite("use_gpu", &FeatureMatchingOptions::use_gpu) |
| 197 |
.def_readwrite("gpu_index", |
| 198 |
&FeatureMatchingOptions::gpu_index, |
| 199 |
"Index of the GPU used for feature matching. For " |
| 200 |
"multi-GPU matching, " |
| 201 |
"you should separate multiple GPU indices by comma, " |
| 202 |
"e.g., \"0,1,2,3\".") |
| 203 |
.def_readwrite("max_num_matches", |
| 204 |
&FeatureMatchingOptions::max_num_matches, |
| 205 |
"Maximum number of matches.") |
| 206 |
.def_readwrite("guided_matching", |
| 207 |
&FeatureMatchingOptions::guided_matching, |
| 208 |
"Whether to perform guided matching, if geometric " |
| 209 |
"verification succeeds.") |
| 210 |
.def_readwrite( |
| 211 |
"skip_geometric_verification", |
| 212 |
&FeatureMatchingOptions::skip_geometric_verification, |
| 213 |
"Skips the geometric verification stage and forwards matches " |
| 214 |
"unchanged. Ignored when guided matching is enabled, because " |
| 215 |
"guided matching depends on the two-view geometry produced by " |
| 216 |
"geometric verification.") |
| 217 |
.def_readwrite("rig_verification", |
| 218 |
&FeatureMatchingOptions::rig_verification, |
| 219 |
"Whether to perform geometric verification using rig " |
| 220 |
"constraints between pairs of non-trivial frames. If " |
| 221 |
"disabled, performs geometric two-view verification " |
| 222 |
"for non-trivial frames without rig constraints. " |
| 223 |
"Ignored when skip_geometric_verification is enabled.") |
| 224 |
.def_readwrite( |
| 225 |
"skip_image_pairs_in_same_frame", |
| 226 |
&FeatureMatchingOptions::skip_image_pairs_in_same_frame, |
| 227 |
"Whether to skip matching images within the same frame. This is " |
| 228 |
"useful for the case of non-overlapping cameras in a rig.") |
| 229 |
.def_readwrite("sift", &FeatureMatchingOptions::sift) |
| 230 |
.def("check", &FeatureMatchingOptions::Check); |
| 231 |
#ifdef COLMAP_ONNX_ENABLED |
| 232 |
PyFeatureMatchingOptions.def_readwrite("aliked", |
| 233 |
&FeatureMatchingOptions::aliked); |
| 234 |
PyFeatureMatchingOptions.def_readwrite("loma", &FeatureMatchingOptions::loma); |
| 235 |
#endif |
| 236 |
MakeDataclass(PyFeatureMatchingOptions); |
| 237 |
|
| 238 |
auto PyFeatureMatcherCls = |
| 239 |
py::classh<FeatureMatcher, PyFeatureMatcher>(m, "FeatureMatcher"); |
| 240 |
|
| 241 |
PyFeatureMatcherCls |
| 242 |
.def_static("create", |
| 243 |
&PyFeatureMatcher::CreateOnDevice, |
| 244 |
"options"_a = std::nullopt, |
| 245 |
"device"_a = Device::AUTO) |
| 246 |
.def( |
| 247 |
"match", |
| 248 |
[](FeatureMatcher& self, |
| 249 |
const FeatureKeypoints* keypoints1, |
| 250 |
std::shared_ptr<const FeatureDescriptors> descriptors1, |
| 251 |
const FeatureKeypoints* keypoints2, |
| 252 |
std::shared_ptr<const FeatureDescriptors> descriptors2) { |
| 253 |
FeatureMatcher::Image image1; |
| 254 |
if (keypoints1) { |
| 255 |
image1.keypoints = std::shared_ptr<const FeatureKeypoints>( |
| 256 |
std::shared_ptr<void>(), keypoints1); |
| 257 |
} |
| 258 |
image1.descriptors = std::move(descriptors1); |
| 259 |
FeatureMatcher::Image image2; |
| 260 |
if (keypoints2) { |
| 261 |
image2.keypoints = std::shared_ptr<const FeatureKeypoints>( |
| 262 |
std::shared_ptr<void>(), keypoints2); |
| 263 |
} |
| 264 |
image2.descriptors = std::move(descriptors2); |
| 265 |
FeatureMatches matches; |
| 266 |
self.Match(image1, image2, &matches); |
| 267 |
return MatchesToMatrix(matches); |
| 268 |
}, |
| 269 |
"keypoints1"_a, |
| 270 |
"descriptors1"_a, |
| 271 |
"keypoints2"_a, |
| 272 |
"descriptors2"_a, |
| 273 |
"Match features between two images. Keypoints are optional. " |
| 274 |
"Returns an Nx2 matrix of point2D indices.") |
| 275 |
.def( |
| 276 |
"match_guided", |
| 277 |
[](FeatureMatcher& self, |
| 278 |
double max_error, |
| 279 |
const FeatureKeypoints& keypoints1, |
| 280 |
std::shared_ptr<const FeatureDescriptors> descriptors1, |
| 281 |
const Camera& camera1, |
| 282 |
const FeatureKeypoints& keypoints2, |
| 283 |
std::shared_ptr<const FeatureDescriptors> descriptors2, |
| 284 |
const Camera& camera2, |
| 285 |
TwoViewGeometry& two_view_geometry) { |
| 286 |
FeatureMatcher::Image image1; |
| 287 |
image1.camera = &camera1; |
| 288 |
image1.keypoints = std::shared_ptr<const FeatureKeypoints>( |
| 289 |
std::shared_ptr<void>(), &keypoints1); |
| 290 |
image1.descriptors = std::move(descriptors1); |
| 291 |
FeatureMatcher::Image image2; |
| 292 |
image2.camera = &camera2; |
| 293 |
image2.keypoints = std::shared_ptr<const FeatureKeypoints>( |
| 294 |
std::shared_ptr<void>(), &keypoints2); |
| 295 |
image2.descriptors = std::move(descriptors2); |
| 296 |
self.MatchGuided(max_error, image1, image2, &two_view_geometry); |
| 297 |
}, |
| 298 |
"max_error"_a, |
| 299 |
"keypoints1"_a, |
| 300 |
"descriptors1"_a, |
| 301 |
"camera1"_a, |
| 302 |
"keypoints2"_a, |
| 303 |
"descriptors2"_a, |
| 304 |
"camera2"_a, |
| 305 |
"two_view_geometry"_a, |
| 306 |
"Perform guided matching using existing two-view geometry. " |
| 307 |
"Updates the two_view_geometry in-place."); |
| 308 |
} |