Ocean
NonLinearOptimizationTransformation.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_GEOMETRY_NON_LINEAR_OPTIMIZATION_TRANSFORMATION_H
9 #define META_OCEAN_GEOMETRY_NON_LINEAR_OPTIMIZATION_TRANSFORMATION_H
10 
13 
14 #include "ocean/math/AnyCamera.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Geometry
21 {
22 
23 /**
24  * This class implements non linear optimization algorithms for coordinate transformations.
25  * @ingroup geometry
26  */
27 class OCEAN_GEOMETRY_EXPORT NonLinearOptimizationTransformation : protected NonLinearOptimization
28 {
29  protected:
30 
31  /**
32  * Forward declaration of a class implementing a provider allowing to optimize a 6-DOF transformation for any camera.
33  */
34  class AdvancedObjectTransformationOptimizationProvider;
35 
36  /**
37  * Forward declaration of a class implementing a provider allowing to optimize a 6-DOF transformation for any stereo camera.
38  */
39  class AdvancedObjectTransformationStereoOptimizationProvider;
40 
41  public:
42 
43  /**
44  * Minimizes the projection error for several 3D object points projected into several camera images via a 6-DOF object transformation (to be optimized).
45  * The individual camera poses and the camera profile will not be adjusted.<br>
46  * The entire projection pipeline has the following equation:
47  * <pre>
48  * q = K * (world_T_camera)^-1 * world_T_object * X
49  *
50  * With:
51  * q Projected 2D image point
52  * K Static camera projection matrix
53  * world_T_camera Static 6-DOF camera pose transforming points from world coordinate system to camera coordinate system
54  * world_T_object 6-DOF object transformation for which the projection error will be minimized, transforming points in the object coordinate system to points in the world coordinate system
55  * X 3D object point, defined in the object coordinate system
56  * </pre>
57  * @param camera The camera profile defining the projection between 3D object points and 2D image points, must be valid
58  * @param world_T_cameras Several 6-DOF camera pose which will not be adjusted, transforming points from the world coordinate system to points in the camera coordinate systems, (world_T_camera), one pose for each group of 2D image points
59  * @param world_T_object The 6-DOF object transformation to be optimized, with orthonormal rotation matrix, must be valid
60  * @param objectPointGroups The groups of 3D object points to be projected into the camera image, one group for each camera pose, one 3D object point for each 2D image point visible in each camera frame
61  * @param imagePointGroups The groups of 2D image points, one group for each camera pose, each image point has a corresponding 3D object point
62  * @param optimized_world_T_object The resulting optimized 6-DOF object point transformation
63  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
64  * @param estimator The robust error estimator to be used
65  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
66  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
67  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator
68  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator
69  * @param intermediateErrors Optional resulting averaged pixel errors for each intermediate optimization step, in relation to the defined estimator
70  * @return True, if the optimization succeeded
71  * @see optimizeObjectTransformationIF(), optimizeObjectTransformationStereo().
72  */
73  static inline bool optimizeObjectTransformation(const AnyCamera& camera, const HomogenousMatrices4& world_T_cameras, const HomogenousMatrix4& world_T_object, const ObjectPointGroups& objectPointGroups, const ImagePointGroups& imagePointGroups, HomogenousMatrix4& optimized_world_T_object, const unsigned int iterations = 20u, const Estimator::EstimatorType estimator = Estimator::ET_SQUARE, Scalar lambda = Scalar(0.001), const Scalar lambdaFactor = Scalar(5), Scalar* initialError = nullptr, Scalar* finalError = nullptr, Scalars* intermediateErrors = nullptr);
74 
75  /**
76  * Minimizes the projection error for several 3D object points projected into several camera images via a 6-DOF object transformation (to be optimized).
77  * This function applies the same optimization as optimizeObjectTransformation() while uses 6-DOF camera poses with inverted and flipped coordinate system.
78  * @see optimizeObjectTransformation(), optimizeObjectTransformationStereoIF().
79  */
80  static bool optimizeObjectTransformationIF(const AnyCamera& camera, const HomogenousMatrices4& flippedCameras_T_world, const HomogenousMatrix4& world_T_object, const ObjectPointGroups& objectPointGroups, const ImagePointGroups& imagePointGroups, HomogenousMatrix4& optimized_world_T_object, const unsigned int iterations = 20u, const Estimator::EstimatorType estimator = Estimator::ET_SQUARE, Scalar lambda = Scalar(0.001), const Scalar lambdaFactor = Scalar(5), Scalar* initialError = nullptr, Scalar* finalError = nullptr, Scalars* intermediateErrors = nullptr);
81 
82  /**
83  * Minimizes the projection error for several 3D object points projected into several stereo camera images via a 6-DOF object transformation (to be optimized).
84  * The individual camera poses and the camera profile will not be adjusted.<br>
85  * @param cameraA First camera profile defining the project for the first stereo camera, must be valid
86  * @param cameraB Second camera profile defining the project for the second stereo camera, must be valid
87  * @param world_T_camerasA Several 6-DOF camera pose connected with the first camera profile which will not be adjusted, transforming points from the world coordinate system to points in the camera coordinate systems, (world_T_camera), one pose for each group of 2D image points
88  * @param world_T_camerasB Several 6-DOF camera pose connected with the second camera profile which will not be adjusted, transforming points from the world coordinate system to points in the camera coordinate systems, (world_T_camera), one pose for each group of 2D image points
89  * @param world_T_object The 6-DOF object transformation to be optimized, with orthonormal rotation matrix, must be valid
90  * @param objectPointGroupsA The groups of 3D object points to be projected into the camera image with first camera profile, one group for each camera pose, one 3D object point for each 2D image point visible in each camera frame
91  * @param objectPointGroupsB The groups of 3D object points to be projected into the camera image with second camera profile, one group for each camera pose, one 3D object point for each 2D image point visible in each camera frame
92  * @param imagePointGroupsA The groups of 2D image points, one group for each camera pose with first camera profile, each image point has a corresponding 3D object point
93  * @param imagePointGroupsB The groups of 2D image points, one group for each camera pose with second camera profile, each image point has a corresponding 3D object point
94  * @param optimized_world_T_object The resulting optimized 6-DOF object point transformation
95  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
96  * @param estimator The robust error estimator to be used
97  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
98  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
99  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator
100  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator
101  * @param intermediateErrors Optional resulting averaged pixel errors for each intermediate optimization step, in relation to the defined estimator
102  * @return True, if the optimization succeeded
103  * @see optimizeObjectTransformationStereoIF(), optimizeObjectTransformation().
104  */
105  static inline bool optimizeObjectTransformationStereo(const AnyCamera& cameraA, const AnyCamera& cameraB, const HomogenousMatrices4& world_T_camerasA, const HomogenousMatrices4& world_T_camerasB, const HomogenousMatrix4& world_T_object, const ObjectPointGroups& objectPointGroupsA, const ObjectPointGroups& objectPointGroupsB, const ImagePointGroups& imagePointGroupsA, const ImagePointGroups& imagePointGroupsB, HomogenousMatrix4& optimized_world_T_object, const unsigned int iterations = 20u, const Estimator::EstimatorType estimator = Estimator::ET_SQUARE, Scalar lambda = Scalar(0.001), const Scalar lambdaFactor = Scalar(5), Scalar* initialError = nullptr, Scalar* finalError = nullptr, Scalars* intermediateErrors = nullptr);
106 
107  /**
108  * Minimizes the projection error for several 3D object points projected into several stereo camera images via a 6-DOF object transformation (to be optimized).
109  * This function applies the same optimization as optimizeObjectTransformation() while uses 6-DOF camera poses with inverted and flipped coordinate system.
110  * @see optimizeObjectTransformation().
111  */
112  static bool optimizeObjectTransformationStereoIF(const AnyCamera& cameraA, const AnyCamera& cameraB, const HomogenousMatrices4& flippedCamerasA_T_world, const HomogenousMatrices4& flippedCamerasB_T_world, const HomogenousMatrix4& world_T_object, const ObjectPointGroups& objectPointGroupsA, const ObjectPointGroups& objectPointGroupsB, const ImagePointGroups& imagePointGroupsA, const ImagePointGroups& imagePointGroupsB, HomogenousMatrix4& optimized_world_T_object, const unsigned int iterations = 20u, const Estimator::EstimatorType estimator = Estimator::ET_SQUARE, Scalar lambda = Scalar(0.001), const Scalar lambdaFactor = Scalar(5), Scalar* initialError = nullptr, Scalar* finalError = nullptr, Scalars* intermediateErrors = nullptr);
113 };
114 
115 inline bool NonLinearOptimizationTransformation::optimizeObjectTransformation(const AnyCamera& camera, const HomogenousMatrices4& world_T_cameras, const HomogenousMatrix4& world_T_object, const ObjectPointGroups& objectPointGroups, const ImagePointGroups& imagePointGroups, HomogenousMatrix4& optimized_world_T_object, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, Scalars* intermediateErrors)
116 {
117  return optimizeObjectTransformationIF(camera, PinholeCamera::standard2InvertedFlipped(world_T_cameras), world_T_object, objectPointGroups, imagePointGroups, optimized_world_T_object, iterations, estimator, lambda, lambdaFactor, initialError, finalError, intermediateErrors);
118 }
119 
120 inline bool NonLinearOptimizationTransformation::optimizeObjectTransformationStereo(const AnyCamera& cameraA, const AnyCamera& cameraB, const HomogenousMatrices4& world_T_camerasA, const HomogenousMatrices4& world_T_camerasB, const HomogenousMatrix4& world_T_object, const ObjectPointGroups& objectPointGroupsA, const ObjectPointGroups& objectPointGroupsB, const ImagePointGroups& imagePointGroupsA, const ImagePointGroups& imagePointGroupsB, HomogenousMatrix4& optimized_world_T_object, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, Scalars* intermediateErrors)
121 {
122  return optimizeObjectTransformationStereoIF(cameraA, cameraB, PinholeCamera::standard2InvertedFlipped(world_T_camerasA), PinholeCamera::standard2InvertedFlipped(world_T_camerasB), world_T_object, objectPointGroupsA, objectPointGroupsB, imagePointGroupsA, imagePointGroupsB, optimized_world_T_object, iterations, estimator, lambda, lambdaFactor, initialError, finalError, intermediateErrors);
123 }
124 
125 }
126 
127 }
128 
129 #endif // META_OCEAN_GEOMETRY_NON_LINEAR_OPTIMIZATION_TRANSFORMATION_H
This class implements the abstract base class for all AnyCamera objects.
Definition: AnyCamera.h:130
static HomogenousMatrixT4< U > standard2InvertedFlipped(const HomogenousMatrixT4< U > &world_T_camera)
Transforms a standard homogenous 4x4 viewing (extrinsic camera) matrix into an inverted and flipped c...
Definition: Camera.h:734
EstimatorType
Definition of individual robust estimator types.
Definition: Estimator.h:34
@ ET_SQUARE
The standard square error estimator (L2).
Definition: Estimator.h:52
This class implements the basic functions for least square or robust optimization algorithms for non ...
Definition: NonLinearOptimization.h:34
This class implements non linear optimization algorithms for coordinate transformations.
Definition: NonLinearOptimizationTransformation.h:28
static bool optimizeObjectTransformation(const AnyCamera &camera, const HomogenousMatrices4 &world_T_cameras, const HomogenousMatrix4 &world_T_object, const ObjectPointGroups &objectPointGroups, const ImagePointGroups &imagePointGroups, HomogenousMatrix4 &optimized_world_T_object, const unsigned int iterations=20u, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, Scalars *intermediateErrors=nullptr)
Minimizes the projection error for several 3D object points projected into several camera images via ...
Definition: NonLinearOptimizationTransformation.h:115
static bool optimizeObjectTransformationIF(const AnyCamera &camera, const HomogenousMatrices4 &flippedCameras_T_world, const HomogenousMatrix4 &world_T_object, const ObjectPointGroups &objectPointGroups, const ImagePointGroups &imagePointGroups, HomogenousMatrix4 &optimized_world_T_object, const unsigned int iterations=20u, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, Scalars *intermediateErrors=nullptr)
Minimizes the projection error for several 3D object points projected into several camera images via ...
static bool optimizeObjectTransformationStereo(const AnyCamera &cameraA, const AnyCamera &cameraB, const HomogenousMatrices4 &world_T_camerasA, const HomogenousMatrices4 &world_T_camerasB, const HomogenousMatrix4 &world_T_object, const ObjectPointGroups &objectPointGroupsA, const ObjectPointGroups &objectPointGroupsB, const ImagePointGroups &imagePointGroupsA, const ImagePointGroups &imagePointGroupsB, HomogenousMatrix4 &optimized_world_T_object, const unsigned int iterations=20u, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, Scalars *intermediateErrors=nullptr)
Minimizes the projection error for several 3D object points projected into several stereo camera imag...
Definition: NonLinearOptimizationTransformation.h:120
static bool optimizeObjectTransformationStereoIF(const AnyCamera &cameraA, const AnyCamera &cameraB, const HomogenousMatrices4 &flippedCamerasA_T_world, const HomogenousMatrices4 &flippedCamerasB_T_world, const HomogenousMatrix4 &world_T_object, const ObjectPointGroups &objectPointGroupsA, const ObjectPointGroups &objectPointGroupsB, const ImagePointGroups &imagePointGroupsA, const ImagePointGroups &imagePointGroupsB, HomogenousMatrix4 &optimized_world_T_object, const unsigned int iterations=20u, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, Scalars *intermediateErrors=nullptr)
Minimizes the projection error for several 3D object points projected into several stereo camera imag...
std::vector< ObjectPoints > ObjectPointGroups
Definition of a vector holding object points, so we have groups of object points.
Definition: geometry/Geometry.h:135
std::vector< ImagePoints > ImagePointGroups
Definition of a vector holding image points, so we have groups of image points.
Definition: geometry/Geometry.h:141
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< HomogenousMatrix4 > HomogenousMatrices4
Definition of a vector holding HomogenousMatrix4 objects.
Definition: HomogenousMatrix4.h:73
std::vector< Scalar > Scalars
Definition of a vector holding Scalar objects.
Definition: Math.h:144
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15