Ocean
UnifiedPoseEstimation.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_MAPBUILDING_UNIFIED_POSE_ESTIMATION_H
9 #define META_OCEAN_TRACKING_MAPBUILDING_UNIFIED_POSE_ESTIMATION_H
10 
14 
15 namespace Ocean
16 {
17 
18 namespace Tracking
19 {
20 
21 namespace MapBuilding
22 {
23 
24 /**
25  * This class implements a brute-force pose estimation for unified data types.
26  * @ingroup trackingmapbuilding
27  */
29 {
30  public:
31 
32  /**
33  * Definition of an unordered map mapping object point ids to 3D object point locations.
34  */
35  using ObjectPointMap = std::unordered_map<unsigned int, Vector3>;
36 
37  public:
38 
39  /**
40  * Creates a new pose estimation object
41  * @param objectPointMap The map containing all 3D object points used during pose estimation
42  * @param unifiedDescriptorMap The unified descriptor map holding the descriptors of the 3D object points
43  */
44  UnifiedBruteForcePoseEstimation(const ObjectPointMap& objectPointMap, const UnifiedDescriptorMap& unifiedDescriptorMap);
45 
46  /**
47  * Returns all 3D object points which are used for pose estimation.
48  * @return The 3D object points
49  */
50  inline const Vectors3& objectPoints() const;
51 
52  /**
53  * Returns the ids of all 3D object points
54  * @return The object point ids, one for each 3D object point, with same order
55  */
56  inline const Indices32& objectPointIds() const;
57 
58  /**
59  * Determines the camera pose based on several image points and their descriptors.
60  * @param camera The camera profile defining the projection, must be valid
61  * @param imagePointDescriptors The descriptors of the image points which will be used for pose estimation, at least 4
62  * @param imagePoints The 2D image points to be used for pose estimation, one for each image point descriptor, defined in the domain of the camera, must be valid
63  * @param randomGenerator The random generator to be used
64  * @param world_T_camera The resulting transformation between camera and world, with default camera pointing towards the negative z-space with y-axis upwards
65  * @param minimalNumberCorrespondences The minimal number of feature correspondences for a valid pose, with range [4, infinity)
66  * @param maximalDescriptorDistance The maximal distance between feature descriptors to count as a valid descriptor match, must be valid
67  * @param maximalProjectionError The maximal projection error between 3D object points and their 2D observations, in pixels, with range [0, infinity)
68  * @param inlierRate The rate of correspondence inliers within the entire set of correspondences, e.g., 0.15 means that 15% of matched features are correct and can be used to determine a valid pose, with range (0, 1]
69  * @param usedObjectPointIndices Optional resulting indices of all 3D object points which were used during pose estimation, nullptr if not of interest
70  * @param usedImagePointIndices Optional resulting indices of all 2D image points which were used during pose estimation, nullptr if not of interest
71  * @param world_T_roughCamera Optional rough camera pose to speedup the pose estimation, if known, invalid otherwise
72  * @param worker Optional worker to distribute the computation
73  * @return True, if succeeded
74  */
75  bool determinePose(const AnyCamera& camera, const UnifiedDescriptors& imagePointDescriptors, const Vector2* imagePoints, RandomGenerator& randomGenerator, HomogenousMatrix4& world_T_camera, const unsigned int minimalNumberCorrespondences, const UnifiedMatching::DistanceValue& maximalDescriptorDistance, const Scalar maximalProjectionError, const Scalar inlierRate, Indices32* usedObjectPointIndices = nullptr, Indices32* usedImagePointIndices = nullptr, const HomogenousMatrix4& world_T_roughCamera = HomogenousMatrix4(false), Worker* worker = nullptr) const;
76 
77  /**
78  * Determines the brute-force matching between two sets of feature descriptors.
79  * @param descriptorsA The first set of feature descriptors
80  * @param descriptorsB The second set of feature descriptors, must be compatible with the first set of descriptors
81  * @param maximalDescriptorDistance The maximal distance between feature descriptors to count as a valid descriptor match, must be valid
82  * @param indicesA The resulting indices of descriptors from the first set which could be matched to descriptors from the second set
83  * @param indicesB The resulting indices of descriptors from the second set which could be matched to descriptors from the first set
84  * @param distances Optional resulting distances between the individually matched descriptors, nullptr if not of interest
85  * @param worker Optional worker to distribute the computation
86  * @return True, if succeeded
87  */
88  static bool determineBruteForceMatchings(const UnifiedDescriptors& descriptorsA, const UnifiedDescriptors& descriptorsB, const UnifiedMatching::DistanceValue& maximalDescriptorDistance, Indices32& indicesA, Indices32& indicesB, std::vector<double>* distances = nullptr, Worker* worker = nullptr);
89 
90  template <typename TDescriptorA, typename TDescriptorB, typename TDescriptorDistance, TDescriptorDistance(*tDescriptorDistanceFunction)(const TDescriptorA&, const TDescriptorB&)>
91  static void determineBruteForceMatchings(const TDescriptorA* descriptorsA, const size_t numberDescriptorsA, const TDescriptorB* descriptorsB, const size_t numberDescriptorsB, const TDescriptorDistance maximalDescriptorDistance, Indices32& indicesA, Indices32& indicesB, std::vector<double>* distances = nullptr, Worker* worker = nullptr);
92 
93  protected:
94 
95  template <typename TUnifiedDescriptorsA, typename TUnifiedDescriptorsB, typename TDescriptorDistance>
96  static bool determineBruteForceMatchings(const UnifiedDescriptors& descriptorsA, const UnifiedDescriptors& descriptorsB, const TDescriptorDistance maximalDescriptorDistance, Indices32& indicesA, Indices32& indicesB, std::vector<double>* distances = nullptr, Worker* worker = nullptr);
97 
98  /**
99  * Extracts serialized descriptors from a descriptor map.
100  * @param unifiedDescriptorMap The map from which the descriptors will be extracted
101  * @param objectPointIds The ids of the object points for which descriptors will be extracted
102  * @return The resulting serialized descriptors
103  */
105 
106  /**
107  * Extracts serialized descriptors from a descriptor map.
108  * @param unifiedDescriptorMap The map from which the descriptors will be extracted
109  * @param objectPointIds The ids of the object points for which descriptors will be extracted
110  * @return The resulting serialized descriptors
111  * @tparam T The data type of the descriptors
112  */
113  template <typename T>
115 
116  protected:
117 
118  /// The 3D object points which will be used for pose estimation.
120 
121  /// The ids of all 3D object points, one for each object point.
123 
124  /// The descriptors of the 3D object point in sequential order (same order as the 3D object points), one for each object point.
126 };
127 
129 {
130  ocean_assert(objectPoints_.size() == objectPointIds_.size());
131 
132  return objectPoints_;
133 }
134 
136 {
137  ocean_assert(objectPoints_.size() == objectPointIds_.size());
138 
139  return objectPointIds_;
140 }
141 
142 template <typename TDescriptorA, typename TDescriptorB, typename TDescriptorDistance, TDescriptorDistance(*tDescriptorDistanceFunction)(const TDescriptorA&, const TDescriptorB&)>
143 void UnifiedBruteForcePoseEstimation::determineBruteForceMatchings(const TDescriptorA* descriptorsA, const size_t numberDescriptorsA, const TDescriptorB* descriptorsB, const size_t numberDescriptorsB, const TDescriptorDistance maximalDescriptorDistance, Indices32& indicesA, Indices32& indicesB, std::vector<double>* distances, Worker* worker)
144 {
145  ocean_assert(descriptorsA != nullptr);
146  ocean_assert(numberDescriptorsA != 0);
147 
148  ocean_assert(descriptorsB != nullptr);
149  ocean_assert(numberDescriptorsB != 0);
150 
151  Indices32 indicesB2A(numberDescriptorsB);
152  Indices32 floatDistances(numberDescriptorsB);
153 
154  PoseEstimationT::determineUnguidedBruteForceMatchings<TDescriptorA, TDescriptorB, TDescriptorDistance, tDescriptorDistanceFunction>(descriptorsA, numberDescriptorsA, descriptorsB, numberDescriptorsB, maximalDescriptorDistance, indicesB2A.data(), worker);
155 
156  for (size_t n = 0; n < indicesB2A.size(); ++n)
157  {
158  const Index32& indexA = indicesB2A[n];
159 
160  if (indexA != Index32(-1))
161  {
162  indicesA.emplace_back(indexA);
163  indicesB.emplace_back(Index32(n));
164 
165  if (distances != nullptr)
166  {
167  distances->emplace_back(double(floatDistances[n]));
168  }
169  }
170  }
171 }
172 
173 }
174 
175 }
176 
177 }
178 
179 #endif // META_OCEAN_TRACKING_MAPBUILDING_UNIFIED_POSE_ESTIMATION_H
This class implements the abstract base class for all AnyCamera objects.
Definition: AnyCamera.h:130
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
This class implements a brute-force pose estimation for unified data types.
Definition: UnifiedPoseEstimation.h:29
const Indices32 & objectPointIds() const
Returns the ids of all 3D object points.
Definition: UnifiedPoseEstimation.h:135
static bool determineBruteForceMatchings(const UnifiedDescriptors &descriptorsA, const UnifiedDescriptors &descriptorsB, const TDescriptorDistance maximalDescriptorDistance, Indices32 &indicesA, Indices32 &indicesB, std::vector< double > *distances=nullptr, Worker *worker=nullptr)
static SharedUnifiedDescriptors extractObjectPointDescriptors(const UnifiedDescriptorMap &unifiedDescriptorMap, const Indices32 &objectPointIds)
Extracts serialized descriptors from a descriptor map.
UnifiedBruteForcePoseEstimation(const ObjectPointMap &objectPointMap, const UnifiedDescriptorMap &unifiedDescriptorMap)
Creates a new pose estimation object.
bool determinePose(const AnyCamera &camera, const UnifiedDescriptors &imagePointDescriptors, const Vector2 *imagePoints, RandomGenerator &randomGenerator, HomogenousMatrix4 &world_T_camera, const unsigned int minimalNumberCorrespondences, const UnifiedMatching::DistanceValue &maximalDescriptorDistance, const Scalar maximalProjectionError, const Scalar inlierRate, Indices32 *usedObjectPointIndices=nullptr, Indices32 *usedImagePointIndices=nullptr, const HomogenousMatrix4 &world_T_roughCamera=HomogenousMatrix4(false), Worker *worker=nullptr) const
Determines the camera pose based on several image points and their descriptors.
static bool determineBruteForceMatchings(const UnifiedDescriptors &descriptorsA, const UnifiedDescriptors &descriptorsB, const UnifiedMatching::DistanceValue &maximalDescriptorDistance, Indices32 &indicesA, Indices32 &indicesB, std::vector< double > *distances=nullptr, Worker *worker=nullptr)
Determines the brute-force matching between two sets of feature descriptors.
SharedUnifiedDescriptors objectPointDescriptors_
The descriptors of the 3D object point in sequential order (same order as the 3D object points),...
Definition: UnifiedPoseEstimation.h:125
const Vectors3 & objectPoints() const
Returns all 3D object points which are used for pose estimation.
Definition: UnifiedPoseEstimation.h:128
Indices32 objectPointIds_
The ids of all 3D object points, one for each object point.
Definition: UnifiedPoseEstimation.h:122
static SharedUnifiedDescriptors extractObjectPointDescriptors(const T &unifiedDescriptorMap, const Indices32 &objectPointIds)
Extracts serialized descriptors from a descriptor map.
Vectors3 objectPoints_
The 3D object points which will be used for pose estimation.
Definition: UnifiedPoseEstimation.h:119
std::unordered_map< unsigned int, Vector3 > ObjectPointMap
Definition of an unordered map mapping object point ids to 3D object point locations.
Definition: UnifiedPoseEstimation.h:35
This class implements the base class for all unified descriptor maps in which ids are mapped to descr...
Definition: UnifiedDescriptorMap.h:38
This class implements the base class for all unified descriptor buffers stored in a continuous memory...
Definition: UnifiedDescriptors.h:29
Definition of a descriptor distance value.
Definition: UnifiedMatching.h:52
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
float Scalar
Definition of a scalar type.
Definition: Math.h:128
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
std::shared_ptr< UnifiedDescriptors > SharedUnifiedDescriptors
Definition of a shared pointer holding a UnifiedDescriptors object.
Definition: UnifiedDescriptors.h:63
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15