Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AbsoluteOrientationRANSAC.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 _AbsoluteOrientationRANSAC_hh_
26 #define _AbsoluteOrientationRANSAC_hh_
27 
28 #include <bias_config.h>
29 #include "AbsoluteOrientation.hh"
30 #include <Base/Math/Random.hh>
31 #include <Base/Math/Vector3.hh>
32 #include <Geometry/RMatrix.hh>
33 #include <vector>
34 
35 namespace BIAS {
36 
37 /** @class AbsoluteOrientationRANSAC
38  @brief Robust computation of similarity transformation between two
39  3D point sets using a RANSAC approach.
40  @see #AbsoluteOrientation
41  @author esquivel
42  @date 12/2008
43 */
44  class BIASGeometry_EXPORT AbsoluteOrientationRANSAC
45  {
46 
47  public:
48 
50 
52 
53  /** @brief Compute max. error for statistical outlier detection from
54  given significance level.
55  @note Significance defines the probability that an inlier is regarded
56  as an outlier ("type I error"). This should typically be a very small
57  value (below 0.05 resp. 5%). The max. error is computes by integrating
58  the chi square distribution.
59  */
60  void SetSignificance(double significance);
61 
62  /** @brief Set max. error for heuristical outlier detection.
63  @note Note that max. squared (and weighted) error is set here! */
64  inline void SetMaxError(double error) { maxError_ = error; };
65 
66  /** @brief Set max. error angle for heuristical outlier detection for
67  pure absolute rotation estimation.
68  @param Max. angle between input ray and estimated ray in radians.
69  @note Use only previous to ComputeRotation() without weights! */
70  inline void SetMaxErrorAngle(double angle)
71  { maxError_ = 2.0*sin(0.5*angle); maxError_ *= maxError_; };
72 
73  /** @brief Set min. inlier ratio for termination. */
74  inline void SetMinRatio(double ratio) { minRatio_ = ratio; };
75 
76  /** @brief Set number of random samples to draw from data. */
77  inline void SetNumSamples(unsigned int n) { numSamples_ = n; };
78 
79  /** @brief Use greedy search for good solution? */
80  inline void SetGreedy(bool greedy) { greedy_ = greedy; };
81 
82  /** @brief Refine final solution with all inliers? */
83  inline void SetRefine(bool refine) { refine_ = refine; };
84 
85  /** @brief Use residual errors instead of inlier count as target function? */
86  inline void SetWeightedTargetFunction(bool use)
87  { useWeightedTargetFunction_ = use; };
88 
89  /** @brief Set algorithm used for rotation estimation. */
91  { solver_.SetRotationAlgorithm(algorithm); };
92 
93  /** @brief Computes rotation dR, translation dt and isometric scale ds
94  between coordinate systems from corresponding 3D point sets X and Y
95  using heuristical outlier detection.
96  Returns inlier flags and error residuals for each correspondence.
97  @return -1 if computation failed, else 0.
98  */
99  inline int Compute(const std::vector< Vector3<double> > &X,
100  const std::vector< Vector3<double> > &Y,
101  RMatrix &dR, Vector3<double> &dt, double &ds,
102  std::vector<bool> &inliers,
103  std::vector<double> &errors, double &errorSum,
104  const std::vector<double> &w = std::vector<double>(0))
105  {
106  const std::vector< Matrix3x3<double> > dummyCov(0);
107  return Compute_(X, Y, dummyCov, dummyCov, dR, dt, ds, false,
108  inliers, errors, errorSum, w);
109  };
110 
111  /** @brief Computes rotation dR, translation dt and isometric scale ds
112  between coordinate systems from corresponding 3D point sets X and Y
113  with covariances CovX, CovY using statistical outlier detection.
114  Returns inlier flags and error residuals for each correspondence.
115  @return -1 if computation failed, else 0.
116  */
117  inline int Compute(const std::vector< Vector3<double> > &X,
118  const std::vector< Vector3<double> > &Y,
119  const std::vector< Matrix3x3<double> > &CovX,
120  const std::vector< Matrix3x3<double> > &CovY,
121  RMatrix &dR, Vector3<double> &dt, double &ds,
122  std::vector<bool> &inliers,
123  std::vector<double> &errors, double &errorSum)
124  {
125  return Compute_(X, Y, CovX, CovY, dR, dt, ds, false,
126  inliers, errors, errorSum);
127  };
128 
129  /** @brief Computes rotation dR and translation dt between coordinate
130  systems from corresponding 3D point sets X and Y using heuristical
131  outlier detection.
132  Returns inlier flags and residual errors for each correspondence.
133  @return -1 if computation failed, else 0.
134  */
135  inline int Compute(const std::vector< Vector3<double> > &X,
136  const std::vector< Vector3<double> > &Y,
137  RMatrix &dR, Vector3<double> &dt,
138  std::vector<bool> &inliers,
139  std::vector<double> &errors, double &errorSum,
140  const std::vector<double> &w = std::vector<double>(0))
141  {
142  double ds = 1.0;
143  const std::vector< Matrix3x3<double> > dummyCov(0);
144  return Compute_(X, Y, dummyCov, dummyCov, dR, dt, ds, true,
145  inliers, errors, errorSum, w);
146  };
147 
148  /** @brief Computes rotation dR and translation dt between coordinate
149  systems from corresponding 3D point sets X and Y with covariances
150  CovX, CovY using statistical outlier detection.
151  Returns inlier flags and residual errors for each correspondence.
152  @return -1 if computation failed, else 0.
153  */
154  inline int Compute(const std::vector< Vector3<double> > &X,
155  const std::vector< Vector3<double> > &Y,
156  const std::vector< Matrix3x3<double> > &CovX,
157  const std::vector< Matrix3x3<double> > &CovY,
158  RMatrix &dR, Vector3<double> &dt,
159  std::vector<bool> &inliers,
160  std::vector<double> &errors, double &errorSum)
161  {
162  double ds = 1.0;
163  return Compute_(X, Y, CovX, CovY, dR, dt, ds, true,
164  inliers, errors, errorSum);
165  };
166 
167  /** @brief Computes rotation dR between corresponding 3D ray sets X and Y
168  using heuristical outlier detection.
169  Returns inlier flags and error residuals for each correspondence.
170  @return -1 if computation failed, else 0.
171  */
172  inline int ComputeRotation(const std::vector< Vector3<double> > &X,
173  const std::vector< Vector3<double> > &Y,
174  RMatrix &dR, std::vector<bool> &inliers,
175  std::vector<double> &errors, double &errorSum,
176  const std::vector<double> &w = std::vector<double>(0))
177  {
178  const std::vector< Matrix3x3<double> > dummyCov(0);
179  return ComputeRotation_(X, Y, dummyCov, dummyCov, dR,
180  inliers, errors, errorSum, w);
181  };
182 
183  /** @brief Computes rotation dR between corresponding 3D ray sets X and Y
184  with covariances CovX, CovY using statistical outlier detection.
185  Returns inlier flags and error residuals for each correspondence.
186  @return -1 if computation failed, else 0.
187  */
188  inline int ComputeRotation(const std::vector< Vector3<double> > &X,
189  const std::vector< Vector3<double> > &Y,
190  const std::vector< Matrix3x3<double> > &CovX,
191  const std::vector< Matrix3x3<double> > &CovY,
192  RMatrix &dR, std::vector<bool> &inliers,
193  std::vector<double> &errors, double &errorSum,
194  const std::vector<double> &w = std::vector<double>(0))
195  {
196  return ComputeRotation_(X, Y, CovX, CovY, dR, inliers, errors, errorSum, w);
197  };
198 
199  /** @brief Computes residual errors for inliers of 3D point sets X and Y with
200  respect to given rotation dR, translation dt and isometric scale ds.
201  @note Each error is given by (weighted) Euclidean distance dist(X',Y)
202  with X' = ds*dR*X - dt for corresponding 3D points X and Y.
203  @return Sum of squared errors dist(X',Y) with X' = ds*dR*X - dt
204  as well as residual error for each single correspondence.
205  */
206  double GetResidualErrors(std::vector<double> &errors,
207  const std::vector< Vector3<double> > &X,
208  const std::vector< Vector3<double> > &Y,
209  const RMatrix &dR, const Vector3<double> &dt,
210  const double &ds = 1.0,
211  const std::vector<bool> &inliers
212  = std::vector<bool>(0),
213  const std::vector<double> &w
214  = std::vector<double>(0));
215 
216  /** @brief Computes residual errors for inliers of 3D point sets X and Y
217  with covariances CovX, CovY with respect to given rotation dR,
218  translation dt and isometric scale ds.
219  @note Computes Mahalanobis distances instead of Euclidean distances!
220  @return Sum of squared errors mahalanobis(X',Y) with X' = ds*dR*X - dt
221  as well as residual error for each single correspondence.
222  */
223  double GetResidualErrors(std::vector<double> &errors,
224  const std::vector< Vector3<double> > &X,
225  const std::vector< Vector3<double> > &Y,
226  const std::vector< Matrix3x3<double> > &CovX,
227  const std::vector< Matrix3x3<double> > &CovY,
228  const RMatrix &dR, const Vector3<double> &dt,
229  const double &ds = 1.0,
230  const std::vector<bool> &inliers
231  = std::vector<bool>(0));
232 
233  /** @brief Computes residual errors for inliers of 3D point sets X and Y
234  with respect to given rotation dR.
235  @note Each error is given by (weighted) Euclidean distance dist(X',Y)
236  with X' = dR*X for corresponding 3D points X and Y.
237  @return Sum of squared errors dist(X',Y) with X' = dR*X
238  as well as residual error for each single correspondence.
239  */
240  double GetResidualErrors(std::vector<double> &errors,
241  const std::vector< Vector3<double> > &X,
242  const std::vector< Vector3<double> > &Y,
243  const RMatrix &dR,
244  const std::vector<bool> &inliers
245  = std::vector<bool>(0),
246  const std::vector<double> &w
247  = std::vector<double>(0));
248 
249  /** @brief Computes residual errors for inliers of 3D point sets X and Y
250  with covariances CovX, CovY with respect to given rotation dR.
251  @note Computes Mahalanobis distances instead of Euclidean distances!
252  @return Sum of squared errors mahalanobis(X',Y) with X' = dR*X
253  as well as residual error for each single correspondence.
254  */
255  double GetResidualErrors(std::vector<double> &errors,
256  const std::vector< Vector3<double> > &X,
257  const std::vector< Vector3<double> > &Y,
258  const std::vector< Matrix3x3<double> > &CovX,
259  const std::vector< Matrix3x3<double> > &CovY,
260  const RMatrix &dR,
261  const std::vector<bool> &inliers
262  = std::vector<bool>(0));
263 
264  private:
265 
266  /// @brief Estimator and refiner for absolute orientation problem
267  AbsoluteOrientation solver_;
268 
269  /// @brief Random sample generator
270  Random randomizer_;
271 
272  /// @brief RANSAC parameters
273  const unsigned int sampleSize_;
274  unsigned int numSamples_;
275  double maxError_;
276  double minRatio_;
277  bool refine_;
278  bool greedy_;
279 
280  /// @brief Use residual error sum instead of inlier count as target function?
281  bool useWeightedTargetFunction_;
282 
283  /** @brief Computes rotation dR, translation dt and isometric scale ds
284  between coordinate systems from corresponding 3D point sets X and Y
285  using heuristical outlier detection or statistical outlier detection
286  if covariances CovX, CovY are given.
287  @note Given scale is optionally enforced instead of being computed.
288  @note Error term for each residual is weighted by w optionally for
289  heuristical outlier detection.
290  @return -1 if computation failed, else 0.
291  */
292  int Compute_(const std::vector< Vector3<double> > &X,
293  const std::vector< Vector3<double> > &Y,
294  const std::vector< Matrix3x3<double> > &CovX,
295  const std::vector< Matrix3x3<double> > &CovY,
296  RMatrix &dR, Vector3<double> &dt,
297  double &ds, bool enforceScale, std::vector<bool> &inliers,
298  std::vector<double> &errors, double &errorSum,
299  const std::vector<double> &w = std::vector<double>(0));
300 
301  /** @brief Computes rotation dR between corresponding 3D ray sets X and Y
302  using heuristical outlier detection or statistical outlier detection
303  if covariances CovX, CovY are given.
304  Returns inlier flags and error residuals for each correspondence.
305  @return -1 if computation failed, else 0.
306  */
307  int ComputeRotation_(const std::vector< Vector3<double> > &X,
308  const std::vector< Vector3<double> > &Y,
309  const std::vector< Matrix3x3<double> > &CovX,
310  const std::vector< Matrix3x3<double> > &CovY,
311  RMatrix &dR, std::vector<bool> &inliers,
312  std::vector<double> &errors, double &errorSum,
313  const std::vector<double> &w = std::vector<double>(0));
314 
315  /** @brief Draw random sample of given size from 3D point sets X, Y
316  with optional weights w and covariances CovX, CovY.
317  @return -1 if computation failed, else size of sample.
318  */
319  int CreateRandomSample_(unsigned int samplesize,
320  const std::vector< Vector3<double> > &X,
321  const std::vector< Vector3<double> > &Y,
322  const std::vector< Matrix3x3<double> > &CovX,
323  const std::vector< Matrix3x3<double> > &CovY,
324  const std::vector<double> &w,
325  std::vector<int> &samples,
326  std::vector< Vector3<double> > &sampleX,
327  std::vector< Vector3<double> > &sampleY,
328  std::vector< Matrix3x3<double> > &sampleCovX,
329  std::vector< Matrix3x3<double> > &sampleCovY,
330  std::vector<double> &samplew);
331 
332  /** @brief Determine inliers for 3D point sets X and Y with respect to
333  given rotation dR, translation dt and scale ds heuristically or
334  statistically if covariances CovX, CovY are given.
335  @note The incidence test for statistical outlier detection is based
336  on the chi square test which relates the H_0 hypothesis (identical)
337  against the H_1 hypothesis (different) by fixing the significance
338  level of the test, i.e. the probability that H_0 is rejected even
339  though it is true ("type I error").
340  @return Number of inliers found.
341  */
342  int EvaluateInliers_(std::vector<bool> &inliers,
343  std::vector<double> &errors, double &errorSum,
344  const std::vector< Vector3<double> > &X,
345  const std::vector< Vector3<double> > &Y,
346  const std::vector< Matrix3x3<double> > &CovX,
347  const std::vector< Matrix3x3<double> > &CovY,
348  const RMatrix &dR, const Vector3<double> &dt,
349  const double &ds, const std::vector<double> &w);
350 
351  /** @brief Determine inliers for 3D point sets X and Y with respect to
352  given rotation dR heuristically or statistically if covariances CovX,
353  CovY are given.
354  @return Number of inliers found.
355  */
356  int EvaluateInliers_(std::vector<bool> &inliers,
357  std::vector<double> &errors, double &errorSum,
358  const std::vector< Vector3<double> > &X,
359  const std::vector< Vector3<double> > &Y,
360  const std::vector< Matrix3x3<double> > &CovX,
361  const std::vector< Matrix3x3<double> > &CovY,
362  const RMatrix &dR, const std::vector<double> &w);
363 
364  };
365 
366 }
367 #endif // _AbsoluteOrientationRANSAC_hh_
Robust computation of similarity transformation between two 3D point sets using a RANSAC approach...
Computes similarity transformation between 3D point sets.
int Compute(const std::vector< Vector3< double > > &X, const std::vector< Vector3< double > > &Y, RMatrix &dR, Vector3< double > &dt, std::vector< bool > &inliers, std::vector< double > &errors, double &errorSum, const std::vector< double > &w=std::vector< double >(0))
Computes rotation dR and translation dt between coordinate systems from corresponding 3D point sets X...
void SetWeightedTargetFunction(bool use)
Use residual errors instead of inlier count as target function?
int Compute(const std::vector< Vector3< double > > &X, const std::vector< Vector3< double > > &Y, RMatrix &dR, Vector3< double > &dt, double &ds, std::vector< bool > &inliers, std::vector< double > &errors, double &errorSum, const std::vector< double > &w=std::vector< double >(0))
Computes rotation dR, translation dt and isometric scale ds between coordinate systems from correspon...
3D rotation matrix
Definition: RMatrix.hh:49
void SetGreedy(bool greedy)
Use greedy search for good solution?
void SetMaxError(double error)
Set max.
void SetMaxErrorAngle(double angle)
Set max.
int Compute(const std::vector< Vector3< double > > &X, const std::vector< Vector3< double > > &Y, const std::vector< Matrix3x3< double > > &CovX, const std::vector< Matrix3x3< double > > &CovY, RMatrix &dR, Vector3< double > &dt, double &ds, std::vector< bool > &inliers, std::vector< double > &errors, double &errorSum)
Computes rotation dR, translation dt and isometric scale ds between coordinate systems from correspon...
void SetRotationAlgorithm(AbsoluteOrientation::RotationEstimationMethod algorithm)
Set algorithm used for rotation estimation.
void SetMinRatio(double ratio)
Set min.
int ComputeRotation(const std::vector< Vector3< double > > &X, const std::vector< Vector3< double > > &Y, const std::vector< Matrix3x3< double > > &CovX, const std::vector< Matrix3x3< double > > &CovY, RMatrix &dR, std::vector< bool > &inliers, std::vector< double > &errors, double &errorSum, const std::vector< double > &w=std::vector< double >(0))
Computes rotation dR between corresponding 3D ray sets X and Y with covariances CovX, CovY using statistical outlier detection.
int ComputeRotation(const std::vector< Vector3< double > > &X, const std::vector< Vector3< double > > &Y, RMatrix &dR, std::vector< bool > &inliers, std::vector< double > &errors, double &errorSum, const std::vector< double > &w=std::vector< double >(0))
Computes rotation dR between corresponding 3D ray sets X and Y using heuristical outlier detection...
RotationEstimationMethod
Methods for rotation estimation.
class for producing random numbers from different distributions
Definition: Random.hh:51
void SetRefine(bool refine)
Refine final solution with all inliers?
void SetNumSamples(unsigned int n)
Set number of random samples to draw from data.
int Compute(const std::vector< Vector3< double > > &X, const std::vector< Vector3< double > > &Y, const std::vector< Matrix3x3< double > > &CovX, const std::vector< Matrix3x3< double > > &CovY, RMatrix &dR, Vector3< double > &dt, std::vector< bool > &inliers, std::vector< double > &errors, double &errorSum)
Computes rotation dR and translation dt between coordinate systems from corresponding 3D point sets X...