Ocean
SimilarityTracker.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_TRACKING_POINT_SIMILARITY_TRACKER_H
9 #define META_OCEAN_TRACKING_POINT_SIMILARITY_TRACKER_H
10 
12 
13 #include "ocean/base/Frame.h"
15 #include "ocean/base/Worker.h"
16 
17 #include "ocean/cv/FramePyramid.h"
19 
21 #include "ocean/math/Vector2.h"
22 
23 namespace Ocean
24 {
25 
26 namespace Tracking
27 {
28 
29 namespace Point
30 {
31 
32 /**
33  * This class implements a tracker determining a similarity transformation between two successive video frames.
34  * A similarity transformation has four degrees of freedom and contains a rotation, a scale, and a 2D translation (in x- and y- direction) within the image domain.<br>
35  * The 3x3 matrix representing the similarity transformation has the following layout:
36  * <pre>
37  * | a -b tx |
38  * | b a ty |
39  * | 0 0 1 |
40  * </pre>
41  * @ingroup trackingpoint
42  */
43 class OCEAN_TRACKING_POINT_EXPORT SimilarityTracker
44 {
45  public:
46 
47  /**
48  * Definition of individual confidence values.
49  */
51  {
52  /// No similarity could be determined.
54  /// The determined similarity may have a bad quality.
56  /// The determined similarity has a moderate quality.
58  /// The determined similarity has a good quality.
60  /// The determined similarity has a very good quality and can be trusted regardless.
61  TC_VERY_GOOD
62  };
63 
64  /**
65  * Definition of individual textureness qualities.
66  */
68  {
69  /// The textureness is unknown.
71  /// The textureness is low (an almost homogeneous region).
73  /// The textureness is moderate.
75  /// The textureness is high (an almost heterogeneous region).
76  RT_HIGH
77  };
78 
79  public:
80 
81  /**
82  * Creates a new tracker object.
83  */
84  SimilarityTracker() = default;
85 
86  /**
87  * Determines the similarity between two successive video frame within a specified sub-region.
88  * @param yFrame The 8bit grayscale frame for which the similarity (in relation to the previous frame) will be determined, with image dimension [40, infinity)x[40, infinity) must be valid
89  * @param previousSubRegion The sub-region (in the previous frame) in which feature points will be determined and tracked to from the previous frame to the current frame, must be valid, must not lie outside the frame
90  * @param currentTprevious Optional resulting 3x3 matrix containing the entire similarity transformation: currentPoint = similarity * previousPoint
91  * @param translation Optional resulting translation between the current and the previous video frame, in pixel, with range (-infinity, infinity)x(-infinity, infinity)
92  * @param rotation Optional resulting rotation between the current and the previous video frame, in radian, with range [-PI, PI]
93  * @param scale Optional resulting scale between the current and the previous video frame, with range (0, infinity)
94  * @param predictedTranslation The predicted translation between the previous frame and the current frame if known, may be based on e.g., a gyro motion
95  * @param trackerConfidence Optional resulting confidence value of the tracker
96  * @param regionTextureness Optional resulting textureness of the provided sub-region
97  * @param worker Optional worker object to distribute the computation
98  * @return True, if succeeded, use `trackerConfidence` to check whether the resulting tracking data is valid
99  */
100  bool determineSimilarity(const Frame& yFrame, const CV::PixelBoundingBox& previousSubRegion, SquareMatrix3* currentTprevious = nullptr, Vector2* translation = nullptr, Scalar* rotation = nullptr, Scalar* scale = nullptr, const Vector2& predictedTranslation = Vector2(0, 0), TrackerConfidence* trackerConfidence = nullptr, RegionTextureness* regionTextureness = nullptr, Worker* worker = nullptr);
101 
102  /**
103  * Returns the frame pyramid of the most recent frame.
104  * @return Frame pyramid of current frame, if any
105  */
106  inline const CV::FramePyramid& currentFramePyramid() const;
107 
108  /**
109  * Returns the frame pyramid of one of the previous frames (the current key-frame).
110  * @return Frame pyramid of previous frame, if any
111  */
112  inline const CV::FramePyramid& keyFramePyramid() const;
113 
114  /**
115  * Resets the similarity tracker.
116  */
117  inline void reset();
118 
119  /**
120  * Calculate an overall confidence values based on a tracker confidence and a region textureness.
121  * @param trackerConfidence The confidence value of the tracker
122  * @param regionTextureness The textureness of the sub-region in which the tracker was applied
123  * @return The resulting overall confidence value, with range [0, 1]
124  */
125  static inline float combinedConfidence(const TrackerConfidence trackerConfidence, const RegionTextureness regionTextureness);
126 
127  protected:
128 
129  /**
130  * Detects feature points in given frame for which a frame pyramid exists.
131  * The points may be determined in a lower image resolution if enough features can be found.
132  * @param yFramePyramid The frame pyramid of the frame in which the feature points will be determined, with pixel format FORMAT_Y8, must be valid with at least one layer
133  * @param subRegion The sub-region inside the frame in which feature points will be determined, specified in the domain of the finest image resolution, must be valid
134  * @param minimalFeaturePoints The minimal number of feature points that must be detected so that the function succeeds, with range [1, infinity)
135  * @param desiredFeaturePoints The desired number of feature points that should be detected in an ideal case, with range [minimalFeaturePoints, infinity)
136  * @param maximalFeaturePoints The maximal number of feature points that will be detected, with range [desiredFeaturePoints, infinity)
137  * @param featurePoints The resulting detected feature points, defined in the domain of the pyramid layer as returned by 'usedLayerIndex'
138  * @param usedLayerIndex The pyramid layer in which the resulting feature points have been detected, with range [0, framePyramid.layers() - 1]
139  * @param regionTextureness The resulting textureness of the sub-region (which is based on the numbers of found feature etc.)
140  * @param worker Optional worker object to distribute the computation
141  * @return True, if at least 'minimalFeaturePoints' could be determined
142  */
143  static bool determineFeaturePoints(const CV::FramePyramid& yFramePyramid, const CV::PixelBoundingBox& subRegion, const size_t minimalFeaturePoints, const size_t desiredFeaturePoints, const size_t maximalFeaturePoints, Vectors2& featurePoints, unsigned int &usedLayerIndex, RegionTextureness& regionTextureness, Worker* worker = nullptr);
144 
145  /**
146  * Determines the similarity transformation between two successive frames.
147  * @param yPreviousFramePyramid The frame pyramid of the previous frame in which the given 'previousPoints' are located, with pixel format FORMAT_Y8, must be valid with at least one layer
148  * @param yCurrentFramePyramid The frame pyramid of the current frame to which the previous points will be tracked, must have the same format and layout as 'yPreviousFramePyramid'
149  * @param previousPoints The previous feature points defined within the domain of the finest pyramid layer of 'yPreviousFramePyramid', at least one
150  * @param roughCurrentPoints Optional the already known rough locations of the previous points with the current frame, one for each previous point, otherwise an empty vector
151  * @param randomGenerator The object to be used to generate random numbers
152  * @param currentTprevious The resulting similarity transformation matching with the resolution of f the finest pyramid layer, transforming points defined in the previous frame to points in the current frame: (currentPoint = currentTprevious * previousPoint)
153  * @param validCorrespondences The resulting indices of all previous feature points that have been used for determination of the similarity transformation
154  * @param coarsestLayerRadius The search radius on the coarsest layer in pixel, with range [2, infinity)
155  * @param subPixelIterations Number of sub-pixel iterations that will be applied, each iteration doubles the sub-pixel accuracy, with range [1, infinity)
156  * @param worker Optional worker object to distribute the computation
157  * @return True, if succeeded
158  */
159  static bool determineSimilarityTransformation(const CV::FramePyramid& yPreviousFramePyramid, const CV::FramePyramid& yCurrentFramePyramid, const Vectors2& previousPoints, const Vectors2& roughCurrentPoints, RandomGenerator& randomGenerator, SquareMatrix3& currentTprevious, Indices32& validCorrespondences, const unsigned int coarsestLayerRadius, const unsigned int subPixelIterations, Worker* worker = nullptr);
160 
161  protected:
162 
163  /// The frame pyramid of the key frame, one of the previous frames.
165 
166  /// The frame pyramid of the current frame.
168 
169  /// The random generator object.
171 
172  /// The similarity since 'previousFramePyramid_' have been updated the last time, defined in the resolution of the usage pyramid layer.
173  SquareMatrix3 previous_T_key_ = SquareMatrix3(true);
174 
175  /// The image points located in 'previousFramePyramid_', defined in the usage pyramid layer.
177 
178  /// The frame pyramid layer index in which 'keyFramePoints_' have been determined, -1 if invalid.
179  unsigned int keyFramePointsLayerIndex_ = (unsigned int)(-1);
180 };
181 
183 {
184  return currentFramePyramid_;
185 }
186 
188 {
189  return keyFramePyramid_;
190 }
191 
193 {
196 
198  keyFramePoints_.clear();
199  keyFramePointsLayerIndex_ = (unsigned int)(-1);
200 }
201 
202 inline float SimilarityTracker::combinedConfidence(const TrackerConfidence trackerConfidence, const RegionTextureness regionTextureness)
203 {
204  if (trackerConfidence == TC_VERY_GOOD)
205  {
206  return 1.0f;
207  }
208 
209  ocean_assert((unsigned int)(trackerConfidence) < 4u);
210  ocean_assert((unsigned int)(regionTextureness) < 4u);
211 
212  constexpr float confidenceValues[] =
213  {
214  0.0f, // TC_NONE, RT_UNKNOWN
215  0.1f, // TC_BAD, RT_LOW
216  0.5f, // TC_MODERATE, RT_MODERATE
217  1.0f, // TC_GOOD, RT_HIGH
218  };
219 
220  return confidenceValues[(unsigned int)(trackerConfidence)] * confidenceValues[(unsigned int)(regionTextureness)];
221 }
222 
223 }
224 
225 }
226 
227 }
228 
229 #endif // META_OCEAN_TRACKING_POINT_SIMILARITY_TRACKER_H
This class implements a frame pyramid.
Definition: FramePyramid.h:37
void clear()
Releases the internal frame layers.
Definition: FramePyramid.h:845
This class implements Ocean's image class.
Definition: Frame.h:1792
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
void toIdentity()
Sets the matrix to the identity matrix.
Definition: SquareMatrix3.h:1297
This class implements a tracker determining a similarity transformation between two successive video ...
Definition: SimilarityTracker.h:44
SimilarityTracker()=default
Creates a new tracker object.
bool determineSimilarity(const Frame &yFrame, const CV::PixelBoundingBox &previousSubRegion, SquareMatrix3 *currentTprevious=nullptr, Vector2 *translation=nullptr, Scalar *rotation=nullptr, Scalar *scale=nullptr, const Vector2 &predictedTranslation=Vector2(0, 0), TrackerConfidence *trackerConfidence=nullptr, RegionTextureness *regionTextureness=nullptr, Worker *worker=nullptr)
Determines the similarity between two successive video frame within a specified sub-region.
static bool determineFeaturePoints(const CV::FramePyramid &yFramePyramid, const CV::PixelBoundingBox &subRegion, const size_t minimalFeaturePoints, const size_t desiredFeaturePoints, const size_t maximalFeaturePoints, Vectors2 &featurePoints, unsigned int &usedLayerIndex, RegionTextureness &regionTextureness, Worker *worker=nullptr)
Detects feature points in given frame for which a frame pyramid exists.
unsigned int keyFramePointsLayerIndex_
The frame pyramid layer index in which 'keyFramePoints_' have been determined, -1 if invalid.
Definition: SimilarityTracker.h:179
static bool determineSimilarityTransformation(const CV::FramePyramid &yPreviousFramePyramid, const CV::FramePyramid &yCurrentFramePyramid, const Vectors2 &previousPoints, const Vectors2 &roughCurrentPoints, RandomGenerator &randomGenerator, SquareMatrix3 &currentTprevious, Indices32 &validCorrespondences, const unsigned int coarsestLayerRadius, const unsigned int subPixelIterations, Worker *worker=nullptr)
Determines the similarity transformation between two successive frames.
SquareMatrix3 previous_T_key_
The similarity since 'previousFramePyramid_' have been updated the last time, defined in the resoluti...
Definition: SimilarityTracker.h:173
RegionTextureness
Definition of individual textureness qualities.
Definition: SimilarityTracker.h:68
@ RT_MODERATE
The textureness is moderate.
Definition: SimilarityTracker.h:74
@ RT_LOW
The textureness is low (an almost homogeneous region).
Definition: SimilarityTracker.h:72
@ RT_UNKNOWN
The textureness is unknown.
Definition: SimilarityTracker.h:70
static float combinedConfidence(const TrackerConfidence trackerConfidence, const RegionTextureness regionTextureness)
Calculate an overall confidence values based on a tracker confidence and a region textureness.
Definition: SimilarityTracker.h:202
const CV::FramePyramid & keyFramePyramid() const
Returns the frame pyramid of one of the previous frames (the current key-frame).
Definition: SimilarityTracker.h:187
Vectors2 keyFramePoints_
The image points located in 'previousFramePyramid_', defined in the usage pyramid layer.
Definition: SimilarityTracker.h:176
const CV::FramePyramid & currentFramePyramid() const
Returns the frame pyramid of the most recent frame.
Definition: SimilarityTracker.h:182
TrackerConfidence
Definition of individual confidence values.
Definition: SimilarityTracker.h:51
@ TC_MODERATE
The determined similarity has a moderate quality.
Definition: SimilarityTracker.h:57
@ TC_GOOD
The determined similarity has a good quality.
Definition: SimilarityTracker.h:59
@ TC_BAD
The determined similarity may have a bad quality.
Definition: SimilarityTracker.h:55
@ TC_VERY_GOOD
The determined similarity has a very good quality and can be trusted regardless.
Definition: SimilarityTracker.h:61
@ TC_NONE
No similarity could be determined.
Definition: SimilarityTracker.h:53
CV::FramePyramid currentFramePyramid_
The frame pyramid of the current frame.
Definition: SimilarityTracker.h:167
void reset()
Resets the similarity tracker.
Definition: SimilarityTracker.h:192
RandomGenerator randomGenerator_
The random generator object.
Definition: SimilarityTracker.h:170
CV::FramePyramid keyFramePyramid_
The frame pyramid of the key frame, one of the previous frames.
Definition: SimilarityTracker.h:164
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition: Base.h:96
SquareMatrixT3< Scalar > SquareMatrix3
Definition of the SquareMatrix3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with ...
Definition: SquareMatrix3.h:35
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
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition: Vector2.h:21
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15