Ocean
HomographyTracker.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_HOMOGRAPHY_TRACKER_H
9 #define META_OCEAN_TRACKING_HOMOGRAPHY_TRACKER_H
10 
12 
13 #include "ocean/base/Frame.h"
15 
16 #include "ocean/cv/FramePyramid.h"
17 #include "ocean/cv/PixelPosition.h"
18 
20 #include "ocean/math/Vector2.h"
21 
22 namespace Ocean
23 {
24 
25 namespace Tracking
26 {
27 
28 /**
29  * This class implements a homography tracker able to determine a homography in real-time.
30  * The tracker stores the two frame pyramids, one pyramid for the previous frame, one pyramid of the current frame.<br>
31  * For each new camera frame a group of reliable/strong feature points visible in the previous camera frame will be tracked to the new camera frame.<br>
32  * The point correspondences will be used to calculate the homography.<br>
33  * In contrast to HomographyImageAlignmentSparse the HomographyTracker uses explicitly provided image points for the determination of the homography while the HomographyImageAlignmentSparse selects suitable sparse information on its own.
34  * @see HomographyImageAlignmentSparse.
35  * @ingroup tracking
36  */
37 class OCEAN_TRACKING_EXPORT HomographyTracker
38 {
39  public:
40 
41  /**
42  * Creates a new homography tracker object and uses 31 as patch size.
43  */
44  inline HomographyTracker() = default;
45 
46  /**
47  * Creates a new homography tracker object and uses 31 as patch size.
48  * @param patchSize The size of the patches, possible values can be [5, 7, 15, 31].
49  */
50  explicit inline HomographyTracker(const unsigned int patchSize);
51 
52  /**
53  * Returns the size of the patches which are used for tracking.
54  * @return The patch size currently used, 31 by default
55  */
56  inline unsigned int patchSize() const;
57 
58  /**
59  * Sets or changes the size of the patches which are used for tracking.
60  * @param size The size of the patches, possible values can be [5, 7, 15, 31].
61  */
62  inline void setPatchSize(const unsigned int size);
63 
64  /**
65  * Tracks a group of given image points from the previous frame to the current frame and determines the corresponding homography afterwards.
66  * In the case this function is invoked for the first time, the current frame is stored for the next function call (as previous frame) and the identity homography is returned.<br>
67  * The resulting homography will transform points defined in the previous frame to points defined in the current frame (pointCurrent = H * pointPrevious).
68  * @param currentFrame The current frame with pixel format as desired, must be valid
69  * @param yPreviousFrame The previous frame as grayscale frame with pixel format FORMAT_Y8, with same frame dimension and pixel origin as the current frame, can be invalid if this function is invoked for the first time
70  * @param randomGenerator Random number generator object
71  * @param previousPositions Image points (e.g., string feature points) located in the previous image that will be used for the determination of the homography, may be empty if this function is invoked for the first time
72  * @param homography The resulting homography
73  * @param worker Optional worker object to distribute the computation
74  * @param frameBorder The size of the area around the frame's border defining a region from which image points will not be used for tracking, with range [0, min(width, height) / 4)
75  * @return True, if a valid homography could be determined
76  */
77  bool trackPoints(const Frame& currentFrame, const Frame& yPreviousFrame, RandomGenerator& randomGenerator, const Vectors2& previousPositions, SquareMatrix3& homography, Worker* worker = nullptr, const Scalar frameBorder = Scalar(10));
78 
79  /**
80  * Clears the previous pyramid frame of this tracking object.
81  */
82  inline void clear();
83 
84  /**
85  * Tracks a group of given image points from the previous frame to the current frame and determines the corresponding homography afterwards.
86  * The resulting homography will transform points defined in the previous frame to points defined in the current frame (pointCurrent = H * pointPrevious).
87  * @param yPreviousFrame The previous frame as grayscale frame, with same frame dimension and pixel origin as the previous/current frame, must have pixel format FORMAT_Y8, must be valid
88  * @param previousFramePyramid The frame pyramid of the previous frame, must be valid
89  * @param currentFramePyramid The frame pyramid of the current frame, with same frame type and layer number as 'previousFramePyramid'
90  * @param randomGenerator Random number generator object
91  * @param previousPositions Image points (e.g., string feature points) located in the previous image that will be used for the determination of the homography
92  * @param homography Resulting homography
93  * @param worker Optional worker object to distribute the computation
94  * @param patchSize The size of the patches used for tracking, possible values can be [5, 7, 15, 31]
95  * @return True, if succeeded
96  */
97  static bool trackPoints(const Frame& yPreviousFrame, const CV::FramePyramid& previousFramePyramid, const CV::FramePyramid& currentFramePyramid, RandomGenerator& randomGenerator, const Vectors2& previousPositions, SquareMatrix3& homography, Worker* worker = nullptr, const unsigned int patchSize = 31u);
98 
99  /**
100  * Transforms a given set of points to a new set using a given transformation.
101  * This function will calculate result[i] = transformation * points[i].
102  * @param points The points to be transformed
103  * @param transformation The transformation to be used, must be valid
104  * @return Resulting transformed points
105  */
106  static inline Vectors2 transformPoints(const Vectors2& points, const SquareMatrix3& transformation);
107 
108  private:
109 
110  /// Frame pyramid of the current frame.
112 
113  /// Frame pyramid of the previous frame.
115 
116  /// The size of the image patches used for tracking, possible values can be [5, 7, 15, 31].
117  unsigned int patchSize_ = 31u;
118 };
119 
120 HomographyTracker::HomographyTracker(const unsigned int patchSize) :
121  patchSize_(patchSize)
122 {
123  ocean_assert(patchSize == 5u || patchSize == 7u || patchSize == 15u || patchSize == 31u);
124 }
125 
126 inline unsigned int HomographyTracker::patchSize() const
127 {
128  return patchSize_;
129 }
130 
131 inline void HomographyTracker::setPatchSize(const unsigned int size)
132 {
133  ocean_assert(size == 5 || size == 7u || size == 15u || size == 31u);
134 
135  patchSize_ = size;
136 }
137 
139 {
141 }
142 
143 inline Vectors2 HomographyTracker::transformPoints(const Vectors2& points, const SquareMatrix3& transformation)
144 {
145  Vectors2 result;
146  result.reserve(points.size());
147 
148  for (Vectors2::const_iterator i = points.begin(); i != points.end(); ++i)
149  {
150  result.emplace_back(transformation * *i);
151  }
152 
153  return result;
154 }
155 
156 }
157 
158 }
159 
160 #endif // META_OCEAN_TRACKING_HOMOGRAPHY_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:1760
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
This class implements a homography tracker able to determine a homography in real-time.
Definition: HomographyTracker.h:38
unsigned int patchSize_
The size of the image patches used for tracking, possible values can be [5, 7, 15,...
Definition: HomographyTracker.h:117
CV::FramePyramid previousFramePyramid_
Frame pyramid of the previous frame.
Definition: HomographyTracker.h:114
bool trackPoints(const Frame &currentFrame, const Frame &yPreviousFrame, RandomGenerator &randomGenerator, const Vectors2 &previousPositions, SquareMatrix3 &homography, Worker *worker=nullptr, const Scalar frameBorder=Scalar(10))
Tracks a group of given image points from the previous frame to the current frame and determines the ...
unsigned int patchSize() const
Returns the size of the patches which are used for tracking.
Definition: HomographyTracker.h:126
static Vectors2 transformPoints(const Vectors2 &points, const SquareMatrix3 &transformation)
Transforms a given set of points to a new set using a given transformation.
Definition: HomographyTracker.h:143
void clear()
Clears the previous pyramid frame of this tracking object.
Definition: HomographyTracker.h:138
static bool trackPoints(const Frame &yPreviousFrame, const CV::FramePyramid &previousFramePyramid, const CV::FramePyramid &currentFramePyramid, RandomGenerator &randomGenerator, const Vectors2 &previousPositions, SquareMatrix3 &homography, Worker *worker=nullptr, const unsigned int patchSize=31u)
Tracks a group of given image points from the previous frame to the current frame and determines the ...
HomographyTracker()=default
Creates a new homography tracker object and uses 31 as patch size.
CV::FramePyramid currentFramePyramid_
Frame pyramid of the current frame.
Definition: HomographyTracker.h:111
void setPatchSize(const unsigned int size)
Sets or changes the size of the patches which are used for tracking.
Definition: HomographyTracker.h:131
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