Ocean
ORBFeature.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_CV_DETECTOR_ORB_FEATURE_H
9 #define META_OCEAN_CV_DETECTOR_ORB_FEATURE_H
10 
15 
16 namespace Ocean
17 {
18 
19 namespace CV
20 {
21 
22 namespace Detector
23 {
24 
25 // Forward declaration.
26 class ORBFeature;
27 
28 /**
29  * Definition of a vector holding ORB features.
30  * @ingroup cvdetector
31  */
32 typedef std::vector<ORBFeature> ORBFeatures;
33 
34 /**
35  * This class implements a ORB feature.
36  * A ORB feature can hold multiple descriptors.
37  * @ingroup cvdetector
38  */
39 class OCEAN_CV_DETECTOR_EXPORT ORBFeature : public OrientedPointFeature
40 {
41  public:
42 
43  /**
44  * Definition of the ORB descriptor type for this feature.
45  */
47  {
48  /// Undescribed descriptor.
50  /// Oriented descriptor.
51  FDT_ORIENTED
52  };
53 
54  public:
55 
56  /**
57  * Creates a new empty ORB feature object.
58  */
59  ORBFeature() = default;
60 
61  /**
62  * Creates a new ORB feature object by a given 2D observation position in e.g. an image.
63  * @param observation 2D feature observation
64  * @param distortionState Distortion state of the 2D feature position
65  * @param strength The strength of the feature
66  * @param orientation The orientation angle of the feature in radian, range [0, 2*PI)
67  */
68  inline ORBFeature(const Vector2& observation, const DistortionState distortionState = DS_UNKNOWN, const Scalar strength = Scalar(0), const Scalar orientation = Scalar(0));
69 
70  /**
71  * Returns the number of descriptors holding by this feature point.
72  * @return Number of descriptors
73  */
74  inline size_t numberDescriptors() const;
75 
76  /**
77  * Returns the first descriptor of this feature.
78  * Beware: Ensure that this object holds at least one descriptor before accessing it.
79  * @return First feature descriptor
80  */
81  inline const ORBDescriptor& firstDescriptor() const;
82 
83  /**
84  * Returns the first descriptor of this feature.
85  * Beware: Ensure that this object holds at least one descriptor before accessing it.
86  * @return First feature descriptor
87  */
88  inline ORBDescriptor& firstDescriptor();
89 
90  /**
91  * Returns a vector of all descriptors of this feature.
92  * Beware: Ensure to check the size of the vector before accessing elements; the vector can be empty
93  * @return All Feature descriptors
94  */
95  inline const ORBDescriptors& descriptors() const;
96 
97  /**
98  * Returns a vector of all descriptors of this feature.
99  * Beware: Ensure to check the size of the vector before accessing elements; the vector can be empty
100  * @return All Feature descriptors
101  */
102  inline ORBDescriptors& descriptors();
103 
104  /**
105  * Adds a given descriptor to this feature.
106  * @param descriptor The descriptor to add
107  */
108  inline void addDescriptor(const ORBDescriptor& descriptor);
109 
110  /**
111  * Returns the type of all descriptors of this feature.
112  * @return Descriptor type
113  */
114  inline FeatureDescriptorType descriptorType() const;
115 
116  /**
117  * Sets or changes the type of all descriptors of this feature.
118  * @param type Descriptor type to set
119  */
120  inline void setDescriptorType(const FeatureDescriptorType type);
121 
122  /**
123  * Converts a vector of point features to ORB feature points.
124  * @param features Point features to convert
125  * @return Converted ORB feature points
126  */
127  template <typename T>
128  static ORBFeatures features2ORBFeatures(const std::vector<T>& features);
129 
130  /**
131  * Converts a vector of point features to ORB feature points.
132  * Further, feature points too close to the image border will be skipped.
133  * @param features The point features to convert
134  * @param width The width of the frame in which the feature points have been detected, in pixel, with range [border, infinity)
135  * @param height The height of the frame in which the feature points have been detected, in pixel, with range [border, infinity)
136  * @param border The minimal distance between the image border and a feature points, with range [21 or 31, infinity), 21 for descriptors with 'useSublayers == false', 31 for descriptors with 'useSublayers == true'
137  * @param validIndices Optional resulting indices of features which were not skipped
138  * @return The resulting ORB feature points
139  */
140  template <typename T>
141  static ORBFeatures features2ORBFeatures(const std::vector<T>& features, const unsigned int width, const unsigned int height, const unsigned int border = 31u, Indices32* validIndices = nullptr);
142 
143  protected:
144 
145  /// Feature descriptor type.
146  FeatureDescriptorType descriptorType_ = FDT_UNDESCRIBED;
147 
148  /// Feature descriptor.
150 };
151 
152 inline ORBFeature::ORBFeature(const Vector2& observation, const DistortionState distortionState, const Scalar strength, const Scalar orientation) :
153  OrientedPointFeature(observation, distortionState, strength, orientation)
154 {
155  // nothing to do here
156 }
157 
158 inline size_t ORBFeature::numberDescriptors() const
159 {
160  return descriptors_.size();
161 }
162 
164 {
165  ocean_assert(!descriptors_.empty());
166  return descriptors_.front();
167 }
168 
170 {
171  ocean_assert(!descriptors_.empty());
172  return descriptors_.front();
173 }
174 
176 {
177  return descriptors_;
178 }
179 
181 {
182  return descriptors_;
183 }
184 
185 inline void ORBFeature::addDescriptor(const ORBDescriptor& descriptor)
186 {
187  descriptors_.pushBack(descriptor);
188 }
189 
191 {
192  return descriptorType_;
193 }
194 
196 {
197  descriptorType_ = type;
198 }
199 
200 template <typename T>
201 ORBFeatures ORBFeature::features2ORBFeatures(const std::vector<T>& features)
202 {
203  ORBFeatures result;
204  result.resize(features.size());
205 
206  for (const T& feature : features)
207  {
208  result.emplace_back(feature.observation(), feature.distortionState(), feature.strength());
209  }
210 
211  return result;
212 }
213 
214 template <typename T>
215 ORBFeatures ORBFeature::features2ORBFeatures(const std::vector<T>& features, const unsigned int width, const unsigned int height, const unsigned int border, Indices32* validIndices)
216 {
217  ORBFeatures result;
218  result.reserve(features.size());
219 
220  if (validIndices != nullptr)
221  {
222  validIndices->clear();
223  validIndices->reserve(features.size());
224  }
225 
226  const Scalar borderLeftTop = Scalar(border);
227  const Scalar borderRight = Scalar(width) - Scalar(border);
228  const Scalar borderBottom = Scalar(height) - Scalar(border);
229 
230  for (size_t i = 0; i < features.size(); i++)
231  {
232  const T& feature = features[i];
233 
234  const Scalar x = feature.observation().x();
235  const Scalar y = feature.observation().y();
236 
237  if (x >= borderLeftTop && y >= borderLeftTop && x < borderRight && y < borderBottom)
238  {
239  result.emplace_back(feature.observation(), feature.distortionState(), feature.strength());
240 
241  if (validIndices != nullptr)
242  {
243  validIndices->emplace_back(Index32(i));
244  }
245  }
246  }
247 
248  return result;
249 }
250 
251 }
252 
253 }
254 
255 }
256 
257 #endif // META_OCEAN_CV_DETECTOR_ORB_FEATURE_H
This class implement the descriptor for ORB features.
Definition: ORBDescriptor.h:41
This class implements a ORB feature.
Definition: ORBFeature.h:40
FeatureDescriptorType
Definition of the ORB descriptor type for this feature.
Definition: ORBFeature.h:47
@ FDT_UNDESCRIBED
Undescribed descriptor.
Definition: ORBFeature.h:49
void setDescriptorType(const FeatureDescriptorType type)
Sets or changes the type of all descriptors of this feature.
Definition: ORBFeature.h:195
static ORBFeatures features2ORBFeatures(const std::vector< T > &features)
Converts a vector of point features to ORB feature points.
Definition: ORBFeature.h:201
size_t numberDescriptors() const
Returns the number of descriptors holding by this feature point.
Definition: ORBFeature.h:158
void addDescriptor(const ORBDescriptor &descriptor)
Adds a given descriptor to this feature.
Definition: ORBFeature.h:185
FeatureDescriptorType descriptorType_
Feature descriptor type.
Definition: ORBFeature.h:146
ORBFeature()=default
Creates a new empty ORB feature object.
const ORBDescriptor & firstDescriptor() const
Returns the first descriptor of this feature.
Definition: ORBFeature.h:163
const ORBDescriptors & descriptors() const
Returns a vector of all descriptors of this feature.
Definition: ORBFeature.h:175
ORBDescriptors descriptors_
Feature descriptor.
Definition: ORBFeature.h:149
FeatureDescriptorType descriptorType() const
Returns the type of all descriptors of this feature.
Definition: ORBFeature.h:190
This class implements the base class for all computer vision features mainly basing on points width a...
Definition: OrientedPointFeature.h:39
DistortionState
Definition of individual distortion states.
Definition: PointFeature.h:51
const T & front() const
Returns the first elements of this vector.
Definition: StaticVector.h:455
size_t size() const
Returns the size of this vector.
Definition: StaticVector.h:340
bool empty() const
Returns whether this vector hold no element.
Definition: StaticVector.h:487
void pushBack(const T &value)
Adds a new element to this vector.
Definition: StaticVector.h:352
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition: Base.h:96
uint32_t Index32
Definition of a 32 bit index value.
Definition: Base.h:84
std::vector< ORBFeature > ORBFeatures
Definition of a vector holding ORB features.
Definition: ORBFeature.h:26
float Scalar
Definition of a scalar type.
Definition: Math.h:128
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15