Ocean
point/Frame2FrameTracker.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_FRAME_2_FRAME_TRACKER_H
9 #define META_OCEAN_TRACKING_POINT_FRAME_2_FRAME_TRACKER_H
10 
12 
13 #include "ocean/base/Frame.h"
14 #include "ocean/base/Worker.h"
15 
16 #include "ocean/cv/FramePyramid.h"
17 
19 
22 #include "ocean/math/Vector2.h"
23 #include "ocean/math/Vector3.h"
24 
26 
27 namespace Ocean
28 {
29 
30 namespace Tracking
31 {
32 
33 namespace Point
34 {
35 
36 /**
37  * This class implements simple functions allowing to track or to determine the camera motion from one frame to another frame.
38  * @ingroup tracking
39  */
41 {
42  public:
43 
44  /**
45  * Determines the camera pose for a current camera frame by tracking image points from the previous frame (for which the corresponding 3D object points are known) to the current frame.
46  * @param pinholeCamera The pinhole camera profile defining the projection
47  * @param previousPose The known camera pose for the previous camera frame, must be valid
48  * @param previousFrame The previous camera frame, must be valid
49  * @param currentFrame The current camera frame, must be valid with same frame type as the previous frame
50  * @param previousObjectPoints The 3D object point locations corresponding to the image points in the previous frame, at least 3
51  * @param previousImagePoints The 2D image points defined in the previous camera frame, one for each provided 3D object point
52  * @param distortImagePoints True, to use the distortion parameters of the camera profile
53  * @param maximalOffset The maximal offset of image points between the previous and the current frame in pixel, with range (0, infinity)
54  * @param currentPose The resulting camera pose of the current frame
55  * @param validIndices Optional resulting indices of the provided previous image points that could be tracked to the current frame
56  * @param worker Optional worker object to distribute the computation
57  * @tparam tSize The size of the image patches that are used for point tracking, must be odd, with range [1, infinity)
58  */
59  template <unsigned int tSize>
60  static bool determinePose(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& previousPose, const Frame& previousFrame, const Frame& currentFrame, const Vectors3& previousObjectPoints, const Vectors2& previousImagePoints, const bool distortImagePoints, const unsigned int maximalOffset, HomogenousMatrix4& currentPose, Indices32* validIndices = nullptr, Worker* worker = nullptr);
61 };
62 
63 template <unsigned int tSize>
64 bool Frame2FrameTracker::determinePose(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& previousPose, const Frame& previousFrame, const Frame& currentFrame, const Vectors3& previousObjectPoints, const Vectors2& previousImagePoints, const bool distortImagePoints, const unsigned int maximalOffset, HomogenousMatrix4& currentPose, Indices32* validIndices, Worker* worker)
65 {
66  ocean_assert(pinholeCamera.isValid() && previousPose.isValid());
67  ocean_assert(previousFrame.isValid() && currentFrame.isValid());
68 
69  ocean_assert(previousFrame.frameType() == currentFrame.frameType());
70 
71  ocean_assert(previousObjectPoints.size() >= 3);
72  ocean_assert(previousObjectPoints.size() == previousImagePoints.size());
73 
74  Vectors2 copyPreviousImagePoints(previousImagePoints);
75 
76  Vectors2 currentImagePoints;
77  Indices32 internalValidIndices;
78  if (!CV::Advanced::AdvancedMotion<>::trackPointsBidirectionalSubPixelMirroredBorder<7u>(previousFrame, currentFrame, maximalOffset, 2u, copyPreviousImagePoints, currentImagePoints, Scalar(0.9 * 0.9), CV::FramePyramid::DM_FILTER_14641, worker, &internalValidIndices))
79  {
80  return false;
81  }
82 
83  if (internalValidIndices.size() < 3)
84  {
85  return false;
86  }
87 
88  if (!Geometry::NonLinearOptimizationPose::optimizePose(pinholeCamera, previousPose, ConstArraySubsetAccessor<Vector3, Index32>(previousObjectPoints, internalValidIndices), ConstArraySubsetAccessor<Vector2, Index32>(currentImagePoints, internalValidIndices), distortImagePoints, currentPose))
89  {
90  return false;
91  }
92 
93  if (validIndices)
94  {
95  *validIndices = std::move(internalValidIndices);
96  }
97 
98  return true;
99 }
100 
101 }
102 
103 }
104 
105 }
106 
107 #endif // META_OCEAN_TRACKING_POINT_FRAME_2_FRAME_TRACKER_H
This class implements advanced motion techniques (mainly with sub-pixel accuracy or binary masks) all...
Definition: AdvancedMotion.h:69
@ DM_FILTER_14641
Down sampling is realized by a 5x5 Gaussian filter.
Definition: FramePyramid.h:72
This class implements an indexed-based constant accessor providing access to a subset of elements sto...
Definition: Accessor.h:1141
This class implements Ocean's image class.
Definition: Frame.h:1792
const FrameType & frameType() const
Returns the frame type of this frame.
Definition: Frame.h:3775
bool isValid() const
Returns whether this frame is valid.
Definition: Frame.h:4448
static bool optimizePose(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &world_T_camera, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, const bool distortImagePoints, HomogenousMatrix4 &world_T_optimizedCamera, const unsigned int iterations=20u, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=10, Scalar *initialError=nullptr, Scalar *finalError=nullptr)
Deprecated.
Definition: NonLinearOptimizationPose.h:282
bool isValid() const
Returns whether this matrix is a valid homogeneous transformation.
Definition: HomogenousMatrix4.h:1806
bool isValid() const
Returns whether this camera is valid.
Definition: PinholeCamera.h:1572
This class implements simple functions allowing to track or to determine the camera motion from one f...
Definition: point/Frame2FrameTracker.h:41
static bool determinePose(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &previousPose, const Frame &previousFrame, const Frame &currentFrame, const Vectors3 &previousObjectPoints, const Vectors2 &previousImagePoints, const bool distortImagePoints, const unsigned int maximalOffset, HomogenousMatrix4 &currentPose, Indices32 *validIndices=nullptr, Worker *worker=nullptr)
Determines the camera pose for a current camera frame by tracking image points from the previous fram...
Definition: point/Frame2FrameTracker.h:64
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
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15