Ocean
FeatureDetector.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_FEATURE_DETECTOR_H
9 #define META_OCEAN_CV_DETECTOR_FEATURE_DETECTOR_H
10 
12 
13 #include "ocean/base/Frame.h"
14 #include "ocean/base/Worker.h"
15 
17 #include "ocean/cv/SubRegion.h"
18 
19 #include "ocean/math/Vector2.h"
20 
21 namespace Ocean
22 {
23 
24 namespace CV
25 {
26 
27 namespace Detector
28 {
29 
30 /**
31  * This class provides an abstraction for visual features and strong feature points.
32  * @ingroup cvdetector
33  */
34 class OCEAN_CV_DETECTOR_EXPORT FeatureDetector
35 {
36  public:
37 
38  /**
39  * The following comfort class provides comfortable functions simplifying prototyping applications but also increasing binary size of the resulting applications.
40  * Best practice is to avoid using these functions if binary size matters,<br>
41  * as for every comfort function a corresponding function exists with specialized functionality not increasing binary size significantly.<br>
42  */
43  class OCEAN_CV_EXPORT Comfort
44  {
45  public:
46 
47  /**
48  * Determines strong feature points in a given image, optional a sub-region can be specified in that the points are detected.
49  * @param frame The frame in which the feature points are detected, will be converted to a frame with pixel format FORMAT_Y8 internally, must be valid
50  * @param subRegion Optional sub-region specifying a small image area in that the points are detected, an invalid sub-region to use the entire frame
51  * @param horizontalBins Optional horizontal bins that can be used to distribute the tracked points into array bins (in each bin there will be at most one point)
52  * @param verticalBins Optional vertical bins that can be used to distribute the tracked points into array bins (in each bin there will be at most one point)
53  * @param strength Minimal aiming strength parameter of the tracked feature points, this value will be weakened if too less feature points can be detected
54  * @param worker Optional worker object to distribute the computation
55  * @param strengths Optional resulting strength values individual for each point
56  * @return The resulting feature points, feature points with high strength value first
57  */
58  static Vectors2 determineHarrisPoints(const Frame& frame, const SubRegion& subRegion = SubRegion(), const unsigned int horizontalBins = 0u, const unsigned int verticalBins = 0u, const unsigned int strength = 30u, Worker* worker = nullptr, std::vector<int>* strengths = nullptr);
59  };
60 
61  protected:
62 
63  /**
64  * This class implements a vector extension holding an additional intensity parameter.
65  */
66  class IntensityVector2 : public Vector2
67  {
68  public:
69 
70  /**
71  * Creates a new intensity vector object with undefined vector elements.
72  * Beware: The elements are neither zero nor a specific value!
73  * @see VectorT2::VectorT2()
74  */
75  IntensityVector2() = default;
76 
77  /**
78  * Creates a new intensity vector by the given position and intensity value.
79  * @param position Vector position
80  * @param intensity Vector intensity
81  */
82  inline IntensityVector2(const Vector2& position, const int intensity);
83 
84  /**
85  * Returns the intensity value of this object.
86  * @return Intensity value
87  */
88  inline int intensity() const;
89 
90  /**
91  * Compares two intensity objects.
92  * @param object Second object to compare
93  * @return True, if the intensity of the left object is higher than the intensity of the right object
94  */
95  inline bool operator<(const IntensityVector2& object) const;
96 
97  private:
98 
99  /// Intensity value.
100  int intensity_ = NumericT<int>::minValue();
101  };
102 
103  /**
104  * Definition of a vector holding intensity vector objects.
105  */
106  using IntensityVectors2 = std::vector<IntensityVector2>;
107 
108  public:
109 
110  /**
111  * Determines the points in an 8bit grayscale image with highest Harris corner response votes.
112  * @param yFrame The 8bit grayscale frame in which the Harris corner responses have to be determined, must be valid
113  * @param width The width of the frame in pixel, with range [5, infinity)
114  * @param height The height of the frame in pixel, with range [5, infinity)
115  * @param yFramePaddingElements The number of padding elements at the end of each row of yFrame, in elements, with range [0, infinity)
116  * @param positions Given 2D positions inside the frame that have to be ordered according their response intensities
117  * @param maximalPoints Number of maximal points that have to be returned
118  * @param minSqrDistance Minimal squared distance between the points to be accepted
119  * @param harrisThreshold Minimal threshold for all Harris responses (points below this threshold will be skipped)
120  * @param worker Optional worker object to distribute the computation
121  * @return The resulting feature points
122  */
123  static Vectors2 filterStrongHarrisPoints(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int yFramePaddingElements, const Vectors2& positions, const unsigned int maximalPoints, const Scalar minSqrDistance, const int harrisThreshold = 0, Worker* worker = nullptr);
124 
125  /**
126  * Determines strong feature points in a given image, optional a sub-region can be specified in that the points are detected.
127  * @param yFrame The grayscale frame in which the feature points are detected, must have pixel format FORMAT_Y8, must be valid
128  * @param subRegion Optional sub-region specifying a small image area in that the points are detected, an invalid sub-region to use the entire frame
129  * @param horizontalBins Optional horizontal bins that can be used to distribute the tracked points into array bins (in each bin there will be at most one point)
130  * @param verticalBins Optional vertical bins that can be used to distribute the tracked points into array bins (in each bin there will be at most one point)
131  * @param strength Minimal aiming strength parameter of the tracked feature points, this value will be weakened if too less feature points can be detected
132  * @param worker Optional worker object to distribute the computation
133  * @param strengths Optional resulting strength values individual for each point
134  * @return The resulting feature points, feature points with high strength value first
135  */
136  static inline Vectors2 determineHarrisPoints(const Frame& yFrame, const SubRegion& subRegion = SubRegion(), const unsigned int horizontalBins = 0u, const unsigned int verticalBins = 0u, const unsigned int strength = 30u, Worker* worker = nullptr, std::vector<int>* strengths = nullptr);
137 
138  /**
139  * Determines strong feature points in a given image, optional a sub-region can be specified in that the points are detected.
140  * @param yFrame The 8 bit grayscale frame (Y8 pixel format) in which the feature points are detected, must be valid
141  * @param width The width of the given frame in pixel, with range [7, infinity)
142  * @param height The height of the given frame in pixel, with range [7, infinity)
143  * @param yFramePaddingElements The number of padding elements at the end of each row of yFrame, in elements, with range [0, infinity)
144  * @param subRegion Optional sub-region specifying a small image area in that the points are detected
145  * @param horizontalBins Optional horizontal bins that can be used to distribute the tracked points into array bins (in each bin there will be at most one point)
146  * @param verticalBins Optional vertical bins that can be used to distribute the tracked points into array bins (in each bin there will be at most one point)
147  * @param strength Minimal aiming strength parameter of the tracked feature points, this value will be weakened if too less feature points can be detected
148  * @param worker Optional worker object to distribute the computation
149  * @param strengths Optional resulting strength values individual for each point
150  * @return The resulting feature points, feature points with high strength value first
151  */
152  static Vectors2 determineHarrisPoints(const uint8_t* yFrame, const unsigned int width, const unsigned int height, const unsigned int yFramePaddingElements, const SubRegion& subRegion, const unsigned int horizontalBins = 0u, const unsigned int verticalBins = 0u, const unsigned int strength = 30u, Worker* worker = nullptr, std::vector<int>* strengths = nullptr);
153 };
154 
155 inline FeatureDetector::IntensityVector2::IntensityVector2(const Vector2& position, const int intensity) :
156  Vector2(position),
157  intensity_(intensity)
158 {
159  // nothing to do here
160 }
161 
163 {
164  return intensity_;
165 }
166 
168 {
169  return intensity_ > object.intensity_;
170 }
171 
172 inline Vectors2 FeatureDetector::determineHarrisPoints(const Frame& yFrame, const SubRegion& subRegion, const unsigned int horizontalBins, const unsigned int verticalBins, const unsigned int strength, Worker* worker, std::vector<int>* strengths)
173 {
174  ocean_assert(yFrame.isValid());
175  ocean_assert(yFrame.isPixelFormatCompatible(FrameType::FORMAT_Y8));
176 
177  if (!yFrame.isValid() || !yFrame.isPixelFormatCompatible(FrameType::FORMAT_Y8))
178  {
179  return Vectors2();
180  }
181 
182  return determineHarrisPoints(yFrame.constdata<uint8_t>(), yFrame.width(), yFrame.height(), yFrame.paddingElements(), subRegion, horizontalBins, verticalBins, strength, worker, strengths);
183 }
184 
185 }
186 
187 }
188 
189 }
190 
191 #endif // META_OCEAN_CV_DETECTOR_FEATURE_DETECTOR_H
The following comfort class provides comfortable functions simplifying prototyping applications but a...
Definition: FeatureDetector.h:44
static Vectors2 determineHarrisPoints(const Frame &frame, const SubRegion &subRegion=SubRegion(), const unsigned int horizontalBins=0u, const unsigned int verticalBins=0u, const unsigned int strength=30u, Worker *worker=nullptr, std::vector< int > *strengths=nullptr)
Determines strong feature points in a given image, optional a sub-region can be specified in that the...
This class implements a vector extension holding an additional intensity parameter.
Definition: FeatureDetector.h:67
bool operator<(const IntensityVector2 &object) const
Compares two intensity objects.
Definition: FeatureDetector.h:167
int intensity() const
Returns the intensity value of this object.
Definition: FeatureDetector.h:162
IntensityVector2()=default
Creates a new intensity vector object with undefined vector elements.
This class provides an abstraction for visual features and strong feature points.
Definition: FeatureDetector.h:35
std::vector< IntensityVector2 > IntensityVectors2
Definition of a vector holding intensity vector objects.
Definition: FeatureDetector.h:106
static Vectors2 determineHarrisPoints(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int yFramePaddingElements, const SubRegion &subRegion, const unsigned int horizontalBins=0u, const unsigned int verticalBins=0u, const unsigned int strength=30u, Worker *worker=nullptr, std::vector< int > *strengths=nullptr)
Determines strong feature points in a given image, optional a sub-region can be specified in that the...
static Vectors2 filterStrongHarrisPoints(const uint8_t *yFrame, const unsigned int width, const unsigned int height, const unsigned int yFramePaddingElements, const Vectors2 &positions, const unsigned int maximalPoints, const Scalar minSqrDistance, const int harrisThreshold=0, Worker *worker=nullptr)
Determines the points in an 8bit grayscale image with highest Harris corner response votes.
static Vectors2 determineHarrisPoints(const Frame &yFrame, const SubRegion &subRegion=SubRegion(), const unsigned int horizontalBins=0u, const unsigned int verticalBins=0u, const unsigned int strength=30u, Worker *worker=nullptr, std::vector< int > *strengths=nullptr)
Determines strong feature points in a given image, optional a sub-region can be specified in that the...
Definition: FeatureDetector.h:172
This class implement a sub-region either defined by 2D triangles or defined by a binary mask.
Definition: SubRegion.h:32
This class implements Ocean's image class.
Definition: Frame.h:1792
const T * constdata(const unsigned int planeIndex=0u) const
Returns a pointer to the read-only pixel data of a specific plane.
Definition: Frame.h:4168
bool isValid() const
Returns whether this frame is valid.
Definition: Frame.h:4448
unsigned int paddingElements(const unsigned int planeIndex=0u) const
Returns the optional number of padding elements at the end of each row for a specific plane.
Definition: Frame.h:4042
@ FORMAT_Y8
Pixel format for grayscale images with byte order Y and 8 bits per pixel.
Definition: Frame.h:594
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
bool isPixelFormatCompatible(const PixelFormat pixelFormat) const
Returns whether the pixel format of this frame type is compatible with a given pixel format.
Definition: Frame.h:3198
static constexpr T minValue()
Returns the min scalar value.
Definition: Numeric.h:3250
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition: Vector2.h:64
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15