Ocean
Loading...
Searching...
No Matches
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
15#include "ocean/math/Matrix.h"
18
19namespace Ocean
20{
21
22namespace Geometry
23{
24
25/**
26 * This class implements least square or robust optimization algorithms for orientations.
27 * @ingroup geometry
28 */
29class 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 (pure rotational) 3-DOF camera pose.
47 * @param camera The camera profile defining the projection between 3D object points and 2D image points, must be valid
48 * @param world_R_camera The 3-DOF camera pose to optimize, with default camera pointing towards the negative z-space, with y-axis upwards, must be valid
49 * @param objectPoints The accessor providing the 3D object points to be projected into the camera plane
50 * @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
51 * @param world_R_optimizedCamera The resulting optimized 3-DOF camera pose, with default camera pointing towards the negative z-space, with y-axis upwards
52 * @param iterations The number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
53 * @param estimator The robust error estimator to be used
54 * @param lambda The initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
55 * @param lambdaFactor The Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
56 * @param initialError Optional resulting averaged robust pixel error for the given initial parameters depending on the selected estimator
57 * @param finalError Optional resulting averaged robust pixel error for the final optimized parameters depending on the selected estimator
58 * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
59 * @param intermediateErrors Optional resulting intermediate (improving) errors
60 * @return True, if the optimization succeeded
61 * @see optimizeOrientationIF().
62 */
63 static inline bool optimizeOrientation(const AnyCamera& camera, const Quaternion& world_R_camera, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, Quaternion& 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);
64
65 /**
66 * Minimizes the projection error of a given (pure rotational) 3-DOF camera pose.
67 * @param camera The camera profile defining the projection between 3D object points and 2D image points, must be valid
68 * @param flippedCamera_R_world The inverted and flipped 3-DOF camera pose to optimize, with flipped camera pointing towards the positive z-space, with y-axis downwards, must be valid
69 * @param objectPoints The accessor providing the 3D object points to be projected into the camera plane
70 * @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
71 * @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
72 * @param optimizedFlippedCamera_R_world The resulting optimized flipped and inverted 3-DOF camera pose, with flipped camera pointing towards the positive z-space, with y-axis downwards
73 * @param iterations Number of iterations to be applied at most, if no convergence can be reached
74 * @param estimator Robust error estimator to be used
75 * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
76 * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
77 * @param initialError Optional resulting averaged robust pixel error for the given initial parameters depending on the selected estimator
78 * @param finalError Optional resulting averaged robust pixel error for the final optimized parameters depending on the selected estimator
79 * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
80 * @param intermediateErrors Optional resulting intermediate (improving) errors
81 * @return True, if succeeded
82 * @see optimizeOrientation().
83 */
84 static bool optimizeOrientationIF(const AnyCamera& camera, const Quaternion& flippedCamera_R_world, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, Quaternion& 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);
85
86 /**
87 * Minimizes the projection error of a given 3DOF orientation and the entire camera parameters (intrinsic and distortion).
88 * The given 3DOF orientation is the rotational part of a standard extrinsic camera matrix.<br>
89 * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
90 * @param world_R_camera 3DOF orientation to minimize the projection error for
91 * @param objectPoints The accessor providing the 3D object points to be projected into the camera plane
92 * @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
93 * @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
94 * @param world_R_optimizedCamera Resulting optimized 3DOF orientation
95 * @param optimizedCamera The resulting optimized camera with modified intrinsic camera parameters and distortion parameters
96 * @param iterations Number of iterations to be applied at most, if no convergence can be reached
97 * @param estimator Robust error estimator to be used
98 * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
99 * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
100 * @param initialError Optional resulting averaged robust pixel error for the given initial parameters depending on the selected estimator
101 * @param finalError Optional resulting averaged robust pixel error for the final optimized parameters depending on the selected estimator
102 * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
103 * @param intermediateErrors Optional resulting intermediate (improving) errors
104 * @return True, if the optimization succeeded
105 * @see optimizeOrientationIF().
106 */
107 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);
108
109 /**
110 * Minimizes the projection error of a given inverted and flipped 3DOF orientation and the entire camera parameters (intrinsic and distortion).
111 * Beware: The given inverted and flipped 3DOF orientation is not equivalent to the rotational part of a standard extrinsic camera matrix.<br>
112 * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
113 * @param flippedCamera_R_world 3DOF orientation to minimize the projection error for (inverted and flipped)
114 * @param objectPoints The accessor providing the 3D object points to be projected into the camera plane
115 * @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
116 * @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
117 * @param optimizedFlippedCamera_R_world Resulting optimized 3DOF orientation (inverted and flipped)
118 * @param optimizedCamera The resulting optimized camera with modified intrinsic camera parameters and distortion parameters
119 * @param iterations Number of iterations to be applied at most, if no convergence can be reached
120 * @param estimator Robust error estimator to be used
121 * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
122 * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
123 * @param initialError Optional resulting averaged robust pixel error for the given initial parameters depending on the selected estimator
124 * @param finalError Optional resulting averaged robust pixel error for the final optimized parameters depending on the selected estimator
125 * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
126 * @param intermediateErrors Optional resulting intermediate (improving) errors
127 * @return True, if the optimization succeeded
128 * @see optimizeOrientationIF().
129 */
130 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);
131};
132
133inline bool NonLinearOptimizationOrientation::optimizeOrientation(const AnyCamera& camera, const Quaternion& world_R_camera, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, Quaternion& 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)
134{
135 ocean_assert(objectPoints.size() >= 3u && objectPoints.size() >= imagePoints.size());
136
137 const Quaternion flippedCamera_R_world(AnyCamera::standard2InvertedFlipped(world_R_camera));
138
139 Quaternion optimizedFlippedCamera_R_world(false);
140 if (!optimizeOrientationIF(camera, flippedCamera_R_world, objectPoints, imagePoints, optimizedFlippedCamera_R_world, iterations, estimator, lambda, lambdaFactor, initialError, finalError, invertedCovariances, intermediateErrors))
141 {
142 return false;
143 }
144
145 world_R_optimizedCamera = AnyCamera::invertedFlipped2Standard(optimizedFlippedCamera_R_world);
146
147 return true;
148}
149
150inline 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)
151{
152 ocean_assert(objectPoints.size() >= 3u && objectPoints.size() >= imagePoints.size());
153
154 const SquareMatrix3 flippedCamera_R_world(PinholeCamera::standard2InvertedFlipped(world_R_camera));
155
156 SquareMatrix3 optimizedFlippedCamera_R_world;
157 if (!optimizeCameraOrientationIF(pinholeCamera, flippedCamera_R_world, objectPoints, imagePoints, distortImagePoints, optimizedFlippedCamera_R_world, optimizedCamera, iterations, estimator, lambda, lambdaFactor, initialError, finalError, invertedCovariances, intermediateErrors))
158 {
159 return false;
160 }
161
162 world_R_optimizedCamera = PinholeCamera::invertedFlipped2Standard(optimizedFlippedCamera_R_world);
163 return true;
164}
165
166}
167
168}
169
170#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
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 optimizeOrientationIF(const AnyCamera &camera, const Quaternion &flippedCamera_R_world, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, Quaternion &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 (pure rotational) 3-DOF camera pose.
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:150
static bool optimizeOrientation(const AnyCamera &camera, const Quaternion &world_R_camera, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, Quaternion &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 (pure rotational) 3-DOF camera pose.
Definition NonLinearOptimizationOrientation.h:133
float Scalar
Definition of a scalar type.
Definition Math.h:129
std::vector< Scalar > Scalars
Definition of a vector holding Scalar objects.
Definition Math.h:145
The namespace covering the entire Ocean framework.
Definition Accessor.h:15