Ocean
SLAMTracker6DOF.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_DEVICES_SLAM_TRACKER_6DOF_H
9 #define META_OCEAN_DEVICES_SLAM_TRACKER_6DOF_H
10 
13 
14 #include "ocean/base/Thread.h"
15 
16 #include "ocean/cv/FramePyramid.h"
17 
20 
23 
24 namespace Ocean
25 {
26 
27 namespace Devices
28 {
29 
30 namespace SLAM
31 {
32 
33 /**
34  * This class implements an SLAM feature based tracker.
35  * @ingroup devicesslam
36  */
37 class OCEAN_DEVICES_SLAM_EXPORT SLAMTracker6DOF :
38  virtual public SLAMDevice,
39  virtual public Tracker6DOF,
40  virtual public VisualTracker,
41  protected Thread
42 {
43  friend class SLAMFactory;
44 
45  protected:
46 
47  /**
48  * Definition of a pair holding a camera pose and image point.
49  */
50  typedef std::pair<HomogenousMatrix4, Vector2> Observation;
51 
52  /**
53  * Definition of a vector holding observations.
54  */
55  typedef std::vector<Observation> Observations;
56 
57  /**
58  * Definition of a vector holding observation groups.
59  */
60  typedef std::vector<Observations> ObservationGroups;
61 
62  public:
63 
64  /**
65  * Returns whether this device is active.
66  * @see Devices::isStarted().
67  */
68  bool isStarted() const override;
69 
70  /**
71  * Starts the device.
72  * @see Device::start().
73  */
74  bool start() override;
75 
76  /**
77  * Stops the device.
78  * @see Device::stop().
79  */
80  bool stop() override;
81 
82  /**
83  * Returns whether a specific object is currently actively tracked by this tracker.
84  * @see Tracker::isObjectTracked().
85  */
86  bool isObjectTracked(const ObjectId& objectId) const override;
87 
88  /**
89  * Returns the name of this tracker.
90  * @return Tracker name
91  */
92  static inline std::string deviceNameSLAMTracker6DOF();
93 
94  /**
95  * Returns the type of this tracker.
96  * @return Tracker type
97  */
98  static inline DeviceType deviceTypeSLAMTracker6DOF();
99 
100  private:
101 
102  /**
103  * Creates a new SLAM feature based 6DOF tracker object.
104  */
106 
107  /**
108  * Destructs an SLAM feature based 6DOF tracker object.
109  */
110  ~SLAMTracker6DOF() override;
111 
112  /**
113  * Thread function.
114  * @see Thread::threadRun().
115  */
116  void threadRun() override;
117 
118  /**
119  * Posts a new camera pose.
120  * @param pose The camera pose to post
121  * @param timestamp The timestamp of the frame to which the pose belongs
122  */
123  void postPose(const HomogenousMatrix4& pose, const Timestamp& timestamp);
124 
125  /**
126  * Determines feature points in a given camera frame, scatters the feature points into individual bins while (optional) skips bins in which points are located already.
127  * @param frame The frame in which the feature points will be determined
128  * @param alreadyKnownFeaturePoints Optional already known feature points in which's bins no further/new feature points will be determined
129  * @param newFeaturePoints The resulting new feature points
130  * @param binSize The size of a bin in pixel, with range [1, infinity)
131  * @param worker Optional worker to distribute the computation
132  */
133  static void determineFeaturePoints(const Frame& frame, const Vectors2& alreadyKnownFeaturePoints, Vectors2& newFeaturePoints, const unsigned int binSize = 50u, Worker* worker = nullptr);
134 
135  /**
136  * Tracks feature points from a previous frame to the current frame.
137  * @param previousFramePyramid The frame pyramid of the previous frame
138  * @param currentFramePyramid The frame pyramid of the current frame, with same layout and pixel format as the pyramid of the previous frame
139  * @param previousImagePoints The feature points located in the previous frame
140  * @param currentImagePoints The resulting tracked feature points in the current frame
141  * @param validIndices The indices of the successfully tracked feature points
142  * @param worker Optional worker to distribute the computation
143  * @return True, if succeeded
144  * @tparam tSize The size of the image patches used for tracking, must be odd, with range [1, infinity)
145  */
146  template <unsigned int tSize>
147  static bool trackPoints(const CV::FramePyramid& previousFramePyramid, const CV::FramePyramid& currentFramePyramid, const Vectors2& previousImagePoints, Vectors2& currentImagePoints, Indices32& validIndices, Worker* worker = nullptr);
148 
149  /**
150  * Determines the locations of initial 3D object points from two sets of corresponding image points from individual (stereo) frames.
151  * The origin of the world coordinate system will be located onto the dominant 3D plane where the principal point's viewing ray is intersecting the plane.
152  * @param pinholeCamera The camera profile defining the projection
153  * @param firstImagePoints The first set of image points, at least 5
154  * @param secondImagePoints The second set of image points, one image point for each image point in the first set
155  * @param pose The resulting camera pose of the frame in which the second image points are located (the camera pose of the first frame is expected to be the identity pose)
156  * @param objectPoints The resulting object point locations for the image points
157  * @param validImagePoints The indices of all valid image point correspondences, one index for each resulting object point
158  * @return True, if succeeded
159  */
160  static bool determineInitialObjectPoints(const PinholeCamera& pinholeCamera, const Vectors2& firstImagePoints, const Vectors2& secondImagePoints, HomogenousMatrix4& pose, Vectors3& objectPoints, Indices32& validImagePoints);
161 
162  /**
163  * Combines three groups of image points to one large set of image points.
164  * @param locatedPreviousImagePoints The image points of already located 3D object points as observed in the previous frame
165  * @param unlocatedObservationGroups The groups of observation of unlocated 3D object points as observed in the previous frames
166  * @param newObservations The set of image points not jet connected with a valid camera pose
167  * @return The large set of image points
168  */
169  static Vectors2 combineImagePointGroups(const Vectors2& locatedPreviousImagePoints, const ObservationGroups& unlocatedObservationGroups, const Vectors2& newObservations);
170 
171  /**
172  * Extracts the indices of the combined image points belonging to the group of image points for which a located 3D object point exists.
173  * @param numberLocatedPreviousImagePoints The number of image points from the previous frame for which a 3D object point location is known
174  * @param validIndices The indices of valid combined image points
175  * @return The indices of the combined image points
176  */
177  static Indices32 extractLocatedImagePointIndices(const size_t numberLocatedPreviousImagePoints, const Indices32& validIndices);
178 
179  /**
180  * Extracts the image points of already located 3D object points from the set of combined image points.
181  * @param combinedImagePoints The set of combined image points
182  * @param numberLocatedPreviousImagePoints The number of image points from the previous frame for which a 3D object point location is known
183  * @param validIndices The indices of valid combined image points
184  * @return The image points for which the location of the 3D object point is known
185  */
186  static Vectors2 extractLocatedImagePoints(const Vectors2& combinedImagePoints, const size_t numberLocatedPreviousImagePoints, const Indices32& validIndices);
187 
188  /**
189  * Extracts the image points for which no known 3D object point location has been determined already from the set of combined image points.
190  * The extracted image points will be inserted into the groups of observations.<br>
191  * Groups of observations without valid (tracked) image point will be removed
192  * @param combinedImagePoints The set of combined image points
193  * @param numberLocatedPreviousImagePoints The number of image points from the previous frame for which a 3D object point location is known
194  * @param validIndices The indices of valid combined image points
195  * @param pose The most recent camera pose, must be valid
196  * @param observationGroups The groups of observations which provide candidates for new 3D object points
197  */
198  static void extractUnlocatedImagePoints(const Vectors2& combinedImagePoints, const size_t numberLocatedPreviousImagePoints, const Indices32& validIndices, const HomogenousMatrix4& pose, ObservationGroups& observationGroups);
199 
200  /**
201  * Extends the tracking database by determining the locations of 3D object points based on the observations in several individual camera frames.
202  * Observations for which a 3D object point could be determined will be removed from the set of candidates.
203  * @param pinholeCamera The camera profile defining the projection
204  * @param observationGroups The groups of observations which provide candidates for new 3D object points
205  * @param objectPoints The already known object points currently used for tracking, this set will be extended by new 3D object point locations
206  * @param imagePoints The image points currently used for tracking, one image point for each object point, this set will be extended by new image points locations
207  * @param minimalObservations The minimal number of observations an object point must have to so that the 3D object point will be determined
208  */
209  static void extendTrackingDatabase(const PinholeCamera& pinholeCamera, ObservationGroups& observationGroups, Vectors3& objectPoints, Vectors2& imagePoints, const unsigned int minimalObservations = 20u);
210 
211  /**
212  * Determines the median angle between the mean viewing ray and all individual viewing rays of an object points (and the corresponding image points respectively).
213  * @param pinholeCamera The camera profile defining the projection
214  * @param observations The observations of the object point
215  * @return The median angle, in radian
216  */
217  static Scalar medianObservationAngle(const PinholeCamera& pinholeCamera, const Observations& observations);
218 
219  private:
220 
221  /// Frame timestamp.
223 
224  /// Frame recent type.
226 
227  /// Most recent sample timestamp.
229 
230  /// The camera calibration object.
232 
233  /// The 2D image points located in the first stereo vision frame which will be used for initialization.
235 
236  /// The 2D image points located in the recent stereo vision frame which will be used for initialization.
238 
239  /// The number of image points determined in the first stereo vision frame.
240  size_t initializationImagePointsDetermined_ = 0;
241 
242  /// The timestamp after which the first stereo vision frame may be used.
244 
245  /// The 3D object points currently used for tracking.
247 
248  /// The 2D image points currently used for tracking.
250 
251  /// The frame pyramid of the current frame.
253 
254  /// The frame pyramid of the previous frame.
256 
257  /// The recent camera pose.
258  HomogenousMatrix4 previousPose_ = HomogenousMatrix4(false);
259 
260  /// The observation groups of feature point candidates.
262 
263  /// The unique object id of this tracker.
264  ObjectId uniqueObjectId_ = invalidObjectId();
265 };
266 
268 {
269  return std::string("SLAM Feature Based 6DOF Tracker");
270 }
271 
273 {
275 }
276 
277 }
278 
279 }
280 
281 }
282 
283 #endif // META_OCEAN_DEVICES_SLAM_TRACKER_6DOF_H
This class implements a frame pyramid.
Definition: FramePyramid.h:37
Definition of a class holding the major and minor device type.
Definition: devices/Device.h:62
unsigned int ObjectId
Definition of an object id.
Definition: Measurement.h:46
This class implements a device for the SLAM library.
Definition: SLAMDevice.h:29
This class implements a device factory for the SLAM feature based tracking system.
Definition: SLAMFactory.h:29
This class implements an SLAM feature based tracker.
Definition: SLAMTracker6DOF.h:42
static Indices32 extractLocatedImagePointIndices(const size_t numberLocatedPreviousImagePoints, const Indices32 &validIndices)
Extracts the indices of the combined image points belonging to the group of image points for which a ...
static void extractUnlocatedImagePoints(const Vectors2 &combinedImagePoints, const size_t numberLocatedPreviousImagePoints, const Indices32 &validIndices, const HomogenousMatrix4 &pose, ObservationGroups &observationGroups)
Extracts the image points for which no known 3D object point location has been determined already fro...
static void extendTrackingDatabase(const PinholeCamera &pinholeCamera, ObservationGroups &observationGroups, Vectors3 &objectPoints, Vectors2 &imagePoints, const unsigned int minimalObservations=20u)
Extends the tracking database by determining the locations of 3D object points based on the observati...
bool stop() override
Stops the device.
bool isStarted() const override
Returns whether this device is active.
static Vectors2 combineImagePointGroups(const Vectors2 &locatedPreviousImagePoints, const ObservationGroups &unlocatedObservationGroups, const Vectors2 &newObservations)
Combines three groups of image points to one large set of image points.
PinholeCamera camera_
The camera calibration object.
Definition: SLAMTracker6DOF.h:231
static std::string deviceNameSLAMTracker6DOF()
Returns the name of this tracker.
Definition: SLAMTracker6DOF.h:267
Timestamp initializationTimestamp_
The timestamp after which the first stereo vision frame may be used.
Definition: SLAMTracker6DOF.h:243
static Scalar medianObservationAngle(const PinholeCamera &pinholeCamera, const Observations &observations)
Determines the median angle between the mean viewing ray and all individual viewing rays of an object...
Timestamp recentFrameTimestamp_
Most recent sample timestamp.
Definition: SLAMTracker6DOF.h:228
void postPose(const HomogenousMatrix4 &pose, const Timestamp &timestamp)
Posts a new camera pose.
~SLAMTracker6DOF() override
Destructs an SLAM feature based 6DOF tracker object.
std::pair< HomogenousMatrix4, Vector2 > Observation
Definition of a pair holding a camera pose and image point.
Definition: SLAMTracker6DOF.h:50
CV::FramePyramid previousFramePyramid_
The frame pyramid of the previous frame.
Definition: SLAMTracker6DOF.h:255
std::vector< Observations > ObservationGroups
Definition of a vector holding observation groups.
Definition: SLAMTracker6DOF.h:60
Vectors2 initializationFirstImagePoints_
The 2D image points located in the first stereo vision frame which will be used for initialization.
Definition: SLAMTracker6DOF.h:234
std::vector< Observation > Observations
Definition of a vector holding observations.
Definition: SLAMTracker6DOF.h:55
SLAMTracker6DOF()
Creates a new SLAM feature based 6DOF tracker object.
static Vectors2 extractLocatedImagePoints(const Vectors2 &combinedImagePoints, const size_t numberLocatedPreviousImagePoints, const Indices32 &validIndices)
Extracts the image points of already located 3D object points from the set of combined image points.
static void determineFeaturePoints(const Frame &frame, const Vectors2 &alreadyKnownFeaturePoints, Vectors2 &newFeaturePoints, const unsigned int binSize=50u, Worker *worker=nullptr)
Determines feature points in a given camera frame, scatters the feature points into individual bins w...
Vectors2 initializationRecentImagePoints_
The 2D image points located in the recent stereo vision frame which will be used for initialization.
Definition: SLAMTracker6DOF.h:237
CV::FramePyramid currentFramePyramid_
The frame pyramid of the current frame.
Definition: SLAMTracker6DOF.h:252
void threadRun() override
Thread function.
FrameType recentFrameType_
Frame recent type.
Definition: SLAMTracker6DOF.h:225
static DeviceType deviceTypeSLAMTracker6DOF()
Returns the type of this tracker.
Definition: SLAMTracker6DOF.h:272
Vectors2 imagePoints_
The 2D image points currently used for tracking.
Definition: SLAMTracker6DOF.h:249
ObservationGroups observationGroups_
The observation groups of feature point candidates.
Definition: SLAMTracker6DOF.h:261
bool start() override
Starts the device.
bool isObjectTracked(const ObjectId &objectId) const override
Returns whether a specific object is currently actively tracked by this tracker.
static bool trackPoints(const CV::FramePyramid &previousFramePyramid, const CV::FramePyramid &currentFramePyramid, const Vectors2 &previousImagePoints, Vectors2 &currentImagePoints, Indices32 &validIndices, Worker *worker=nullptr)
Tracks feature points from a previous frame to the current frame.
static bool determineInitialObjectPoints(const PinholeCamera &pinholeCamera, const Vectors2 &firstImagePoints, const Vectors2 &secondImagePoints, HomogenousMatrix4 &pose, Vectors3 &objectPoints, Indices32 &validImagePoints)
Determines the locations of initial 3D object points from two sets of corresponding image points from...
Timestamp frameTimestamp_
Frame timestamp.
Definition: SLAMTracker6DOF.h:222
Vectors3 objectPoints_
The 3D object points currently used for tracking.
Definition: SLAMTracker6DOF.h:246
This class implements the base for all 6DOF trackers.
Definition: Tracker6DOF.h:39
static DeviceType deviceTypeTracker6DOF()
Definition of this device type.
Definition: Tracker6DOF.h:100
@ TRACKER_VISUAL
Tracker using a visual input for their measurements.
Definition: devices/Tracker.h:62
This class is the base class for all tracker using visual input to create the tracking results.
Definition: devices/VisualTracker.h:41
This class implements Ocean's image class.
Definition: Frame.h:1760
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition: Frame.h:30
This class implements a thread.
Definition: Thread.h:115
This class implements a timestamp.
Definition: Timestamp.h:36
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
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
HomogenousMatrixT4< Scalar > HomogenousMatrix4
Definition of the HomogenousMatrix4 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION flag eit...
Definition: HomogenousMatrix4.h:37
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
const ObjectId invalidObjectId
Definition of an invalid object id.
Definition: rendering/Rendering.h:65
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15