Ocean
NonLinearOptimizationPose.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_POSE_H
9 #define META_OCEAN_GEOMETRY_NON_LINEAR_OPTIMIZATION_POSE_H
10 
13 
14 #include "ocean/math/AnyCamera.h"
17 
18 namespace Ocean
19 {
20 
21 namespace Geometry
22 {
23 
24 /**
25  * This class implements non linear optimization algorithms for camera poses.
26  * @ingroup geometry
27  */
28 class OCEAN_GEOMETRY_EXPORT NonLinearOptimizationPose : protected NonLinearOptimization
29 {
30  protected:
31 
32  /**
33  * Forward declaration of a class implementing a provider allowing to optimize a camera pose.
34  */
35  class PoseOptimizationProvider;
36 
37  /**
38  * Forward declaration of a class implementing an advanced provider allowing to optimize a pinhole camera pose.
39  */
40  class AdvancedPinholeCameraPoseOptimizationProvider;
41 
42  /**
43  * Forward declaration of a class implementing an advanced provider allowing to optimize any camera pose.
44  */
45  class AdvancedAnyCameraPoseOptimizationProvider;
46 
47  /**
48  * Forward declaration of a class implementing a provider allowing to optimize a camera pose together with a zoom factor.
49  */
50  class PoseZoomOptimizationProvider;
51 
52  public:
53 
54  /**
55  * Deprecated.
56  *
57  * Minimizes the projection error of a given 6DOF pose holding orientation and translation parameters for a pinhole camera.
58  * The given 6DOF pose is a standard extrinsic camera matrix.<br>
59  * Beware: There is another optimizePose() function with almost identical functionality/parameter layout.<br>
60  * However, this function here does not support covariance parameters and thus creates a smaller binary footprint.<br>
61  * In case binary size matters, try to use this function only, and do not mix the usage of both options.
62  * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points, must be valid
63  * @param world_T_camera 6DOF pose to minimized the projection error for, must be valid
64  * @param objectPoints 3D object points to be projected into the camera plane
65  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
66  * @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
67  * @param world_T_optimizedCamera Resulting optimized 6DOF pose
68  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
69  * @param estimator Robust error estimator to be used
70  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
71  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
72  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator
73  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator
74  * @return True, if the optimization succeeded
75  * @see optimizePoseIF().
76  */
77  static inline 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);
78 
79  /**
80  * Minimizes the projection error of a given 6DOF pose holding orientation and translation parameters for any camera.
81  * The given 6DOF pose is a standard extrinsic camera matrix.<br>
82  * @param anyCamera The camera profile defining the projection, must be valid
83  * @param world_T_camera 6DOF pose to minimized the projection error for, must be valid
84  * @param objectPoints 3D object points to be projected into the camera plane
85  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
86  * @param world_T_optimizedCamera Resulting optimized 6DOF pose
87  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
88  * @param estimator Robust error estimator to be used
89  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
90  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
91  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator
92  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator
93  * @return True, if the optimization succeeded
94  * @see optimizePoseIF().
95  */
96  static inline bool optimizePose(const AnyCamera& anyCamera, const HomogenousMatrix4& world_T_camera, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, 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);
97 
98  /**
99  * Minimizes the projection error of a given 6-DOF camera pose.
100  * Beware: There is another optimizePose() function with almost identical functionality/parameter layout.<br>
101  * However, this function here supports covariance parameters and thus creates a bigger binary footprint.
102  * @param camera The camera profile defining the projection, must be valid
103  * @param world_T_camera The camera pose to optimized, transforming camera points to world points, with default camera pointing towards the negative z-space with y-axis upwards, must be valid
104  * @param objectPoints 3D object points to be projected into the camera plane
105  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
106  * @param world_T_optimizedCamera The resulting optimized 6-DOF camera pose, transforming camera points to world points, with default camera pointing towards the negative z-space with y-axis upwards, must be valid
107  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
108  * @param estimator Robust error estimator to be used
109  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
110  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
111  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator, nullptr to avoid the usage of the return value
112  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator, nullptr to avoid the usage of the return value
113  * @param invertedCovariances Optional 2x2 inverted covariance matrices which represent the uncertainties of the image points, one for each image point (a (2*n)x2 matrix)
114  * @return True, if the optimization succeeded
115  * @see optimizePoseIF().
116  */
117  static inline bool optimizePose(const AnyCamera& camera, const HomogenousMatrix4& world_T_camera, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, HomogenousMatrix4& world_T_optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances);
118 
119  /**
120  * Deprecated.
121  *
122  * Minimizes the projection error of a given 6DOF pose holding orientation and translation parameters.
123  * The given 6DOF pose is a standard extrinsic camera matrix.<br>
124  * Beware: There is another optimizePose() function with almost identical functionality/parameter layout.<br>
125  * However, this function here supports covariance parameters and thus creates a bigger binary footprint.
126  * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
127  * @param world_T_camera 6DOF pose to minimized the projection error for, must be valid
128  * @param objectPoints 3D object points to be projected into the camera plane
129  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
130  * @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
131  * @param world_T_optimizedCamera Resulting optimized 6DOF pose
132  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
133  * @param estimator Robust error estimator to be used
134  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
135  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
136  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator, nullptr to avoid the usage of the return value
137  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator, nullptr to avoid the usage of the return value
138  * @param invertedCovariances Set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
139  * @return True, if the optimization succeeded
140  * @see optimizePoseIF().
141  */
142  static inline 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, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances);
143 
144  /**
145  * Minimizes the projection error of a given 6DOF pose holding orientation and translation parameters together with a flexible zoom.
146  * The given 6DOF pose is a standard extrinsic camera matrix.<br>
147  * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
148  * @param world_T_camera 6DOF pose to minimized the projection error for, must be valid
149  * @param zoom The scalar zoom factor matching to the given pose, with range (0, infinity)
150  * @param objectPoints 3D object points to be projected into the camera plane
151  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
152  * @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
153  * @param world_T_optimizedCamera Resulting optimized 6DOF pose
154  * @param optimizedZoom Resulting optimized zoom factor matching to the resulting optimized pose, with range (0, infinity)
155  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
156  * @param estimator Robust error estimator to be used
157  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
158  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
159  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator
160  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator
161  * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
162  * @return True, if the optimization succeeded
163  * @see optimizePoseIF().
164  */
165  static inline bool optimizePoseZoom(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& world_T_camera, const Scalar zoom, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, const bool distortImagePoints, HomogenousMatrix4& world_T_optimizedCamera, Scalar& optimizedZoom, 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, const Matrix* invertedCovariances = nullptr);
166 
167  /**
168  * Deprecated.
169  *
170  * Minimizes the projection error of a given inverted and flipped 6DOF pose holding orientation and translation parameters for a pinhole camera.
171  * Beware: The given inverted and flipped 6DOF pose is not equivalent to a standard extrinsic camera matrix.<br>
172  * Beware: There is another optimizePoseIF() function with almost identical functionality/parameter layout.<br>
173  * However, this function here does not support covariance parameters and thus creates a smaller binary footprint.<br>
174  * In case binary size matters, try to use this function only, and do not mix the usage of both options.
175  * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
176  * @param flippedCamera_T_world 6DOF pose to minimized the projection error for (inverted and flipped), must be valid
177  * @param objectPoints 3D object points to be projected into the camera plane
178  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
179  * @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
180  * @param optimizedInvertedFlippedPose Resulting optimized 6DOF pose (inverted and flipped)
181  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
182  * @param estimator Robust error estimator to be used
183  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
184  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
185  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator
186  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator
187  * @return True, if succeeded
188  * @see optimizePose().
189  */
190  static bool optimizePoseIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& flippedCamera_T_world, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, const bool distortImagePoints, HomogenousMatrix4& optimizedInvertedFlippedPose, 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);
191 
192  /**
193  * Minimizes the projection error of a given inverted and flipped 6DOF pose holding orientation and translation parameters for any camera.
194  * Beware: The given inverted and flipped 6DOF pose is not equivalent to a standard extrinsic camera matrix.
195  * @param anyCamera The camera profile defining the projection, must be valid
196  * @param flippedCamera_T_world 6DOF pose to minimized the projection error for (inverted and flipped), must be valid
197  * @param objectPoints 3D object points to be projected into the camera plane
198  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
199  * @param optimizedPose_flippedCamera_T_world Resulting optimized 6DOF pose (inverted and flipped)
200  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
201  * @param estimator Robust error estimator to be used
202  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
203  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
204  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator
205  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator
206  * @return True, if succeeded
207  * @see optimizePose().
208  */
209  static bool optimizePoseIF(const AnyCamera& anyCamera, const HomogenousMatrix4& flippedCamera_T_world, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, HomogenousMatrix4& optimizedPose_flippedCamera_T_world, 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);
210 
211  /**
212  * Minimizes the projection error of a given 6-DOF camera pose.
213  * Beware: There is another optimizePose() function with almost identical functionality/parameter layout.<br>
214  * However, this function here supports covariance parameters and thus creates a bigger binary footprint.
215  * @param camera The camera profile defining the projection, must be valid
216  * @param flippedCamera_T_world The inverted and flipped camera pose to optimized, transforming world points to flipped camera points, with default camera pointing towards the positive z-space with y-axis downwards, must be valid
217  * @param objectPoints 3D object points to be projected into the camera plane
218  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
219  * @param optimizedFlippedCamera_T_world The resulting optimized inverted and flipped camera pose, transforming world points to flipped camera points, with default camera pointing towards the positive z-space with y-axis downwards, must be valid
220  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
221  * @param estimator Robust error estimator to be used
222  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
223  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
224  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator, nullptr to avoid the usage of the return value
225  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator, nullptr to avoid the usage of the return value
226  * @param invertedCovariances Optional 2x2 inverted covariance matrices which represent the uncertainties of the image points, one for each image point (a (2*n)x2 matrix)
227  * @return True, if the optimization succeeded
228  * @see optimizePose().
229  */
230  static bool optimizePoseIF(const AnyCamera& camera, const HomogenousMatrix4& flippedCamera_T_world, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, HomogenousMatrix4& optimizedFlippedCamera_T_world, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances);
231 
232 
233  /**
234  * Deprecated.
235  *
236  * Minimizes the projection error of a given inverted and flipped 6DOF pose holding orientation and translation parameters.
237  * Beware: The given inverted and flipped 6DOF pose is not equivalent to a standard extrinsic camera matrix.<br>
238  * Beware: There is another optimizePoseIF() function with almost identical functionality/parameter layout.<br>
239  * However, this function here supports covariance parameters and thus creates a bigger binary footprint.
240  * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
241  * @param flippedCamera_T_world 6DOF pose to minimized the projection error for (inverted and flipped), must be valid
242  * @param objectPoints 3D object points to be projected into the camera plane
243  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
244  * @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
245  * @param optimizedInvertedFlippedPose Resulting optimized 6DOF pose (inverted and flipped)
246  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
247  * @param estimator Robust error estimator to be used
248  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
249  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
250  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator, nullptr to avoid the usage of the return value
251  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator, nullptr to avoid the usage of the return value
252  * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
253  * @return True, if succeeded
254  * @see optimizePose().
255  */
256  static bool optimizePoseIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& flippedCamera_T_world, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, const bool distortImagePoints, HomogenousMatrix4& optimizedInvertedFlippedPose, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances);
257 
258  /**
259  * Minimizes the projection error of a given inverted and flipped 6DOF pose holding orientation and translation parameters together with a flexible zoom.
260  * Beware: The given inverted and flipped 6DOF pose is not equivalent to a standard extrinsic camera matrix.<br>
261  * @param pinholeCamera The pinhole camera object defining the projection between 3D object points and 2D image points
262  * @param flippedCamera_T_world 6DOF pose to minimized the projection error for (inverted and flipped), must be valid
263  * @param zoom The scalar zoom factor matching to the given pose, with range (0, infinity)
264  * @param objectPoints 3D object points to be projected into the camera plane
265  * @param imagePoints 2D image points corresponding to the object points, the image points may be distorted or undistorted depending on the usage of the distortImagePoints state
266  * @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
267  * @param optimizedInvertedFlippedPose Resulting optimized 6DOF pose (inverted and flipped)
268  * @param optimizedZoom Resulting optimized zoom factor matching to the resulting optimized pose, with range (0, infinity)
269  * @param iterations Number of iterations to be applied at most, if no convergence can be reached, with range [1, infinity)
270  * @param estimator Robust error estimator to be used
271  * @param lambda Initial Levenberg-Marquardt damping value which may be changed after each iteration using the damping factor, with range [0, infinity)
272  * @param lambdaFactor Levenberg-Marquardt damping factor to be applied to the damping value, with range [1, infinity)
273  * @param initialError Optional resulting averaged pixel error for the given initial parameters, in relation to the defined estimator
274  * @param finalError Optional resulting averaged pixel error for the final optimized parameters, in relation to the defined estimator
275  * @param invertedCovariances Optional set of 2x2 inverted covariance matrices that represent the uncertainties of the image points (a 2*n x 2 matrix)
276  * @return True, if succeeded
277  * @see optimizePose().
278  */
279  static bool optimizePoseZoomIF(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& flippedCamera_T_world, const Scalar zoom, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, const bool distortImagePoints, HomogenousMatrix4& optimizedInvertedFlippedPose, Scalar& optimizedZoom, 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, const Matrix* invertedCovariances = nullptr);
280 };
281 
282 inline bool NonLinearOptimizationPose::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, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError)
283 {
284  ocean_assert(pinholeCamera.isValid());
285  ocean_assert(world_T_camera.isValid());
286  ocean_assert(objectPoints.size() >= 3);
287  ocean_assert(objectPoints.size() == imagePoints.size());
288 
289  const HomogenousMatrix4 flippedCamera_T_world(PinholeCamera::standard2InvertedFlipped(world_T_camera));
290 
291  HomogenousMatrix4 optimizedInvertedFlippedPose;
292  if (!optimizePoseIF(pinholeCamera, flippedCamera_T_world, objectPoints, imagePoints, distortImagePoints, optimizedInvertedFlippedPose, iterations, estimator, lambda, lambdaFactor, initialError, finalError))
293  {
294  return false;
295  }
296 
297  world_T_optimizedCamera = PinholeCamera::invertedFlipped2Standard(optimizedInvertedFlippedPose);
298  return true;
299 }
300 
301 inline bool NonLinearOptimizationPose::optimizePose(const AnyCamera& anyCamera, const HomogenousMatrix4& world_T_camera, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, HomogenousMatrix4& world_T_optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError)
302 {
303  ocean_assert(anyCamera.isValid());
304  ocean_assert(world_T_camera.isValid());
305  ocean_assert(objectPoints.size() >= 3);
306  ocean_assert(objectPoints.size() == imagePoints.size());
307 
308  const HomogenousMatrix4 flippedCamera_T_world(PinholeCamera::standard2InvertedFlipped(world_T_camera));
309 
310  HomogenousMatrix4 optimizedFlippedCamera_T_world;
311  if (!optimizePoseIF(anyCamera, flippedCamera_T_world, objectPoints, imagePoints, optimizedFlippedCamera_T_world, iterations, estimator, lambda, lambdaFactor, initialError, finalError))
312  {
313  return false;
314  }
315 
316  world_T_optimizedCamera = PinholeCamera::invertedFlipped2Standard(optimizedFlippedCamera_T_world);
317  return true;
318 }
319 
320 inline bool NonLinearOptimizationPose::optimizePose(const AnyCamera& camera, const HomogenousMatrix4& world_T_camera, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, HomogenousMatrix4& world_T_optimizedCamera, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances)
321 {
322  ocean_assert(camera.isValid());
323  ocean_assert(world_T_camera.isValid());
324 
325  ocean_assert(objectPoints.size() >= 3u);
326  ocean_assert(objectPoints.size() == imagePoints.size());
327 
328  const HomogenousMatrix4 flippedCamera_T_world(PinholeCamera::standard2InvertedFlipped(world_T_camera));
329 
330  HomogenousMatrix4 optimizedFlippedCamera_T_world(false);
331  if (!optimizePoseIF(camera, flippedCamera_T_world, objectPoints, imagePoints, optimizedFlippedCamera_T_world, iterations, estimator, lambda, lambdaFactor, initialError, finalError, invertedCovariances))
332  {
333  return false;
334  }
335 
336  world_T_optimizedCamera = PinholeCamera::invertedFlipped2Standard(optimizedFlippedCamera_T_world);
337  return true;
338 }
339 
340 inline bool NonLinearOptimizationPose::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, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances)
341 {
342  const AnyCameraPinhole anyCamera(PinholeCamera(pinholeCamera, distortImagePoints));
343 
344  return optimizePose(anyCamera, world_T_camera, objectPoints, imagePoints, world_T_optimizedCamera, iterations, estimator, lambda, lambdaFactor, initialError, finalError, invertedCovariances);
345 }
346 
347 inline bool NonLinearOptimizationPose::optimizePoseZoom(const PinholeCamera& pinholeCamera, const HomogenousMatrix4& world_T_camera, const Scalar zoom, const ConstIndexedAccessor<Vector3>& objectPoints, const ConstIndexedAccessor<Vector2>& imagePoints, const bool distortImagePoints, HomogenousMatrix4& world_T_optimizedCamera, Scalar& optimizedZoom, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar* initialError, Scalar* finalError, const Matrix* invertedCovariances)
348 {
349  ocean_assert(world_T_camera.isValid() && zoom > Numeric::eps());
350  ocean_assert(objectPoints.size() >= 3u);
351  ocean_assert(objectPoints.size() == imagePoints.size());
352 
353  const HomogenousMatrix4 flippedCamera_T_world(PinholeCamera::standard2InvertedFlipped(world_T_camera));
354 
355  HomogenousMatrix4 optimizedInvertedFlippedPose;
356  if (!optimizePoseZoomIF(pinholeCamera, flippedCamera_T_world, zoom, objectPoints, imagePoints, distortImagePoints, optimizedInvertedFlippedPose, optimizedZoom, iterations, estimator, lambda, lambdaFactor, initialError, finalError, invertedCovariances))
357  {
358  return false;
359  }
360 
361  world_T_optimizedCamera = PinholeCamera::invertedFlipped2Standard(optimizedInvertedFlippedPose);
362  return true;
363 }
364 
365 }
366 
367 }
368 
369 #endif // META_OCEAN_GEOMETRY_NON_LINEAR_OPTIMIZATION_POSE_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
virtual bool isValid() const =0
Returns whether this camera is valid.
This class implements a specialized AnyCamera object wrapping the actual camera model.
Definition: AnyCamera.h:494
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
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 camera poses.
Definition: NonLinearOptimizationPose.h:29
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
static bool optimizePoseIF(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &flippedCamera_T_world, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, const bool distortImagePoints, HomogenousMatrix4 &optimizedInvertedFlippedPose, 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.
static bool optimizePoseIF(const AnyCamera &camera, const HomogenousMatrix4 &flippedCamera_T_world, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, HomogenousMatrix4 &optimizedFlippedCamera_T_world, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar *initialError, Scalar *finalError, const Matrix *invertedCovariances)
Minimizes the projection error of a given 6-DOF camera pose.
static bool optimizePoseZoom(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &world_T_camera, const Scalar zoom, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, const bool distortImagePoints, HomogenousMatrix4 &world_T_optimizedCamera, Scalar &optimizedZoom, 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, const Matrix *invertedCovariances=nullptr)
Minimizes the projection error of a given 6DOF pose holding orientation and translation parameters to...
Definition: NonLinearOptimizationPose.h:347
static bool optimizePoseIF(const AnyCamera &anyCamera, const HomogenousMatrix4 &flippedCamera_T_world, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, HomogenousMatrix4 &optimizedPose_flippedCamera_T_world, 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)
Minimizes the projection error of a given inverted and flipped 6DOF pose holding orientation and tran...
static bool optimizePoseZoomIF(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &flippedCamera_T_world, const Scalar zoom, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, const bool distortImagePoints, HomogenousMatrix4 &optimizedInvertedFlippedPose, Scalar &optimizedZoom, 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, const Matrix *invertedCovariances=nullptr)
Minimizes the projection error of a given inverted and flipped 6DOF pose holding orientation and tran...
static bool optimizePoseIF(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &flippedCamera_T_world, const ConstIndexedAccessor< Vector3 > &objectPoints, const ConstIndexedAccessor< Vector2 > &imagePoints, const bool distortImagePoints, HomogenousMatrix4 &optimizedInvertedFlippedPose, const unsigned int iterations, const Estimator::EstimatorType estimator, Scalar lambda, const Scalar lambdaFactor, Scalar *initialError, Scalar *finalError, const Matrix *invertedCovariances)
Deprecated.
bool isValid() const
Returns whether this matrix is a valid homogeneous transformation.
Definition: HomogenousMatrix4.h:1806
static constexpr T eps()
Returns a small epsilon.
bool isValid() const
Returns whether this camera is valid.
Definition: PinholeCamera.h:1572
float Scalar
Definition of a scalar type.
Definition: Math.h:128
PinholeCameraT< Scalar > PinholeCamera
Definition of an pinhole camera object with Scalar precision.
Definition: PinholeCamera.h:32
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15