AliceVision
Photogrammetric Computer Vision Framework
SIFT.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2016 AliceVision contributors.
3 // Copyright (c) 2012 openMVG contributors.
4 // This Source Code Form is subject to the terms of the Mozilla Public License,
5 // v. 2.0. If a copy of the MPL was not distributed with this file,
6 // You can obtain one at https://mozilla.org/MPL/2.0/.
7 
8 #pragma once
9 
10 #include <aliceVision/feature/Descriptor.hpp>
11 #include <aliceVision/feature/ImageDescriber.hpp>
12 #include <aliceVision/feature/regionsFactory.hpp>
13 #include <aliceVision/config.hpp>
14 #include <aliceVision/system/Logger.hpp>
15 
16 #include <aliceVision/feature/imageStats.hpp>
17 
18 extern "C"
19 {
20 #include "nonFree/sift/vl/sift.h"
21 }
22 
23 #include <iostream>
24 #include <numeric>
25 #include <stdexcept>
26 
27 namespace aliceVision {
28 namespace feature {
29 
35 struct SiftParams
36 {
38  int _firstOctave = 0;
40  int _numScales = 3;
42  float _edgeThreshold = 10.0f;
44  float _peakThreshold = 0.005f;
46  float _relativePeakThreshold = 0.01f;
47  EFeatureConstrastFiltering _contrastFiltering = EFeatureConstrastFiltering::GridSort;
48 
49  std::size_t _gridSize = 4;
50  std::size_t _maxTotalKeypoints = 10000;
52  bool _rootSift = true;
53 
54  virtual void setPreset(ConfigurationPreset preset);
55 
56  int getImageFirstOctave(int w, int h) const
57  {
58  return _firstOctave - (w * h <= 3000 * 2000 ? 1 : 0); // -1 to upscale for small resolutions
59  }
60 };
61 
62 // VLFeat Instance management
64 {
65  public:
66  static void initialize();
67 
68  static void destroy();
69 
70  private:
71  static int nbInstances;
72 };
73 
74 // convertSIFT
76 template<typename TOut>
77 inline void convertSIFT(const vl_sift_pix* descr, Descriptor<TOut, 128>& descriptor, bool rootSift);
78 
79 template<>
80 inline void convertSIFT<float>(const vl_sift_pix* descr, Descriptor<float, 128>& descriptor, bool rootSift)
81 {
82  if (rootSift)
83  {
84  const float sum = std::accumulate(descr, descr + 128, 0.0f);
85  for (int k = 0; k < 128; ++k)
86  descriptor[k] = std::floor(512.f * sqrt(descr[k] / sum));
87  }
88  else
89  {
90  for (int k = 0; k < 128; ++k)
91  descriptor[k] = std::floor(512.f * descr[k]);
92  }
93 }
94 
95 template<>
96 inline void convertSIFT<unsigned char>(const vl_sift_pix* descr, Descriptor<unsigned char, 128>& descriptor, bool rootSift)
97 {
98  if (rootSift)
99  {
100  // rootsift = sqrt( sift / sum(sift) );
101  const float sum = std::accumulate(descr, descr + 128, 0.0f);
102  for (int k = 0; k < 128; ++k)
103  descriptor[k] = static_cast<unsigned char>(512.f * sqrt(descr[k] / sum));
104  }
105  else
106  {
107  for (int k = 0; k < 128; ++k)
108  descriptor[k] = static_cast<unsigned char>(512.f * descr[k]);
109  }
110 }
111 
119 std::size_t getMemoryConsumptionVLFeat(std::size_t width, std::size_t height, const SiftParams& params);
120 
131 template<typename T>
132 bool extractSIFT(const image::Image<float>& image,
133  std::unique_ptr<Regions>& regions,
134  const SiftParams& params,
135  bool orientation,
136  const image::Image<unsigned char>* mask);
137 
138 } // namespace feature
139 } // namespace aliceVision
aliceVision::feature::SiftParams::_relativePeakThreshold
float _relativePeakThreshold
Min contrast (relative to variance median)
Definition: SIFT.hpp:46
aliceVision::feature::VLFeatInstance
Definition: SIFT.hpp:63
aliceVision::feature::SiftParams::_rootSift
bool _rootSift
see [1]
Definition: SIFT.hpp:52
aliceVision::feature::SiftParams
Definition: SIFT.hpp:35
aliceVision::feature::SiftParams::_peakThreshold
float _peakThreshold
Min contrast.
Definition: SIFT.hpp:44
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::feature::Descriptor
Definition: Descriptor.hpp:29
aliceVision::feature::SiftParams::_numScales
int _numScales
Scales per octave.
Definition: SIFT.hpp:40
aliceVision::feature::SiftParams::_firstOctave
int _firstOctave
Use original image, or perform an upscale if == -1.
Definition: SIFT.hpp:38
aliceVision::feature::SiftParams::_edgeThreshold
float _edgeThreshold
Max ratio of Hessian eigenvalues.
Definition: SIFT.hpp:42
aliceVision::feature::ConfigurationPreset
Definition: ImageDescriber.hpp:118