Ocean
NonLinearOptimizationOrientation.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_ORIENTATION_H
9 #define META_OCEAN_GEOMETRY_NON_LINEAR_OPTIMIZATION_ORIENTATION_H
10 
13 
14 #include "ocean/math/AnyCamera.h"
15 #include "ocean/math/Matrix.h"
18 
19 namespace Ocean
20 {
21 
22 namespace Geometry
23 {
24 
25 /**
26  * This class implements least square or robust optimization algorithms for orientations.
27  * @ingroup geometry
28  */
29 class OCEAN_GEOMETRY_EXPORT NonLinearOptimizationOrientation : protected NonLinearOptimization
30 {
31  protected:
32 
33  /**
34  * Forward declaration of a provider object allowing to optimize an orientation.
35  */
36  class OrientationOptimizationProvider;
37 
38  /**
39  * Forward declaration of a provider object allowing to optimize an orientation and a camera profile.
40  */
41  class CameraOrientationOptimizationProvider;
42 
43  public:
44 
45  /**
46  * Minimizes the projection error of a given 3DOF orientation.
47  * The given 3DOF orientation is the rotational part of a standard extrinsic camera matrix.<br>
48  * @param camera The camera profile defining the projection, must be valid
49  * @param world_R_camera 3DOF pose to minimize the projection error for
50  * @param objectPoints The accessor providing the 3D object points to be projected into the camera plane
51  * @param imagePoints The accessor providing the 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
52  * @param world_R_optimizedCamera Resulting optimized 3DOF orientation
53  * @param iterations Number of iterations to be applied at most, if no convergence can be reached
54  * @param estimator Robust error estimator to be used
55  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
56  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
57  * @param initialError Optional resulting averaged robust pixel error for the given initial parameters depending on the selected estimator
58  * @param finalError Optional resulting averaged robust pixel error for the final optimized parameters depending on the selected estimator
59  * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
60  * @param intermediateErrors Optional resulting intermediate (improving) errors
61  * @return True, if the optimization succeeded
62  * @see optimizeOrientationIF().
63  */
64  static inline bool optimizeOrientation(const AnyCamera& camera, const SquareMatrix3& world_R_camera, const ConstIndexedAccessor<ObjectPoint>& objectPoints, const ConstIndexedAccessor<ImagePoint>& imagePoints, SquareMatrix3& world_R_optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator = Estimator::ET_SQUARE, const Scalar lambda = Scalar(0.001), const Scalar lambdaFactor = Scalar(5), Scalar* initialError = nullptr, Scalar* finalError = nullptr, const Matrix* invertedCovariances = nullptr, Scalars* intermediateErrors = nullptr);
65 
66  /**
67  * Minimizes the projection error of a given inverted and flipped 3DOF orientation.
68  * Beware: The given inverted and flipped 3DOF orientation is not equivalent to the rotational part of a standard extrinsic camera matrix.<br>
69  * @param camera The camera profile defining the projection, must be valid
70  * @param flippedCamera_R_world 3DOF orientation to minimize the projection error for (inverted and flipped)
71  * @param objectPoints The accessor providing the 3D object points to be projected into the camera plane
72  * @param imagePoints The accessor providing the 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
73  * @param optimizedFlippedCamera_R_world Resulting optimized 3DOF orientation (inverted and flipped)
74  * @param iterations Number of iterations to be applied at most, if no convergence can be reached
75  * @param estimator Robust error estimator to be used
76  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
77  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
78  * @param initialError Optional resulting averaged robust pixel error for the given initial parameters depending on the selected estimator
79  * @param finalError Optional resulting averaged robust pixel error for the final optimized parameters depending on the selected estimator
80  * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
81  * @param intermediateErrors Optional resulting intermediate (improving) errors
82  * @return True, if succeeded
83  * @see optimizeOrientation().
84  */
85  static bool optimizeOrientationIF(const AnyCamera& camera, const SquareMatrix3& flippedCamera_R_world, const ConstIndexedAccessor<ObjectPoint>& objectPoints, const ConstIndexedAccessor<ImagePoint>& imagePoints, SquareMatrix3& optimizedFlippedCamera_R_world, const unsigned int iterations, const Estimator::EstimatorType estimator = Estimator::ET_SQUARE, const Scalar lambda = Scalar(0.001), const Scalar lambdaFactor = Scalar(5), Scalar* initialError = nullptr, Scalar* finalError = nullptr, const Matrix* invertedCovariances = nullptr, Scalars* intermediateErrors = nullptr);
86 
87  /**
88  * Minimizes the projection error of a given 3DOF orientation and the entire camera parameters (intrinsic and distortion).
89  * The given 3DOF orientation is the rotational part of a standard extrinsic camera matrix.<br>
90  * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
91  * @param world_R_camera 3DOF orientation to minimize the projection error for
92  * @param objectPoints The accessor providing the 3D object points to be projected into the camera plane
93  * @param imagePoints The accessor providing the 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
94  * @param distortImagePoints True, to force the usage of the distortion parameters of the given camera object to distort the projected 2D image points before error determination
95  * @param world_R_optimizedCamera Resulting optimized 3DOF orientation
96  * @param optimizedCamera The resulting optimized camera with modified intrinsic camera parameters and distortion parameters
97  * @param iterations Number of iterations to be applied at most, if no convergence can be reached
98  * @param estimator Robust error estimator to be used
99  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
100  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
101  * @param initialError Optional resulting averaged robust pixel error for the given initial parameters depending on the selected estimator
102  * @param finalError Optional resulting averaged robust pixel error for the final optimized parameters depending on the selected estimator
103  * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
104  * @param intermediateErrors Optional resulting intermediate (improving) errors
105  * @return True, if the optimization succeeded
106  * @see optimizeOrientationIF().
107  */
108  static inline bool optimizeCameraOrientation(const PinholeCamera& pinholeCamera, const SquareMatrix3& world_R_camera, const ConstIndexedAccessor<ObjectPoint>& objectPoints, const ConstIndexedAccessor<ImagePoint>& imagePoints, const bool distortImagePoints, SquareMatrix3& world_R_optimizedCamera, PinholeCamera& optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator = Estimator::ET_SQUARE, const Scalar lambda = Scalar(0.001), const Scalar lambdaFactor = Scalar(5), Scalar* initialError = nullptr, Scalar* finalError = nullptr, const Matrix* invertedCovariances = nullptr, Scalars* intermediateErrors = nullptr);
109 
110  /**
111  * Minimizes the projection error of a given inverted and flipped 3DOF orientation and the entire camera parameters (intrinsic and distortion).
112  * Beware: The given inverted and flipped 3DOF orientation is not equivalent to the rotational part of a standard extrinsic camera matrix.<br>
113  * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
114  * @param flippedCamera_R_world 3DOF orientation to minimize the projection error for (inverted and flipped)
115  * @param objectPoints The accessor providing the 3D object points to be projected into the camera plane
116  * @param imagePoints The accessor providing the 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
117  * @param distortImagePoints True, to force the usage of the distortion parameters of the given camera object to distort the projected 2D image points before error determination
118  * @param optimizedFlippedCamera_R_world Resulting optimized 3DOF orientation (inverted and flipped)
119  * @param optimizedCamera The resulting optimized camera with modified intrinsic camera parameters and distortion parameters
120  * @param iterations Number of iterations to be applied at most, if no convergence can be reached
121  * @param estimator Robust error estimator to be used
122  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
123  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
124  * @param initialError Optional resulting averaged robust pixel error for the given initial parameters depending on the selected estimator
125  * @param finalError Optional resulting averaged robust pixel error for the final optimized parameters depending on the selected estimator
126  * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
127  * @param intermediateErrors Optional resulting intermediate (improving) errors
128  * @return True, if the optimization succeeded
129  * @see optimizeOrientationIF().
130  */
131  static bool optimizeCameraOrientationIF(const PinholeCamera& pinholeCamera, const SquareMatrix3& flippedCamera_R_world, const ConstIndexedAccessor<ObjectPoint>& objectPoints, const ConstIndexedAccessor<ImagePoint>& imagePoints, const bool distortImagePoints, SquareMatrix3& optimizedFlippedCamera_R_world, PinholeCamera& optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator = Estimator::ET_SQUARE, const Scalar lambda = Scalar(0.001), const Scalar lambdaFactor = Scalar(5), Scalar* initialError = nullptr, Scalar* finalError = nullptr, const Matrix* invertedCovariances = nullptr, Scalars* intermediateErrors = nullptr);
132 };
133 
134 inline bool NonLinearOptimizationOrientation::optimizeOrientation(const AnyCamera& camera, const SquareMatrix3& world_R_camera, const ConstIndexedAccessor<ObjectPoint>& objectPoints, const ConstIndexedAccessor<ImagePoint>& imagePoints, SquareMatrix3& world_R_optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator, const Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances, Scalars* intermediateErrors)
135 {
136  ocean_assert(objectPoints.size() >= 3u && objectPoints.size() >= imagePoints.size());
137 
138  const SquareMatrix3 flippedCamera_R_world(PinholeCamera::standard2InvertedFlipped(world_R_camera));
139 
140  SquareMatrix3 optimizedFlippedCamera_R_world;
141  if (!optimizeOrientationIF(camera, flippedCamera_R_world, objectPoints, imagePoints, optimizedFlippedCamera_R_world, iterations, estimator, lambda, lambdaFactor, initialError, finalError, invertedCovariances, intermediateErrors))
142  {
143  return false;
144  }
145 
146  world_R_optimizedCamera = PinholeCamera::invertedFlipped2Standard(optimizedFlippedCamera_R_world);
147 
148  return true;
149 }
150 
151 inline bool NonLinearOptimizationOrientation::optimizeCameraOrientation(const PinholeCamera& pinholeCamera, const SquareMatrix3& world_R_camera, const ConstIndexedAccessor<ObjectPoint>& objectPoints, const ConstIndexedAccessor<ImagePoint>& imagePoints, const bool distortImagePoints, SquareMatrix3& world_R_optimizedCamera, PinholeCamera& optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator, const Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances, Scalars* intermediateErrors)
152 {
153  ocean_assert(objectPoints.size() >= 3u && objectPoints.size() >= imagePoints.size());
154 
155  const SquareMatrix3 flippedCamera_R_world(PinholeCamera::standard2InvertedFlipped(world_R_camera));
156 
157  SquareMatrix3 optimizedFlippedCamera_R_world;
158  if (!optimizeCameraOrientationIF(pinholeCamera, flippedCamera_R_world, objectPoints, imagePoints, distortImagePoints, optimizedFlippedCamera_R_world, optimizedCamera, iterations, estimator, lambda, lambdaFactor, initialError, finalError, invertedCovariances, intermediateErrors))
159  {
160  return false;
161  }
162 
163  world_R_optimizedCamera = PinholeCamera::invertedFlipped2Standard(optimizedFlippedCamera_R_world);
164  return true;
165 }
166 
167 }
168 
169 }
170 
171 #endif // META_OCEAN_GEOMETRY_NON_LINEAR_OPTIMIZATION_ORIENTATION_H
virtual size_t size() const =0
Returns the number of accessible elements of this accessor object.
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
static HomogenousMatrixT4< U > invertedFlipped2Standard(const HomogenousMatrixT4< U > &flippedCamera_T_world)
Transforms an inverted and flipped camera pose into a standard camera pose.
Definition: Camera.h:757
This class implements a base class for all indexed-based accessors allowing a constant reference acce...
Definition: Accessor.h:241
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 least square or robust optimization algorithms for orientations.
Definition: NonLinearOptimizationOrientation.h:30
static bool optimizeCameraOrientationIF(const PinholeCamera &pinholeCamera, const SquareMatrix3 &flippedCamera_R_world, const ConstIndexedAccessor< ObjectPoint > &objectPoints, const ConstIndexedAccessor< ImagePoint > &imagePoints, const bool distortImagePoints, SquareMatrix3 &optimizedFlippedCamera_R_world, PinholeCamera &optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, const Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, const Matrix *invertedCovariances=nullptr, Scalars *intermediateErrors=nullptr)
Minimizes the projection error of a given inverted and flipped 3DOF orientation and the entire camera...
static bool optimizeCameraOrientation(const PinholeCamera &pinholeCamera, const SquareMatrix3 &world_R_camera, const ConstIndexedAccessor< ObjectPoint > &objectPoints, const ConstIndexedAccessor< ImagePoint > &imagePoints, const bool distortImagePoints, SquareMatrix3 &world_R_optimizedCamera, PinholeCamera &optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, const Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, const Matrix *invertedCovariances=nullptr, Scalars *intermediateErrors=nullptr)
Minimizes the projection error of a given 3DOF orientation and the entire camera parameters (intrinsi...
Definition: NonLinearOptimizationOrientation.h:151
static bool optimizeOrientation(const AnyCamera &camera, const SquareMatrix3 &world_R_camera, const ConstIndexedAccessor< ObjectPoint > &objectPoints, const ConstIndexedAccessor< ImagePoint > &imagePoints, SquareMatrix3 &world_R_optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, const Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, const Matrix *invertedCovariances=nullptr, Scalars *intermediateErrors=nullptr)
Minimizes the projection error of a given 3DOF orientation.
Definition: NonLinearOptimizationOrientation.h:134
static bool optimizeOrientationIF(const AnyCamera &camera, const SquareMatrix3 &flippedCamera_R_world, const ConstIndexedAccessor< ObjectPoint > &objectPoints, const ConstIndexedAccessor< ImagePoint > &imagePoints, SquareMatrix3 &optimizedFlippedCamera_R_world, const unsigned int iterations, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, const Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, const Matrix *invertedCovariances=nullptr, Scalars *intermediateErrors=nullptr)
Minimizes the projection error of a given inverted and flipped 3DOF orientation.
float Scalar
Definition of a scalar type.
Definition: Math.h:128
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