ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
sift.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
11 #include "feature/types.h"
12 #include "util/bitmap.h"
13 
14 class SiftGPU;
15 class SiftMatchGPU;
16 
17 namespace colmap {
18 
20  // Number of threads for feature extraction.
21  int num_threads = -1;
22 
23  // Whether to use the GPU for feature extraction.
24 #ifdef CUDA_ENABLED
25  bool use_gpu = true;
26 #else
27  bool use_gpu = false;
28 #endif
29 
30  // Index of the GPU used for feature extraction. For multi-GPU extraction,
31  // you should separate multiple GPU indices by comma, e.g., "0,1,2,3".
32  std::string gpu_index = "-1";
33 
34  // Maximum image size, otherwise image will be down-scaled.
35  int max_image_size = 3200;
36 
37  // Maximum number of features to detect, keeping larger-scale features.
38  int max_num_features = 8192;
39 
40  // First octave in the pyramid, i.e. -1 upsamples the image by one level.
41  int first_octave = -1;
42 
43  // Number of octaves.
44  int num_octaves = 4;
45 
46  // Number of levels per octave.
48 
49  // Peak threshold for detection.
51 
52  // Edge threshold for detection.
53  double edge_threshold = 10.0;
54 
55  // Estimate affine shape of SIFT features in the form of oriented ellipses
56  // as opposed to original SIFT which estimates oriented disks.
57  bool estimate_affine_shape = false;
58 
59  // Maximum number of orientations per keypoint if not estimate_affine_shape.
61 
62  // Fix the orientation to 0 for upright features.
63  bool upright = false;
64 
65  // Whether to adapt the feature detection depending on the image darkness.
66  // Note that this feature is only available in the OpenGL SiftGPU version.
67  bool darkness_adaptivity = false;
68 
69  // Domain-size pooling parameters. Domain-size pooling computes an average
70  // SIFT descriptor across multiple scales around the detected scale. This
71  // was proposed in "Domain-Size Pooling in Local Descriptors and Network
72  // Architectures", J. Dong and S. Soatto, CVPR 2015. This has been shown to
73  // outperform other SIFT variants and learned descriptors in "Comparative
74  // Evaluation of Hand-Crafted and Learned Local Features", Schönberger,
75  // Hardmeier, Sattler, Pollefeys, CVPR 2016.
76  bool domain_size_pooling = false;
77  double dsp_min_scale = 1.0 / 6.0;
78  double dsp_max_scale = 3.0;
79  int dsp_num_scales = 10;
80 
81  enum class Normalization {
82  // L1-normalizes each descriptor followed by element-wise square
83  // rooting.
84  // This normalization is usually better than standard L2-normalization.
85  // See "Three things everyone should know to improve object retrieval",
86  // Relja Arandjelovic and Andrew Zisserman, CVPR 2012.
87  L1_ROOT,
88  // Each vector is L2-normalized.
89  L2,
90  };
92 
93  bool Check() const;
94 };
95 
97  // Number of threads for feature matching and geometric verification.
98  int num_threads = -1;
99 
100  // Whether to use the GPU for feature matching.
101 #ifdef CUDA_ENABLED
102  bool use_gpu = true;
103 #else
104  bool use_gpu = false;
105 #endif
106 
107  // Index of the GPU used for feature matching. For multi-GPU matching,
108  // you should separate multiple GPU indices by comma, e.g., "0,1,2,3".
109  std::string gpu_index = "-1";
110 
111  // Maximum distance ratio between first and second best match.
112  double max_ratio = 0.8;
113 
114  // Maximum distance to best match.
115  double max_distance = 0.7;
116 
117  // Whether to enable cross checking in matching.
118  bool cross_check = true;
119 
120  // Maximum number of matches.
121  int max_num_matches = 32768;
122 
123  // Maximum epipolar error in pixels for geometric verification.
124  double max_error = 4.0;
125 
126  // Confidence threshold for geometric verification.
127  double confidence = 0.999;
128 
129  // Minimum/maximum number of RANSAC iterations. Note that this option
130  // overrules the min_inlier_ratio option.
131  int min_num_trials = 100;
132  int max_num_trials = 10000;
133 
134  // A priori assumed minimum inlier ratio, which determines the maximum
135  // number of iterations.
136  double min_inlier_ratio = 0.25;
137 
138  // Minimum number of inliers for an image pair to be considered as
139  // geometrically verified.
140  int min_num_inliers = 15;
141 
142  // Whether to attempt to estimate multiple geometric models per image pair.
143  bool multiple_models = false;
144 
145  // Whether to perform guided matching, if geometric verification succeeds.
146  bool guided_matching = false;
147 
148  bool Check() const;
149 };
150 
151 // Extract SIFT features for the given image on the CPU. Only extract
152 // descriptors if the given input is not NULL.
154  const Bitmap& bitmap,
155  FeatureKeypoints* keypoints,
158  const Bitmap& bitmap,
159  FeatureKeypoints* keypoints,
161 
162 // Create a SiftGPU feature extractor. The same SiftGPU instance can be used to
163 // extract features for multiple images. Note a OpenGL context must be made
164 // current in the thread of the caller. If the gpu_index is not -1, the CUDA
165 // version of SiftGPU is used, which produces slightly different results
166 // than the OpenGL implementation.
168  SiftGPU* sift_gpu);
169 
170 // Extract SIFT features for the given image on the GPU.
171 // SiftGPU must already be initialized using `CreateSiftGPU`.
173  const Bitmap& bitmap,
174  SiftGPU* sift_gpu,
175  FeatureKeypoints* keypoints,
177 
178 // Load keypoints and descriptors from text file in the following format:
179 //
180 // LINE_0: NUM_FEATURES DIM
181 // LINE_1: X Y SCALE ORIENTATION D_1 D_2 D_3 ... D_DIM
182 // LINE_I: ...
183 // LINE_NUM_FEATURES: X Y SCALE ORIENTATION D_1 D_2 D_3 ... D_DIM
184 //
185 // where the first line specifies the number of features and the descriptor
186 // dimensionality followed by one line per feature: X, Y, SCALE, ORIENTATION are
187 // of type float and D_J represent the descriptor in the range [0, 255].
188 //
189 // For example:
190 //
191 // 2 4
192 // 0.32 0.12 1.23 1.0 1 2 3 4
193 // 0.32 0.12 1.23 1.0 1 2 3 4
194 //
195 void LoadSiftFeaturesFromTextFile(const std::string& path,
196  FeatureKeypoints* keypoints,
198 
199 // Match the given SIFT features on the CPU.
200 void MatchSiftFeaturesCPUBruteForce(const SiftMatchingOptions& match_options,
201  const FeatureDescriptors& descriptors1,
202  const FeatureDescriptors& descriptors2,
203  FeatureMatches* matches);
204 void MatchSiftFeaturesCPUFLANN(const SiftMatchingOptions& match_options,
205  const FeatureDescriptors& descriptors1,
206  const FeatureDescriptors& descriptors2,
207  FeatureMatches* matches);
208 void MatchSiftFeaturesCPU(const SiftMatchingOptions& match_options,
209  const FeatureDescriptors& descriptors1,
210  const FeatureDescriptors& descriptors2,
211  FeatureMatches* matches);
212 void MatchGuidedSiftFeaturesCPU(const SiftMatchingOptions& match_options,
213  const FeatureKeypoints& keypoints1,
214  const FeatureKeypoints& keypoints2,
215  const FeatureDescriptors& descriptors1,
216  const FeatureDescriptors& descriptors2,
217  TwoViewGeometry* two_view_geometry);
218 
219 // Create a SiftGPU feature matcher. Note that if CUDA is not available or the
220 // gpu_index is -1, the OpenGLContextManager must be created in the main thread
221 // of the Qt application before calling this function. The same SiftMatchGPU
222 // instance can be used to match features between multiple image pairs.
223 bool CreateSiftGPUMatcher(const SiftMatchingOptions& match_options,
224  SiftMatchGPU* sift_match_gpu);
225 
226 // Match the given SIFT features on the GPU. If either of the descriptors is
227 // NULL, the keypoints/descriptors will not be uploaded and the previously
228 // uploaded descriptors will be reused for the matching.
229 void MatchSiftFeaturesGPU(const SiftMatchingOptions& match_options,
230  const FeatureDescriptors* descriptors1,
231  const FeatureDescriptors* descriptors2,
232  SiftMatchGPU* sift_match_gpu,
233  FeatureMatches* matches);
234 void MatchGuidedSiftFeaturesGPU(const SiftMatchingOptions& match_options,
235  const FeatureKeypoints* keypoints1,
236  const FeatureKeypoints* keypoints2,
237  const FeatureDescriptors* descriptors1,
238  const FeatureDescriptors* descriptors2,
239  SiftMatchGPU* sift_match_gpu,
240  TwoViewGeometry* two_view_geometry);
241 
242 } // namespace colmap
static const std::string path
Definition: PointCloud.cpp:59
bool ExtractCovariantSiftFeaturesCPU(const SiftExtractionOptions &options, const Bitmap &bitmap, FeatureKeypoints *keypoints, FeatureDescriptors *descriptors)
Definition: sift.cc:599
void MatchGuidedSiftFeaturesGPU(const SiftMatchingOptions &match_options, const FeatureKeypoints *keypoints1, const FeatureKeypoints *keypoints2, const FeatureDescriptors *descriptors1, const FeatureDescriptors *descriptors2, SiftMatchGPU *sift_match_gpu, TwoViewGeometry *two_view_geometry)
Definition: sift.cc:1267
bool CreateSiftGPUExtractor(const SiftExtractionOptions &options, SiftGPU *sift_gpu)
Definition: sift.cc:776
void MatchSiftFeaturesGPU(const SiftMatchingOptions &match_options, const FeatureDescriptors *descriptors1, const FeatureDescriptors *descriptors2, SiftMatchGPU *sift_match_gpu, FeatureMatches *matches)
Definition: sift.cc:1195
bool CreateSiftGPUMatcher(const SiftMatchingOptions &match_options, SiftMatchGPU *sift_match_gpu)
Definition: sift.cc:1124
Eigen::Matrix< uint8_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > FeatureDescriptors
Definition: types.h:79
void MatchSiftFeaturesCPUBruteForce(const SiftMatchingOptions &match_options, const FeatureDescriptors &descriptors1, const FeatureDescriptors &descriptors2, FeatureMatches *matches)
Definition: sift.cc:998
void MatchSiftFeaturesCPU(const SiftMatchingOptions &match_options, const FeatureDescriptors &descriptors1, const FeatureDescriptors &descriptors2, FeatureMatches *matches)
Definition: sift.cc:1042
bool ExtractSiftFeaturesCPU(const SiftExtractionOptions &options, const Bitmap &bitmap, FeatureKeypoints *keypoints, FeatureDescriptors *descriptors)
Definition: sift.cc:419
void MatchGuidedSiftFeaturesCPU(const SiftMatchingOptions &match_options, const FeatureKeypoints &keypoints1, const FeatureKeypoints &keypoints2, const FeatureDescriptors &descriptors1, const FeatureDescriptors &descriptors2, TwoViewGeometry *two_view_geometry)
Definition: sift.cc:1050
std::vector< FeatureKeypoint > FeatureKeypoints
Definition: types.h:77
std::vector< FeatureMatch > FeatureMatches
Definition: types.h:80
bool ExtractSiftFeaturesGPU(const SiftExtractionOptions &options, const Bitmap &bitmap, SiftGPU *sift_gpu, FeatureKeypoints *keypoints, FeatureDescriptors *descriptors)
Definition: sift.cc:875
void LoadSiftFeaturesFromTextFile(const std::string &path, FeatureKeypoints *keypoints, FeatureDescriptors *descriptors)
Definition: sift.cc:943
void MatchSiftFeaturesCPUFLANN(const SiftMatchingOptions &match_options, const FeatureDescriptors &descriptors1, const FeatureDescriptors &descriptors2, FeatureMatches *matches)
Definition: sift.cc:1013
CorePointDescSet * descriptors
Normalization normalization
Definition: sift.h:91
std::string gpu_index
Definition: sift.h:32
bool Check() const
Definition: sift.cc:403
std::string gpu_index
Definition: sift.h:109