AliceVision
Photogrammetric Computer Vision Framework
ImageDescriber.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/numeric/numeric.hpp>
11 #include <aliceVision/feature/imageDescriberCommon.hpp>
12 #include <aliceVision/feature/Regions.hpp>
13 #include <aliceVision/image/Image.hpp>
14 #include <memory>
15 
16 #include <string>
17 #include <iostream>
18 
19 namespace aliceVision {
20 namespace feature {
21 
25 enum class EImageDescriberPreset
26 {
27  LOW = 0,
28  MEDIUM,
29  NORMAL,
30  HIGH,
31  ULTRA
32 };
33 
34 inline std::string EImageDescriberPreset_information()
35 {
36  return "Feature preset controls the density of the feature extraction:\n"
37  "* LOW: Very low density (max 1K points).\n"
38  "* MEDIUM: Low density (max 5K points).\n"
39  "* NORMAL: Default feature density (max 10K points).\n"
40  "* HIGH: High density (max 50K points).\n"
41  "* ULTRA: Very high density (max 100K points). Can use large amount of storage and large amount of computation. Use only on small "
42  "datasets.\n";
43 }
44 
45 EImageDescriberPreset EImageDescriberPreset_stringToEnum(const std::string& imageDescriberPreset);
46 std::string EImageDescriberPreset_enumToString(const EImageDescriberPreset imageDescriberPreset);
47 std::ostream& operator<<(std::ostream& os, EImageDescriberPreset p);
48 std::istream& operator>>(std::istream& in, EImageDescriberPreset& p);
49 
53 enum class EFeatureQuality
54 {
55  LOW = 0,
56  MEDIUM,
57  NORMAL,
58  HIGH,
59  ULTRA,
60 };
61 
62 inline std::string EFeatureQuality_information()
63 {
64  return "Feature extraction contains a trade-off between speed and result accuracy:\n"
65  "* LOW: Very quick results.\n"
66  "* MEDIUM: Quick results.\n"
67  "* NORMAL: Default feature quality.\n"
68  "* HIGH: Improved quality over performances.\n"
69  "* ULTRA: Highest quality at the expense of high computational cost.\n";
70 }
71 
72 EFeatureQuality EFeatureQuality_stringToEnum(const std::string& v);
73 std::string EFeatureQuality_enumToString(const EFeatureQuality v);
74 std::ostream& operator<<(std::ostream& os, EFeatureQuality v);
75 std::istream& operator>>(std::istream& in, EFeatureQuality& v);
76 
80 enum class EFeatureConstrastFiltering
81 {
83  Static = 0,
85  AdaptiveToMedianVariance,
87  NoFiltering,
89  GridSortOctaves,
91  GridSort,
93  GridSortScaleSteps,
95  GridSortOctaveSteps,
97  NonExtremaFiltering
98 };
99 
100 inline std::string EFeatureConstrastFiltering_information()
101 {
102  return "Contrast filtering method to ignore features with too low contrast that can be consided as noise:\n"
103  "* Static: Fixed threshold.\n"
104  "* AdaptiveToMedianVariance: Based on image content analysis.\n"
105  "* NoFiltering: Disable contrast filtering.\n"
106  "* GridSortOctaves: Grid sort by peak value per octave and by scale at the end.\n"
107  "* GridSort: Grid sort by scale*peakValue per octave and at the end.\n"
108  "* GridSortScaleSteps: Grid sort per scale steps and at the end (scale and then peak value).\n"
109  "* GridSortOctaveSteps: Grid sort per octaves and at the end (scale and then peak value).\n"
110  "* NonExtremaFiltering: Filter non-extrema peak values.\n";
111 }
112 
113 EFeatureConstrastFiltering EFeatureConstrastFiltering_stringToEnum(const std::string& v);
114 std::string EFeatureConstrastFiltering_enumToString(const EFeatureConstrastFiltering v);
115 std::ostream& operator<<(std::ostream& os, EFeatureConstrastFiltering v);
116 std::istream& operator>>(std::istream& in, EFeatureConstrastFiltering& v);
117 
119 {
120  EImageDescriberPreset descPreset{EImageDescriberPreset::NORMAL};
121  int maxNbFeatures{0};
122  EFeatureQuality quality{EFeatureQuality::NORMAL};
123  bool gridFiltering{true};
124  EFeatureConstrastFiltering contrastFiltering{EFeatureConstrastFiltering::Static};
125  float relativePeakThreshold{0.02f};
126 
127  inline ConfigurationPreset& setDescPreset(EImageDescriberPreset v)
128  {
129  descPreset = v;
130  return *this;
131  }
132  inline ConfigurationPreset& setDescPreset(const std::string& v)
133  {
134  descPreset = EImageDescriberPreset_stringToEnum(v);
135  return *this;
136  }
137 
138  inline ConfigurationPreset& setGridFiltering(bool v)
139  {
140  gridFiltering = v;
141  return *this;
142  }
143 
144  inline ConfigurationPreset& setContrastFiltering(EFeatureConstrastFiltering v)
145  {
146  contrastFiltering = v;
147  return *this;
148  }
149  inline ConfigurationPreset& setContrastFiltering(const std::string& v)
150  {
151  contrastFiltering = EFeatureConstrastFiltering_stringToEnum(v);
152  return *this;
153  }
154 };
155 
160 {
161  public:
162  ImageDescriber() = default;
163 
164  virtual ~ImageDescriber() = default;
165 
170  virtual bool useCuda() const = 0;
171 
176  virtual bool useFloatImage() const = 0;
177 
182  virtual EImageDescriberType getDescriberType() const = 0;
183 
191  virtual std::size_t getMemoryConsumption(std::size_t width, std::size_t height) const = 0;
192 
197  virtual void setUpRight([[maybe_unused]] bool upRight) {}
198 
203  virtual void setUseCuda([[maybe_unused]] bool useCuda) {}
204 
209  virtual void setCudaPipe([[maybe_unused]] int pipe) {}
210 
215  virtual void setConfigurationPreset(ConfigurationPreset preset) = 0;
216 
224  virtual bool describe([[maybe_unused]] const image::Image<unsigned char>& image,
225  [[maybe_unused]] std::unique_ptr<Regions>& regions,
226  [[maybe_unused]] const image::Image<unsigned char>* mask = nullptr)
227  {
228  throw std::logic_error("Cannot use " + EImageDescriberType_enumToString(getDescriberType()) + " image describer with an 8-bit image.");
229  return false;
230  }
231 
239  virtual bool describe([[maybe_unused]] const image::Image<float>& image,
240  [[maybe_unused]] std::unique_ptr<Regions>& regions,
241  [[maybe_unused]] const image::Image<unsigned char>* mask = nullptr)
242  {
243  throw std::logic_error("Cannot use " + EImageDescriberType_enumToString(getDescriberType()) + " image describer with a float image.");
244  return false;
245  }
246 
251  virtual void allocate(std::unique_ptr<Regions>& regions) const = 0;
252 
253  // IO - one file for region features, one file for region descriptors
254 
255  void Load(Regions* regions, const std::string& sfileNameFeats, const std::string& sfileNameDescs) const
256  {
257  regions->Load(sfileNameFeats, sfileNameDescs);
258  }
259 
260  void Save(const Regions* regions, const std::string& sfileNameFeats, const std::string& sfileNameDescs) const;
261 
262  void LoadFeatures(Regions* regions, const std::string& sfileNameFeats) const { regions->LoadFeatures(sfileNameFeats); }
263 };
264 
269 std::unique_ptr<ImageDescriber> createImageDescriber(EImageDescriberType imageDescriberType);
270 
271 } // namespace feature
272 } // namespace aliceVision
aliceVision::feature::ImageDescriber::useFloatImage
virtual bool useFloatImage() const =0
Check if the image describer use float image.
aliceVision::feature::ImageDescriber::describe
virtual bool describe([[maybe_unused]] const image::Image< unsigned char > &image, [[maybe_unused]] std::unique_ptr< Regions > &regions, [[maybe_unused]] const image::Image< unsigned char > *mask=nullptr)
Detect regions on the 8-bit image and compute their attributes (description)
Definition: ImageDescriber.hpp:224
aliceVision::feature::ImageDescriber::setCudaPipe
virtual void setCudaPipe([[maybe_unused]] int pipe)
Set the CUDA pipe.
Definition: ImageDescriber.hpp:209
aliceVision::feature::ImageDescriber::getDescriberType
virtual EImageDescriberType getDescriberType() const =0
Get the corresponding EImageDescriberType.
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::feature::ImageDescriber
A pure virtual class for image description computation.
Definition: ImageDescriber.hpp:159
aliceVision::feature::ImageDescriber::useCuda
virtual bool useCuda() const =0
Check if the image describer use CUDA.
aliceVision::image::Image< unsigned char >
aliceVision::feature::ImageDescriber::allocate
virtual void allocate(std::unique_ptr< Regions > &regions) const =0
Allocate Regions type depending of the ImageDescriber.
aliceVision::feature::ImageDescriber::setUpRight
virtual void setUpRight([[maybe_unused]] bool upRight)
Set whether the image describer is always upright.
Definition: ImageDescriber.hpp:197
aliceVision::feature::ImageDescriber::getMemoryConsumption
virtual std::size_t getMemoryConsumption(std::size_t width, std::size_t height) const =0
Get the total amount of RAM needed for a feature extraction of an image of the given dimension.
aliceVision::feature::Regions
The Regions class describe a set of regions extracted from an image. It contains both a feature (posi...
Definition: Regions.hpp:46
aliceVision::feature::ImageDescriber::setUseCuda
virtual void setUseCuda([[maybe_unused]] bool useCuda)
Set whether the image describer needs to use CUDA implementation.
Definition: ImageDescriber.hpp:203
aliceVision::feature::ConfigurationPreset
Definition: ImageDescriber.hpp:118
aliceVision::feature::ImageDescriber::describe
virtual bool describe([[maybe_unused]] const image::Image< float > &image, [[maybe_unused]] std::unique_ptr< Regions > &regions, [[maybe_unused]] const image::Image< unsigned char > *mask=nullptr)
Detect regions on the float image and compute their attributes (description)
Definition: ImageDescriber.hpp:239
aliceVision::feature::ImageDescriber::setConfigurationPreset
virtual void setConfigurationPreset(ConfigurationPreset preset)=0
Use a preset to control the number of detected regions.