Ocean
PointTracker.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_POINT_TRACKER_H
9 #define META_OCEAN_TRACKING_POINT_POINT_TRACKER_H
10 
12 
13 #include "ocean/base/Frame.h"
14 #include "ocean/base/Lock.h"
15 #include "ocean/base/Worker.h"
16 
17 #include "ocean/cv/FramePyramid.h"
18 
20 
21 #include "ocean/math/Vector2.h"
22 
24 
25 namespace Ocean
26 {
27 
28 namespace Tracking
29 {
30 
31 namespace Point
32 {
33 
34 /**
35  * This class implements a point tracker able to track points between concurrent frames and stores previous points in a database.
36  * The tracker determines feature points in the very first frame - and stored as object points (feature points).<br>
37  * Those points are tracked from one frame to another frame as long as possible.<br>
38  * All points are tracked from the previous frame to the current frame (and not from a common reference frame to the current frame).<br>
39  * Whenever an object point (feature point) is lost the tracker will add a new feature point in the empty region - so that the tracker is always tracking a high number of feature points.<br>
40  * Tracking is based on matches between small image patches around the image points.
41  * @ingroup trackingpoint
42  */
43 class OCEAN_TRACKING_POINT_EXPORT PointTracker
44 {
45  public:
46 
47  /**
48  * Definition of an invalid frame index.
49  */
50  static constexpr Index32 invalidFrameIndex = Index32(-1);
51 
52  /**
53  * Definition of a vector holding 2D vectors.
54  */
55  typedef std::vector<Vectors2> PointTracks;
56 
57  /**
58  * Definition of individual tracking modes.
59  */
60  enum TrackingMode : uint32_t
61  {
62  /// Sum square differences tracking with 7 pixel image patch
64  /// Sum square differences tracking with 15 pixel image patch
66  /// Sum square differences tracking with 31 pixel image patch
68 
69  /// Zero-mean sum square differences tracking with 7 pixel image patch
71  /// Zero-mean sum square differences tracking with 15 pixel image patch
73  /// Zero-mean sum square differences tracking with 31 pixel image patch
75 
76  /// End mode.
77  TM_END
78  };
79 
80  public:
81 
82  /**
83  * Creates a new point tracker.
84  */
86 
87  /**
88  * Move constructor.
89  * @param pointTracker The tracker object to be moved
90  */
91  PointTracker(PointTracker&& pointTracker) noexcept;
92 
93  /**
94  * Copy constructor.
95  * @param pointTracker The tracker object to be copied
96  */
97  PointTracker(const PointTracker& pointTracker);
98 
99  /**
100  * Sets or changes the tracking mode.
101  * @param trackingMode The new tracking mode
102  */
103  inline void setTrackingMode(const TrackingMode trackingMode);
104 
105  /**
106  * Returns the current tracking mode.
107  * @return The point tracker's tracking mode
108  */
109  inline TrackingMode trackingMode() const;
110 
111  /**
112  * Tracks object points (feature points) in a new frame.
113  * @param yFrame The new frame in which the points will be tracked, must have pixel format FORMAT_Y8, must be valid
114  * @param worker Optional worker object to distribute the computation
115  * @return The index of the new frame, 'invalidFrameIndex' if something went wrong
116  */
117  Index32 newFrame(const Frame& yFrame, Worker* worker = nullptr);
118 
119  /**
120  * Returns the point tracks for all object points.
121  * One track is a connected path of images points belonging to one object point (feature point).
122  * @param imageIndex The index of the frame for which the tracks will be provided
123  * @param maximalLength The maximal length of each track (the maximal number of image points for each object point), with range [1, infinity)
124  * @return The tracks of tracked object points starting at the specified frame (and going into the past)
125  */
126  PointTracks pointTracks(const Index32 imageIndex, const unsigned int maximalLength = (unsigned int)(-1));
127 
128  /**
129  * Removes all entries from the tracking database older than a specified frame index.
130  * @param frameIndex The index of the frame which will be the first frame in the database for which data exists
131  * @see clear().
132  */
133  void clearUpTo(const unsigned int frameIndex);
134 
135  /**
136  * Clears the database containing the object points and their corresponding image points.
137  * @see clearUpTo().
138  */
139  inline void clear();
140 
141  /**
142  * Returns the internal database storing the topology of the tracked points.
143  * Beware: This function is not thread-safe.
144  * @return The tracker's database
145  */
146  inline Database& database();
147 
148  /**
149  * Move operator.
150  * @param pointTracker The tracker object to be moved
151  * @return Reference to this object
152  */
153  PointTracker& operator=(PointTracker&& pointTracker) noexcept;
154 
155  /**
156  * Assign operator.
157  * @param pointTracker The tracker object to be moved
158  * @return Reference to this object
159  */
160  PointTracker& operator=(const PointTracker& pointTracker);
161 
162  protected:
163 
164  /**
165  * Detects new feature points in empty regions of the provided frame.
166  * @param yFrame The provided frame in which new feature points will be detected, must have pixel format FORMAT_Y8, must be valid
167  * @param occupancyArray The occupancy array specifying empty and non-empty regions in the frame, must be valid
168  * @param newFeaturePoints The resulting new feature points
169  * @param worker Optional worker object to distribute the computation
170  */
171  void detectNewFeaturePoints(const Frame& yFrame, Geometry::SpatialDistribution::OccupancyArray& occupancyArray, Vectors2& newFeaturePoints, Worker* worker = nullptr);
172 
173  /**
174  * Tracks feature points from one frame (pyramid) to another frame (pyramid).
175  * @param trackingMode The tracking mode to be used, must be valid
176  * @param previousFramePyramid The frame pyramid of the previous frame, must be valid
177  * @param currentFramePyramid The frame pyramid of the current frame, must be valid
178  * @param previousImagePoints The image points located in the previous frame which will be tracked to the current frame, at least one point
179  * @param currentImagePoints The resulting image points located in the current image, one for each previous image points
180  * @param validIndices The indices of all point correspondences that could be tracked reliably (all other correspondences are invalid)
181  * @param worker Optional worker object to distribute the computation
182  * @return True, if succeeded
183  */
184  static bool trackFeaturePoints(const TrackingMode trackingMode, const CV::FramePyramid& previousFramePyramid, const CV::FramePyramid& currentFramePyramid, Vectors2& previousImagePoints, Vectors2& currentImagePoints, Indices32& validIndices, Worker* worker);
185 
186  protected:
187 
188  /// The tracking mode to be used.
189  TrackingMode trackingMode_ = TM_ZM_SSD_7;
190 
191  /// The database storing the object points (feature points) and their corresponding image points.
193 
194  /// The frame pyramid of the previous frame.
196 
197  /// The frame pyramid of the current frame.
199 
200  /// The index of the previous frame.
201  Index32 previousFrameIndex_ = invalidFrameIndex;
202 
203  /// Threshold for strengths of feature points, with range [1, 255]
204  unsigned int featurePointStrengthThreshold_ = 15u;
205 
206  /// The size of each bin (edge length) in pixel controlling whether new feature points will be added in an empty region.
207  unsigned int binSize_ = 40u;
208 
209  /// The lock for this tracker.
210  mutable Lock lock_;
211 };
212 
213 inline void PointTracker::setTrackingMode(const TrackingMode trackingMode)
214 {
215  const ScopedLock scopedLock(lock_);
216 
218 }
219 
221 {
222  const ScopedLock scopedLock(lock_);
223 
224  return trackingMode_;
225 }
226 
227 inline void PointTracker::clear()
228 {
229  const ScopedLock scopedLock(lock_);
230 
231  database_.clear<false>();
232 
236 }
237 
239 {
240  return database_;
241 }
242 
243 }
244 
245 }
246 
247 }
248 
249 #endif // META_OCEAN_TRACKING_POINT_POINT_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 an occupancy array.
Definition: SpatialDistribution.h:370
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This class implements a database for 3D object points, 2D image points and 6DOF camera poses.
Definition: Database.h:67
void clear()
Clears the database including all camera poses, object points, image points and any topology.
Definition: Database.h:4877
This class implements a point tracker able to track points between concurrent frames and stores previ...
Definition: PointTracker.h:44
PointTracker & operator=(PointTracker &&pointTracker) noexcept
Move operator.
Database database_
The database storing the object points (feature points) and their corresponding image points.
Definition: PointTracker.h:192
Lock lock_
The lock for this tracker.
Definition: PointTracker.h:210
PointTracker(const PointTracker &pointTracker)
Copy constructor.
static constexpr Index32 invalidFrameIndex
Definition of an invalid frame index.
Definition: PointTracker.h:50
TrackingMode trackingMode() const
Returns the current tracking mode.
Definition: PointTracker.h:220
Index32 newFrame(const Frame &yFrame, Worker *worker=nullptr)
Tracks object points (feature points) in a new frame.
void clear()
Clears the database containing the object points and their corresponding image points.
Definition: PointTracker.h:227
CV::FramePyramid currentFramePyramid_
The frame pyramid of the current frame.
Definition: PointTracker.h:198
void clearUpTo(const unsigned int frameIndex)
Removes all entries from the tracking database older than a specified frame index.
void detectNewFeaturePoints(const Frame &yFrame, Geometry::SpatialDistribution::OccupancyArray &occupancyArray, Vectors2 &newFeaturePoints, Worker *worker=nullptr)
Detects new feature points in empty regions of the provided frame.
void setTrackingMode(const TrackingMode trackingMode)
Sets or changes the tracking mode.
Definition: PointTracker.h:213
Database & database()
Returns the internal database storing the topology of the tracked points.
Definition: PointTracker.h:238
Index32 previousFrameIndex_
The index of the previous frame.
Definition: PointTracker.h:201
PointTracker(PointTracker &&pointTracker) noexcept
Move constructor.
PointTracks pointTracks(const Index32 imageIndex, const unsigned int maximalLength=(unsigned int)(-1))
Returns the point tracks for all object points.
TrackingMode trackingMode_
The tracking mode to be used.
Definition: PointTracker.h:189
PointTracker & operator=(const PointTracker &pointTracker)
Assign operator.
PointTracker()
Creates a new point tracker.
std::vector< Vectors2 > PointTracks
Definition of a vector holding 2D vectors.
Definition: PointTracker.h:55
TrackingMode
Definition of individual tracking modes.
Definition: PointTracker.h:61
@ TM_ZM_SSD_15
Zero-mean sum square differences tracking with 15 pixel image patch.
Definition: PointTracker.h:72
@ TM_SSD_7
Sum square differences tracking with 7 pixel image patch.
Definition: PointTracker.h:63
@ TM_SSD_15
Sum square differences tracking with 15 pixel image patch.
Definition: PointTracker.h:65
@ TM_ZM_SSD_7
Zero-mean sum square differences tracking with 7 pixel image patch.
Definition: PointTracker.h:70
@ TM_SSD_31
Sum square differences tracking with 31 pixel image patch.
Definition: PointTracker.h:67
@ TM_ZM_SSD_31
Zero-mean sum square differences tracking with 31 pixel image patch.
Definition: PointTracker.h:74
static bool trackFeaturePoints(const TrackingMode trackingMode, const CV::FramePyramid &previousFramePyramid, const CV::FramePyramid &currentFramePyramid, Vectors2 &previousImagePoints, Vectors2 &currentImagePoints, Indices32 &validIndices, Worker *worker)
Tracks feature points from one frame (pyramid) to another frame (pyramid).
CV::FramePyramid previousFramePyramid_
The frame pyramid of the previous frame.
Definition: PointTracker.h:195
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
uint32_t Index32
Definition of a 32 bit index value.
Definition: Base.h:84
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