Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AbsoluteOrientation.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef _AbsoluteOrientation_hh_
26 #define _AbsoluteOrientation_hh_
27 
28 #include <bias_config.h>
29 #include <Base/Math/Vector3.hh>
30 #include <Base/Math/Matrix3x3.hh>
31 #include <Geometry/RMatrix.hh>
32 #include <vector>
33 
34 namespace BIAS {
35 
36 /** @class AbsoluteOrientation
37  @brief Computes similarity transformation between 3D point sets.
38 
39  Estimates the relationship between two coordinate systems using
40  pairs of measurements of the coordinates of a number of 3D points
41  in both systems. A SVD-based solution to the least-squares problem
42  for at least three points is used based on an approach proposed in:
43 
44  [1] B. Horn: Closed-form Solution of Absolute Orientation Using
45  Unit Quaternions, 1987.
46 
47  Another SVD-based solution estimating a rotation matrix directly
48  instead of using unit quaternions is given by Kabsch's algorithm:
49 
50  [2] W. Kabsch: A Solution for the Best Rotation to Relate Two Sets
51  of Vectors, 1976.
52 
53  @note The transformation between the coordinate systems is given
54  as rotation dR, translation dt, and isometric scale ds, such that
55  points X in the first coordinate system are related to points Y
56  in the second coordinate system by Y = ds*dR*X + dt.
57 
58  @todo Implement Horn's closed-form solution and compare with our
59  SVD-based approach!
60 
61  @author esquivel
62  @date 12/2008
63 */
64  class BIASGeometry_EXPORT AbsoluteOrientation
65  {
66 
68 
69  public:
70 
71  /// @brief Methods for rotation estimation
72  enum RotationEstimationMethod { HORN_ALGORITHM, KABSCH_ALGORITHM };
73 
74  static const unsigned int MIN_CORRESPONDENCES;
75 
77 
79 
80  /** @brief Computes rotation dR, translation dt and isometric scale ds
81  between coordinate systems from corresponding 3D point sets X and Y.
82  @return -1 if computation failed, else 0.
83  */
84  int Compute(const std::vector< Vector3<double> > &X,
85  const std::vector< Vector3<double> > &Y,
86  RMatrix &dR, Vector3<double> &dt, double &ds,
87  const std::vector<double> &w = std::vector<double>(0)) {
88  return Compute_(X, Y, dR, dt, ds, false, w);
89  };
90 
91  /** @brief Computes rotation dR and translation dt between coordinate
92  systems from corresponding 3D point sets X and Y.
93  @return -1 if computation failed, else 0.
94  */
95  int Compute(const std::vector< Vector3<double> > &X,
96  const std::vector< Vector3<double> > &Y,
97  RMatrix &dR, Vector3<double> &dt,
98  const std::vector<double> &w = std::vector<double>(0)) {
99  double ds = 1.0;
100  return Compute_(X, Y, dR, dt, ds, true, w);
101  };
102 
103  /** @brief Computes rotation dR between corresponding 3D ray sets X and Y
104  using the currently set algorithm.
105  @return -1 if computation failed, else 0.
106  */
107  int ComputeRotation(const std::vector< Vector3<double> > &X,
108  const std::vector< Vector3<double> > &Y,
109  RMatrix &dR,
110  const std::vector<double> &w = std::vector<double>(0));
111 
112  /** @brief Set algorithm used for rotation estimation. */
114  { rotationAlgorithm_ = algorithm; };
115 
116  /** @brief Computes residual errors of 3D point sets X and Y with
117  respect to given rotation dR, translation dt and isometric scale ds.
118  @note Each error is given by (weighted) Euclidean distance dist(X',Y)
119  with X' = ds*dR*X - dt for corresponding 3D points X and Y.
120  @return Sum of squared errors dist(X',Y) with X' = ds*dR*X - dt
121  and residual error for each single correspondence.
122  */
123  double GetResidualErrors(std::vector<double> &errors,
124  const std::vector< Vector3<double> > &X,
125  const std::vector< Vector3<double> > &Y,
126  const RMatrix &dR, const Vector3<double> &dt,
127  const double &ds = 1.0,
128  const std::vector<double> &w
129  = std::vector<double>(0));
130 
131  /** @brief Computes residual errors of 3D point sets X and Y with
132  covariances CovX, CovY with respect to given rotation dR,
133  translation dt and isometric scale ds.
134  @note Computes Mahalanobis distances instead of Euclidean distances!
135  @return Sum of squared errors mahalanobis(X',Y) with X' = ds*dR*X - dt
136  as well as residual error for each single correspondence.
137  */
138  double GetResidualErrors(std::vector<double> &errors,
139  const std::vector< Vector3<double> > &X,
140  const std::vector< Vector3<double> > &Y,
141  const std::vector< Matrix3x3<double> > &CovX,
142  const std::vector< Matrix3x3<double> > &CovY,
143  const RMatrix &dR, const Vector3<double> &dt,
144  const double &ds = 1.0);
145 
146  /** @brief Computes residual errors of 3D point sets X and Y with
147  respect to given rotation dR.
148  @note Each error is given by (weighted) Euclidean distance dist(X',Y)
149  with X' = dR*X for corresponding 3D points X and Y.
150  @return Sum of squared errors dist(X',Y) with X' = dR*X
151  and residual error for each single correspondence.
152  */
153  double GetResidualErrors(std::vector<double> &errors,
154  const std::vector< Vector3<double> > &X,
155  const std::vector< Vector3<double> > &Y,
156  const RMatrix &dR, const std::vector<double> &w
157  = std::vector<double>(0));
158 
159  /** @brief Computes residual errors of 3D point sets X and Y with
160  covariances CovX, CovY with respect to given rotation dR.
161  @note Computes Mahalanobis distances instead of Euclidean distances!
162  @return Sum of squared errors mahalanobis(X',Y) with X' = dR*X
163  as well as residual error for each single correspondence.
164  */
165  double GetResidualErrors(std::vector<double> &errors,
166  const std::vector< Vector3<double> > &X,
167  const std::vector< Vector3<double> > &Y,
168  const std::vector< Matrix3x3<double> > &CovX,
169  const std::vector< Matrix3x3<double> > &CovY,
170  const RMatrix &dR);
171 
172  private:
173 
174  /// @brief Threshold for comparison to zero
175  double epsilon_;
176 
177  /// @brief Method used for rotation estimation
178  RotationEstimationMethod rotationAlgorithm_;
179 
180  /** @brief Computes rotation dR, translation dt and isometric scale ds
181  between coordinate systems from corresponding 3D point sets X and Y.
182  @note Given scale is optionally enforced instead of being computed.
183  @note Error term for each residual is weighted by w optionally.
184  @return -1 if computation failed, else 0.
185  */
186  int Compute_(const std::vector< Vector3<double> > &X,
187  const std::vector< Vector3<double> > &Y,
188  RMatrix &dR, Vector3<double> &dt,
189  double &ds, bool enforceScale = false,
190  const std::vector<double> &w = std::vector<double>(0));
191 
192  /** @brief Computes rotation dR between corresponding 3D ray sets X and Y
193  using method proposed by Horn [1].
194  @return -1 if computation failed, else 0.
195  */
196  int ComputeRotationHorn_(const std::vector< Vector3<double> > &X,
197  const std::vector< Vector3<double> > &Y,
198  RMatrix &dR,
199  const std::vector<double> &w = std::vector<double>(0));
200 
201  /** @brief Computes rotation dR between corresponding 3D ray sets X and Y
202  using the Kabsch algorithm [2].
203  @return -1 if computation failed, else 0.
204  */
205  int ComputeRotationKabsch_(const std::vector< Vector3<double> > &X,
206  const std::vector< Vector3<double> > &Y,
207  RMatrix &dR,
208  const std::vector<double> &w = std::vector<double>(0));
209 
210  };
211 
212 }
213 #endif // _AbsoluteOrientation_hh_
Robust computation of similarity transformation between two 3D point sets using a RANSAC approach...
Computes similarity transformation between 3D point sets.
static const unsigned int MIN_CORRESPONDENCES
3D rotation matrix
Definition: RMatrix.hh:49
int Compute(const std::vector< Vector3< double > > &X, const std::vector< Vector3< double > > &Y, RMatrix &dR, Vector3< double > &dt, const std::vector< double > &w=std::vector< double >(0))
Computes rotation dR and translation dt between coordinate systems from corresponding 3D point sets X...
RotationEstimationMethod
Methods for rotation estimation.
void SetRotationAlgorithm(RotationEstimationMethod algorithm)
Set algorithm used for rotation estimation.
int Compute(const std::vector< Vector3< double > > &X, const std::vector< Vector3< double > > &Y, RMatrix &dR, Vector3< double > &dt, double &ds, const std::vector< double > &w=std::vector< double >(0))
Computes rotation dR, translation dt and isometric scale ds between coordinate systems from correspon...