Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ProjectionError.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS 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 
26 #include "ProjectionError.hh"
27 using namespace BIAS;
28 using namespace std;
29 
31  : cam(2)
32 {
33 }
34 
35 void BIAS::ProjectionError::Init(int res, double baseline)
36 {
37  this->baseline = baseline;
38 
39  cam[Left].SetSimplePerspective(0.87, res, res);
40  cam[Right].SetSimplePerspective(0.87, res, res);
41 
42  cam[Left].SetC(Vector3<double>(0.0, 0.0, 0.0));
43  cam[Right].SetC(Vector3<double>(baseline, 0.0, 0.0));
44 
45  RMatrix r;
46 
47  r.SetIdentity();
48  r.SetXYZ(0.0, 0.0, 0.0);
49  cam[Left].SetR(r);
50 
51  r.SetIdentity();
52  r.SetXYZ(0.0, 0.0, 0.0);
53  cam[Right].SetR(r);
54 }
55 void BIAS::ProjectionError::Init(int res, double depth, double angle)
56 {
57  baseline = tan(angle) * depth;
58  Init(res, baseline);
59 }
60 
61 double BIAS::ProjectionError::ComputeDiff(double z, double offset)
62 {
63  HomgPoint3D p(0.0, 0.0, z);
64  HomgPoint2D prj;
65  vector<Vector3<double> > ray(2);
66 
67  prj = cam[Right].Project(p);
68  if (prj.IsAtInfinity())
69  return -1.0;
70 
71  Vector3<double> pos;
72  prj[0] += offset;
73  cam[Right].UnProjectToRay(prj, pos, ray[Right]);
74  ray[Left] = Vector3<double>(p[0], p[1], p[2]) - cam[Left].GetC();
75 
76  /*
77  Schnittpunktberechnung:
78  P ist der Schnittpunkt
79  b ist der Baselinevektor
80  P = t * ray[Left]
81  P = b + (s * ray[Right])
82 
83  -->
84  t * ray[Left] = b + (s * ray[Right])
85  <=> b = t * ray[Left] - s * ray[Right]
86 
87  Aus ray[Left] und ray[Right] lässt sich eine 3x2 Matrix A erstellen.
88  Es gilt:
89  b = A * x
90  Wobei x ein 2D Vektor mit x-Koordinate = t und y-Koordinate = s ist.
91 
92  (A^-1 * A) * x = A^-1 * b
93  x = A^-1 * b
94 
95  Wenn man den Vektor x hat, kann man s und t verwenden.
96  */
97  Matrix<double> c(3, 2);
98  for (int i = 0; i < 3; i++)
99  {
100  c[i][Left] = ray[Left][i];
101  c[i][Right] = -ray[Right][i];
102  }
103 
104  SVD svd(c);
105  c = svd.Invert();
106  Vector3<double> b(baseline, 0.0, 0.0);
107  Vector2<double> result = c * b;
108 
109  double t = result[0];
110  Vector3<double> prjp = ray[Left] * t;
111  // t wird im Richtungsvektor eingesetzt.
112  Vector3<double> vp = Vector3<double>(p[0], p[1], p[2]) + cam[Left].GetC();
113  double diff = prjp.Length();// - vp.Length();
114 
115  return abs(diff);
116 }
117 
120 {
121  if (cam.size() > (unsigned int)which && which >= 0)
122  return &cam[which];
123  else
124  return NULL;
125 }
126 
128 {
129  return baseline;
130 }
void Init(int res, double depth, double angle)
Initiates cameras and calculates the length of the baseline based on minDepth and angle...
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void SetXYZ(ROTATION_MATRIX_TYPE PhiX, ROTATION_MATRIX_TYPE PhiY, ROTATION_MATRIX_TYPE PhiZ)
Set Euler angles (in rad) in order XYZ.
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
double Length() const
returns the Euclidean Length of the Vector
Definition: Vector3.hh:193
double GetBaseline() const
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
double ComputeDiff(double z, double offset)
3D rotation matrix
Definition: RMatrix.hh:49
bool IsAtInfinity() const
Definition: HomgPoint2D.hh:165
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
const ProjectionParametersPerspective * GetCam(ELeftRight which)
Matrix< double > Invert()
returns pseudoinverse of A = U * S * V^T A^+ = V * S^+ * U^T
Definition: SVD.cpp:214
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429