Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Triangulation.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
21 #ifndef __Triangulation_hh__
22 #define __Triangulation_hh__
23 #include <bias_config.h>
24 
25 //#include <Base/Common/BIASpragmaStart.hh>
26 
27 
28 #include <Base/Debug/Debug.hh>
29 #include <Base/Math/Matrix3x3.hh>
30 // #include <Base/Math/Matrix4x4.hh>
31 // #include <Base/Geometry/HomgPoint2DCov.hh>
32 // #include <Base/Geometry/HomgPoint3DCov.hh>
33 // #include "PMatrix.hh"
34 // #include "FMatrix.hh"
35 
36 
37 #include <vector>
38 
39 /// minimal length of difference between two unit vectors
40 /// before they are considered to be not parallel
41 #define PARALLEL_THRESHOLD 1e-8
42 
43 /// default step value left/right/up/down in pixels for triangulation
44 #define TRIANGULATION_DEFAULT_DELTAPIXEL 1.0
45 #define TRIANGULATION_DEFAULT_SIGMA 1.0
46 
47 #define TRIANG_RES_PARALLEL -42
48 #define TRIANG_RES_BEHIND_CAMERA1 1
49 #define TRIANG_RES_BEHIND_CAMERA2 2
50 #define TRIANG_RES_BEHIND_CAMERA 42
51 
52 namespace BIAS{
53 
54  // forward declarations
55  class BIASGeometry_EXPORT ProjectionParametersBase;
56  class BIASGeometry_EXPORT Pose;
57  class BIASGeometry_EXPORT FMatrix;
58  class BIASGeometry_EXPORT PMatrix;
59  class BIASGeometryBase_EXPORT HomgPoint2D;
60  class BIASGeometryBase_EXPORT HomgPoint2DCov;
61  class BIASGeometryBase_EXPORT HomgPoint3D;
62  class BIASGeometryBase_EXPORT HomgPoint3DCov;
63  template <class T> class BIASMathBase_EXPORT Matrix4x4;
64  template <class T> class BIASGeometryBase_EXPORT Quaternion;
65  template <class T> class BIASMathBase_EXPORT Vector3;
66 
67  /** @class Triangulation
68  * @test tested with TestTriangulation.cpp
69  @ingroup g_estimation
70  @brief Class for triangulation of 3Dpoints from 2D matches.
71  Covariance matrix (refering to an uncertainty ellipsoid) is computed also
72 
73  all functions so far only for 2 images (no trifocal).
74  @author: Felix Woelk and Jan Frahm */
75  class BIASGeometry_EXPORT Triangulation : public Debug
76  {
77  public:
78  Triangulation();
79 
80  ~Triangulation();
81 
82  /** @brief analytic correct intersection of two lines, point returned has
83  minimum distance to both lines.
84 
85  Lines have parametric description by point and direction.
86  IMPORTANT: directions MUST BE normalized!!
87  @return 0 in case of no errror, <0 if errors, 1 if intersection
88  point lies not in direction of dirB, 2 if intersection
89  point lies not in direction of dirA
90 
91  Refer to Kanatani Statistical Opt. for Geometric Comp. , page 108
92  @author grest, Sept. 2003 */
93  int Intersect(Vector3<double> &pA, Vector3<double> &dirA,
95  Vector3<double> &res);
96 
97  /** @brief analytic correct intersection of two lines, point returned has
98  minimum distance to both lines, distance between lines is returned in
99  dist.
100 
101  Lines have parametric description by point and direction.
102  IMPORTANT: directions MUST BE normalized!!
103  @return 0 in case of no errror, <0 if errors, 1 if intersection
104  point lies not in direction of dirB
105  @attention sanity check regarding dirA is not done !
106 
107  Refer to Kanatani Statistical Opt. for Geometric Comp. , page 108
108  @author grest, Sept. 2003 */
109  int Intersect(Vector3<double> &pA, Vector3<double> &dirA,
110  Vector3<double> &pB, Vector3<double> &dirB,
111  Vector3<double> &res, double &dist);
112 
113  /** @brief analytic correct intersection of two lines, point returned has
114  minimum distance to both lines.
115 
116  Line A is described by points a1,a2 and line B by b1,b2
117  Refer to Kanatani Statistical Opt. for Geometric Comp. ,page108
118  @author grest, Sept. 2003 */
121 
122  /** method from Hartley Zisserman chapter 12.5 pp 315
123  first uses CorrectCorrespondences()
124  and then calls TriangulateLinear()
125  @author woelk 08/2004 */
126  int Optimal(PMatrix &P1, PMatrix &P2, HomgPoint2D& p1, HomgPoint2D& p2,
127  HomgPoint3D& point3d);
128 
129  /** @brief optimize 2d correspondences regarding F for triangulation
130 
131  In projective reconstruction you have to optimize the
132  correspondences regarding epipolar geometry for the 3d point to
133  be projectively optimal before you use TriangulateLinear()
134  See section Hartley/Zisserman "Multiple View Geometry",
135  section "optimal triangulation", section 12.5, pp.315
136 
137  if refine==true, a non linear optimization for the real roots of the
138  6 degree polynomial is done using the powell algorithm from minpack
139 
140  Hereby F maps point from image 1 into lines in image 2, i.e. F_{12}.
141 
142  @author woelk 08/2004 heavily tested (ExampleTriangulateOptimal) */
143  int CorrectCorrespondences(const FMatrix& F, HomgPoint2D& p1,
144  HomgPoint2D& p2, bool refine=false);
145 
146  /** @brief Triangulates a 3d point without decomposition of P matrix, use
147  this method in selfcalibration scenarios
148 
149  Homogeneous DLT algorithm using SVD,
150  see Hartley/Zisserman: "Multiple View Geometry", p.297
151  For projective reconstructions first optimize 2d points using
152  CorrectCorrespondences() regarding epipolar geometry
153 
154  @attention NOT YET TESTED AT ALL, since CorrectCorrespondences missing
155  @author koeser, 10/2003*/
156  int TriangulateLinear(PMatrix &P1, PMatrix &P2,
157  HomgPoint2D& p1, HomgPoint2D& p2,
158  HomgPoint3D& point3d);
159 
160  /** @brief Triangulates a 3d point without decomposition of P matrix, use
161  this method in selfcalibration scenarios
162 
163  Homogeneous DLT algorithm using SVD,
164  see Hartley/Zisserman: "Multiple View Geometry", p.297
165  For projective reconstructions first optimize 2d points using
166  CorrectCorrespondences() regarding epipolar geometry
167 
168  @attention tested with only some examples
169  @author cmenk, 10/2006*/
170  int TriangulateLinear(std::vector<PMatrix> pmatrices,
171  std::vector<HomgPoint2D> homgPoints2D,
172  HomgPoint3D& point3d);
173 
174  /** @brief Triangulation for metric PMatrices (using C and Hinf)
175  @attention Decomposition of PMatrix is used !
176  @param point3d (output) euclidean 3d point
177  @return 0 on success \n <0 on error \n >0 if point is behind a camera
178  @status tested, Daniel Grest */
179  int Triangulate(PMatrix &P1, PMatrix &P2,
180  const HomgPoint2D& p1, const HomgPoint2D& p2,
181  BIAS::Vector3<double>& point3d);
182 
183  /// deprecated
184  int Triangulate(PMatrix &P1, PMatrix &P2,
185  const HomgPoint2D& p1, const HomgPoint2D& p2,
186  BIAS::HomgPoint3D& point3d);
187 
188  /** @brief Triangulation for metric Poses (using C and R)
189  @param point3d (output) guaranteed to be homogenized, inf causes error
190  @return 0 on success \n <0 on error \n >0 if point is behind a camera
191  @status woelk 07/2006 */
192  int Triangulate(const ProjectionParametersBase *P1,
193  const ProjectionParametersBase *P2,
194  const HomgPoint2D& p1, const HomgPoint2D& p2,
195  Vector3<double>& point3d);
196  /// deprecated
197  int Triangulate(const ProjectionParametersBase *P1,
198  const ProjectionParametersBase *P2,
199  const HomgPoint2D& p1, const HomgPoint2D& p2,
200  HomgPoint3D& point3d);
201 
202  /** @brief Triangulation for metric Poses (using C and R)
203  @param point3d: resulting 3D point. It may be at infinity.
204  @author woelk 06/2008 (c) www.vision-n.de */
205  int TriangulateProjective(const ProjectionParametersBase *P1,
206  const ProjectionParametersBase *P2,
207  const HomgPoint2D& p1, const HomgPoint2D& p2,
208  HomgPoint3D& point3d);
209 
210 
211  /** @brief Triangulation for metric Poses (using C and R)
212  @param point3d (output) guaranteed to be homogenized, inf causes error
213  @return 0 on success \n <0 on error \n >0 if point is behind a camera
214  @status woelk 07/2006 */
215  int Triangulate(const Pose& P1, const Pose& P2,
216  const HomgPoint2D& p1, const HomgPoint2D& p2,
217  Vector3<double>& point3d);
218 
219  /// deprecated
220  int Triangulate(const Pose& P1, const Pose& P2,
221  const HomgPoint2D& p1, const HomgPoint2D& p2,
222  HomgPoint3D& point3d);
223 
224  /** @brief Triangulation for metric Poses (using C and Q)
225  @author woelk 09/2007 */
226  int Triangulate(const Vector3<double>& C1, const Quaternion<double>& Q1,
227  const Vector3<double>& C2, const Quaternion<double>& Q2,
228  const HomgPoint2D& p1, const HomgPoint2D& p2,
229  Vector3<double>& point3d);
230 
231  /// deprecated
232  int Triangulate(const Vector3<double>& C1, const Quaternion<double>& Q1,
233  const Vector3<double>& C2, const Quaternion<double>& Q2,
234  const HomgPoint2D& p1, const HomgPoint2D& p2,
235  HomgPoint3D& point3d);
236 
237  /** @brief does not use BackprojectPseudoInverse but get NormRayWorldCoo
238 
239  Also calculates the minimal distance between the two rays (dist)
240  and the cos of the angle between the two rays (angle) (scalarproduct)
241  input: P1, P2, p1, p2
242  output: point3d, dist
243  @return 0 on success
244  -1 if interpolated point lies behind the cameras
245  -2 if lines do not intersect
246  -3 if camera center not extractable from P
247  (faster) @author Woelk 11 2002 */
248  int Triangulate2D(PMatrix& P1, PMatrix& P2, HomgPoint2D& p1,
249  HomgPoint2D& p2, HomgPoint3D& point3d, double& dist,
250  double& angle);
251 
252  /** @brief as above, but also determines the covariance matrix
253  @return -4 if GetCovariance2D failed
254  @author Woelk 11 2002 */
255  int Triangulate2D(PMatrix& P1, PMatrix& P2, HomgPoint2D& p1,
256  HomgPoint2D& p2, HomgPoint3D& point3d,
257  Matrix3x3<double>& CovMatrix, double& dist,
258  double& angle, const double &triangdeltapixel =
259  TRIANGULATION_DEFAULT_DELTAPIXEL);
260 
261  /** @brief Calculates and returns the uncertainty as normalized
262  covariance matrix in 'covariance'.
263 
264  It is calculated by triangulation of all neighboring
265  pixels within +- triangdeltapixel of p1 and p2 and taking the variance
266  with 'point'.
267  Uses Triangulate2D.
268  returns 0 in case of success
269  returns -4 in case of error (too little triangulations succeeded)
270  @author Daniel Grest, Oct 2002
271  @status tested */
272  int GetCovariance2D(PMatrix& P1, PMatrix& P2, HomgPoint2D& p1,
273  HomgPoint2D& p2, HomgPoint3D &point3D,
274  Matrix3x3<double>& covariance,
275  const double &triangdeltapixel =
276  TRIANGULATION_DEFAULT_DELTAPIXEL);
277 
278  /** @brief Calculates and returns the uncertainty as normalized
279  covariance matrix in 'covariance'.
280 
281  It is calculated by triangulation of all neighboring
282  pixels within +- triangdeltapixel of p1 and p2 and taking the variance
283  with 'point'.
284  Uses Triangulate.
285  @author Daniel Grest, Oct 2002
286  @param P1 PMatrix for image one
287  @param P1 PMatrix for image two
288  @param p1 Homogenized(!) point in image one
289  @param p2 Homogenized(!) point in image two
290  @param triangdeltapixel offset in all directions and both images
291  @status tested
292  @return 0 in case of success, <0 otherwise */
293  int GetCovarianceAnalytic(PMatrix& P1, PMatrix& P2, HomgPoint2D& p1,
294  HomgPoint2D& p2, HomgPoint3D &point3D,
295  Matrix3x3<double>& covariance,
296  const double &triangdeltapixel =
297  TRIANGULATION_DEFAULT_DELTAPIXEL,
298  const double &sigma=
299  TRIANGULATION_DEFAULT_SIGMA);
300 
301  /** @brief returns an approximation to the 3D point p with uncertainty
302  cov as derived from the 2D points p1 and p2 with uncertainties
303  cov1 and cov2
304 
305  Generates randomly num 2D coordinates distributed according to cov1
306  centered around p1 and num 2D coordinates distributed according to
307  cov2 centered around p2. For every possible combination of these two
308  2D point clouds a 3D point is triangulated resulting in a 3D point
309  cloud of maximum num*num points. The mean and the covariance of this
310  3D point cloud is calculated and returned in p (mean) and cov.
311  The 3D point p, which is the mean of the point cloud, is generally
312  *not* aequivalent to the 3D point resulting from simple triangulation
313  using p1 and p2.
314 
315  The 2D points p1 and p2 must be homogenized and are not allowed to be
316  at infinity.
317 
318  !! untested !!
319 
320  @returns the number of points in 3D cloud. The covariance of the 3D
321  approximation cov is only valid if *more* than 1 point was triangulated
322 
323  @author woelk 07/2005 */
324  int GetCovariance(PMatrix& P1, PMatrix& P2,
325  const HomgPoint2D& p1, const Matrix2x2<double>& cov1,
326  const HomgPoint2D& p2, const Matrix2x2<double>& cov2,
327  HomgPoint3D& p, Matrix3x3<double>& cov, const int num, bool quasiEuclidean=false);
328 
329 
330  int GetCovarianceProjective(PMatrix& P1, PMatrix& P2,
331  const HomgPoint2D& p1, const HomgPoint2DCov& cov1,
332  const HomgPoint2D& p2, const HomgPoint2DCov& cov2,
333  HomgPoint3D& p, HomgPoint3DCov& cov);
334 
335 #ifdef BIAS_TRIANG_DEBUG
336  void GetCovPoints(std::vector<HomgPoint3D>& pvec,
337  std::vector<double>& weight);
338 #endif
339 
340 
341 
342  protected:
343  /** @brief called by constructor to set up neighbourhood weights for
344  covariance computation */
345  void _UpdateCovWeights(const double &TriangDeltaPixels=
346  TRIANGULATION_DEFAULT_DELTAPIXEL,
347  const double &sigma=
348  TRIANGULATION_DEFAULT_SIGMA);
349 
350  /// neighbourhood weights for covariance computation
352  /// value to go up/down/left/right for "manual" covariance computation
355 #ifdef BIAS_TRIANG_DEBUG
356  std::vector<HomgPoint3D> covpoints_;
357  std::vector<double> covweights_;
358 #endif
359  };
360 
361 
362 } // namespace
363 
364 
365 //#include <Base/Common/BIASpragmaEnd.hh>
366 
367 #endif // __Triangulation_hh__
double _dTriangDeltaPixels
value to go up/down/left/right for &quot;manual&quot; covariance computation
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
class representing the covariance matrix of a homogenous point 2D
Class for triangulation of 3Dpoints from 2D matches. Covariance matrix (refering to an uncertainty el...
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
class representing a Fundamental matrix
Definition: FMatrix.hh:46
Matrix3x3< double > cov_weights_
neighbourhood weights for covariance computation
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
class representing the covariance matrix of a homogenous point 3D
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix4x4.hh:54
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
class for rotation with axis and angle
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...