Ocean
FASTFeatureDetector.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_FAST_FEATURE_DETECTOR_H
9 #define META_OCEAN_CV_DETECTOR_FAST_FEATURE_DETECTOR_H
10 
13 
14 #include "ocean/base/Frame.h"
15 #include "ocean/base/Worker.h"
16 
18 
19 namespace Ocean
20 {
21 
22 namespace CV
23 {
24 
25 namespace Detector
26 {
27 
28 /**
29  * This class implements a FAST feature detector.
30  * @ingroup cvdetector
31  */
32 class OCEAN_CV_DETECTOR_EXPORT FASTFeatureDetector
33 {
34  public:
35 
36  /**
37  * Definition of a boolean enum for frame un-/distortion properties (to improve code readability).
38  */
39  enum FrameDistortion : bool
40  {
41  /// The provided frame is distorted so that all resulting feature locations are distorted.
42  FD_FRAME_IS_DISTORTED = false,
43  /// The provided frame is undistorted so that all resulting feature locations are also undistorted.
44  FD_FRAME_IS_UNDISTORTED = true,
45  };
46 
47  /**
48  * Definition of a boolean enum for scoring methods (to improve code readability).
49  */
50  enum ScoringMethod : bool
51  {
52  /// The feature's strength will be based on an approximated scoring.
53  SM_APPROXIMATED = false,
54  /// The feature's strength will be based on a precise scoring.
55  SM_PRECISE = true
56  };
57 
58  /**
59  * The following comfort class provides comfortable functions simplifying prototyping applications but also increasing binary size of the resulting applications.
60  * Best practice is to avoid using these functions if binary size matters,<br>
61  * as for every comfort function a corresponding function exists with specialized functionality not increasing binary size significantly.<br>
62  */
63  class OCEAN_CV_DETECTOR_EXPORT Comfort
64  {
65  public:
66 
67  /**
68  * Detects FAST features inside a given frame and can distribute the computation to several CPU cores.
69  * This function detects (standard) FAST features.
70  * @param frame The frame to detect FAST features in, will be converted internally if the pixel format and pixel origin matches the internal requirements
71  * @param threshold FAST intensity threshold
72  * @param frameIsUndistorted True, if the original input frame is undistorted and thus the 2D feature position will be undistorted too
73  * @param preciseScoring State defining whether an additional precise scoring is applied for each feature point
74  * @param features Resulting FAST features
75  * @param worker Optional worker object to distribute the computation
76  * @return True, if succeeded
77  */
78  static inline bool detectFeatures(const Frame& frame, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures& features, Worker* worker = nullptr);
79 
80  /**
81  * Detects FAST features inside a narrow area of a given frame and can distribute the computation to several CPU cores.
82  * This function detects (standard) FAST features.
83  * @param frame The frame to detect FAST features in, will be converted internally if the pixel format and pixel origin matches the internal requirements
84  * @param subRegionLeft Horizontal start position of the sub-region, in pixel, with range [0, width - 1]
85  * @param subRegionTop Vertical start position of the sub-region, in pixel, with range [0, height - 1]
86  * @param subRegionWidth Width of the sub-region, in pixel, with range [1, infinity)
87  * @param subRegionHeight Height of the sub-region, in pixel, with range [1, infinity)
88  * @param threshold The minimal intensity threshold for all detected features, with range [1, 255]
89  * @param frameIsUndistorted True, if the frame is undistorted and thus the 2D feature position will be undistorted too
90  * @param preciseScoring True to apply an additional precise scoring for each detected feature point; False, to use the approximated feature strength
91  * @param features The resulting FAST features
92  * @param worker Optional worker object to distribute the computation
93  * @return True, if succeeded
94  */
95  static bool detectFeatures(const Frame& frame, const unsigned int subRegionLeft, const unsigned int subRegionTop, const unsigned int subRegionWidth, const unsigned int subRegionHeight, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures& features, Worker* worker = nullptr);
96  };
97 
98  private:
99 
100  /**
101  * Definition of a maximum suppression object holding integer strength parameters.
102  */
104 
105  public:
106 
107  /**
108  * Detects FAST features inside a given frame.
109  * @param yFrame The 8-bit grayscale frame in which the FAST feature has been detected, must be valid
110  * @param width The width of the frame in pixel, with range [9, infinity)
111  * @param height The height of the frame in pixel, with range [9, infinity)
112  * @param threshold The minimal intensity threshold for all detected features, with range [1, 255]
113  * @param frameIsUndistorted True, if the frame is undistorted and thus the 2D feature position will be undistorted too
114  * @param preciseScoring True to apply an additional precise scoring for each detected feature point; False, to use the approximated feature strength
115  * @param features The resulting FAST features
116  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
117  * @param worker Optional worker object to distribute the computation
118  */
119  static inline void detectFeatures(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures& features, const unsigned int framePaddingElements, Worker* worker = nullptr);
120 
121  /**
122  * Detects FAST features inside a sub-region of a given frame.
123  * @param yFrame The 8-bit grayscale frame in which the FAST feature has been detected, must be valid
124  * @param width The width of the frame in pixel, with range [9, infinity)
125  * @param height The height of the frame in pixel, with range [9, infinity)
126  * @param subRegionLeft Horizontal start position of the sub-region, in pixel, with range [0, width - 1]
127  * @param subRegionTop Vertical start position of the sub-region, in pixel, with range [0, height - 1]
128  * @param subRegionWidth Width of the sub-region, in pixel, with range [1, infinity)
129  * @param subRegionHeight Height of the sub-region, in pixel, with range [1, infinity)
130  * @param threshold The minimal intensity threshold for all detected features, with range [1, 255]
131  * @param frameIsUndistorted True, if the frame is undistorted and thus the 2D feature position will be undistorted too
132  * @param preciseScoring True to apply an additional precise scoring for each detected feature point; False, to use the approximated feature strength
133  * @param features The resulting FAST features
134  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
135  * @param worker Optional worker object to distribute the computation
136  */
137  static void detectFeatures(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int subRegionLeft, const unsigned int subRegionTop, const unsigned int subRegionWidth, const unsigned int subRegionHeight, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures& features, const unsigned int framePaddingElements, Worker* worker = nullptr);
138 
139  private:
140 
141  /**
142  * Detects candidates of FAST features inside a sub region of a given frame.
143  * @param yFrame The 8-bit grayscale frame in which the FAST feature has been detected, must be valid
144  * @param width The width of the frame in pixel, with range [7, infinity)
145  * @param height The height of the frame in pixel, with range [7, infinity)
146  * @param threshold Detection threshold defining the lower boundary of the binary search
147  * @param nonMaximumSuppression Non maximum suppression buffer holding all votes stronger than the specified threshold
148  * @param firstColumn First column to be handled
149  * @param numberColumns Number of columns to be handled
150  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
151  * @param firstRow The first row to be handled, with range [0, height - 1]
152  * @param numberRows The number of rows to be handled, with range [1, height - firstRow]
153  */
154  static void detectFeatureCandidatesSubset(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, NonMaximumSuppressionVote* nonMaximumSuppression, const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int framePaddingElements, const unsigned int firstRow, const unsigned int numberRows);
155 
156  /**
157  * Applies a more precise feature scoring using a binary threshold search.
158  * This function may use a worker object to distribute the computational load if preferred.<br>
159  * @param yFrame The 8-bit grayscale frame in which the FAST feature has been detected, must be valid
160  * @param width The width of the frame in pixel, with range [7, infinity)
161  * @param height The height of the frame in pixel, with range [7, infinity)
162  * @param threshold Detection threshold defining the lower boundary of the binary search
163  * @param features Already detected features receiving the more precise score
164  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
165  * @param worker Optional worker object to be used for computation distribution
166  */
167  static void scoreFeaturesPrecise(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, FASTFeatures& features, const unsigned int framePaddingElements, Worker* worker = nullptr);
168 
169  /**
170  * Applies a more precise feature scoring using a binary threshold search to a subset of the given features.
171  * @param yFrame The 8-bit grayscale frame in which the FAST feature has been detected, must be valid
172  * @param width The width of the frame in pixel, with range [7, infinity)
173  * @param height The height of the frame in pixel, with range [7, infinity)
174  * @param threshold Detection threshold defining the lower boundary of the binary search
175  * @param features Already detected features receiving the more precise score
176  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
177  * @param firstFeature First feature to be handled, with range [0, features->size() - 1]
178  * @param numberFeatures Number of features to be handled, with range [1, features->size() - firstFeature]
179  */
180  static void scoreFeaturesPreciseSubset(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, FASTFeatures* features, const unsigned int framePaddingElements, const unsigned int firstFeature, const unsigned int numberFeatures);
181 
182  /**
183  * Applies a more precise feature scoring using a binary threshold search for one feature.
184  * @param yFrame The grayscale frame in which the FAST feature has been detected, must be valid
185  * @param width The width of the frame in pixel, with range [7, infinity)
186  * @param height The height of the frame in pixel, with range [7, infinity)
187  * @param threshold Detection threshold defining the lower boundary of the binary search
188  * @param feature The already detected feature for which the more precise score will be determined (and updated)
189  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
190  */
191  static void scoreFeaturePrecise(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, FASTFeature& feature, const unsigned int framePaddingElements);
192 };
193 
194 inline bool FASTFeatureDetector::Comfort::detectFeatures(const Frame& frame, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures& features, Worker* worker)
195 {
196  constexpr unsigned int subRegionLeft = 0u;
197  constexpr unsigned int subRegionTop = 0u;
198 
199  return detectFeatures(frame, subRegionLeft, subRegionTop, frame.width(), frame.height(), threshold, frameIsUndistorted, preciseScoring, features, worker);
200 }
201 
202 inline void FASTFeatureDetector::detectFeatures(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures& features, const unsigned int framePaddingElements, Worker* worker)
203 {
204  constexpr unsigned int subRegionLeft = 0u;
205  constexpr unsigned int subRegionTop = 0u;
206 
207  detectFeatures(yFrame, width, height, subRegionLeft, subRegionTop, width, height, threshold, frameIsUndistorted, preciseScoring, features, framePaddingElements, worker);
208 }
209 
210 }
211 
212 }
213 
214 }
215 
216 #endif // META_OCEAN_CV_DETECTOR_FAST_FEATURE_DETECTOR_H
The following comfort class provides comfortable functions simplifying prototyping applications but a...
Definition: FASTFeatureDetector.h:64
static bool detectFeatures(const Frame &frame, const unsigned int subRegionLeft, const unsigned int subRegionTop, const unsigned int subRegionWidth, const unsigned int subRegionHeight, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures &features, Worker *worker=nullptr)
Detects FAST features inside a narrow area of a given frame and can distribute the computation to sev...
static bool detectFeatures(const Frame &frame, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures &features, Worker *worker=nullptr)
Detects FAST features inside a given frame and can distribute the computation to several CPU cores.
Definition: FASTFeatureDetector.h:194
This class implements a FAST feature detector.
Definition: FASTFeatureDetector.h:33
static void scoreFeaturesPreciseSubset(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, FASTFeatures *features, const unsigned int framePaddingElements, const unsigned int firstFeature, const unsigned int numberFeatures)
Applies a more precise feature scoring using a binary threshold search to a subset of the given featu...
static void scoreFeaturesPrecise(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, FASTFeatures &features, const unsigned int framePaddingElements, Worker *worker=nullptr)
Applies a more precise feature scoring using a binary threshold search.
static void detectFeatures(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures &features, const unsigned int framePaddingElements, Worker *worker=nullptr)
Detects FAST features inside a given frame.
Definition: FASTFeatureDetector.h:202
static void detectFeatureCandidatesSubset(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, NonMaximumSuppressionVote *nonMaximumSuppression, const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int framePaddingElements, const unsigned int firstRow, const unsigned int numberRows)
Detects candidates of FAST features inside a sub region of a given frame.
static void detectFeatures(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int subRegionLeft, const unsigned int subRegionTop, const unsigned int subRegionWidth, const unsigned int subRegionHeight, const unsigned int threshold, const bool frameIsUndistorted, const bool preciseScoring, FASTFeatures &features, const unsigned int framePaddingElements, Worker *worker=nullptr)
Detects FAST features inside a sub-region of a given frame.
ScoringMethod
Definition of a boolean enum for scoring methods (to improve code readability).
Definition: FASTFeatureDetector.h:51
FrameDistortion
Definition of a boolean enum for frame un-/distortion properties (to improve code readability).
Definition: FASTFeatureDetector.h:40
NonMaximumSuppression< uint32_t > NonMaximumSuppressionVote
Definition of a maximum suppression object holding integer strength parameters.
Definition: FASTFeatureDetector.h:103
static void scoreFeaturePrecise(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int threshold, FASTFeature &feature, const unsigned int framePaddingElements)
Applies a more precise feature scoring using a binary threshold search for one feature.
This class implements a FAST feature.
Definition: FASTFeature.h:37
This class implements the possibility to find local maximum in a 2D array by applying a non-maximum-s...
Definition: NonMaximumSuppression.h:41
This class implements Ocean's image class.
Definition: Frame.h:1792
unsigned int width() const
Returns the width of the frame format in pixel.
Definition: Frame.h:3143
unsigned int height() const
Returns the height of the frame in pixel.
Definition: Frame.h:3148
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
std::vector< FASTFeature > FASTFeatures
Definition of a vector holding FAST features.
Definition: FASTFeature.h:24
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15