Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExamplePMatrix.cpp
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 
26 /**
27  @example ExamplePMatrix.cpp
28  @relates PMatrix
29  @brief Example PMatrix
30  @ingroup g_examples
31  @author MIP
32 */
33 
34 #include <Base/Common/BIASpragma.hh>
35 
36 #include <Base/Geometry/HomgPoint2D.hh>
37 #include <Geometry/PMatrix.hh>
38 #include <Geometry/RMatrix.hh>
39 #include <Base/Geometry/KMatrix.hh>
40 #include <Base/Math/Random.hh>
41 
42 #include <vector>
43 #include <Geometry/PMatrixLinear.hh>
44 
45 
46 using namespace BIAS;
47 using namespace std;
48 
49 
50 //int main(int argc, char *argv[])
51 int main()
52 {
53 
54  cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ExamplePMatrix $$$$$$$$$$$$$$$$$$$$$$"
55  << endl;
56  PMatrix P_current;
57  P_current.SetIdentity();
58 
59  KMatrix K;
60  K.SetIdentity();
61  K *= 100.0;
62  K[0][2] = 384;
63  K[1][2] = 256;
64  K[2][2] = 1.0;
65  K.SetIdentity();
66 
67 
68  Vector3<double> axis(1,2,3);
69  axis.Normalize();
70  double angle = 1.0;
71  RMatrix R;
72  R.Set(axis, angle);
73 
74  Vector3<double> C(1,2,3);
75  Matrix3x3<double> Hinf;
76  P_current.Compose(K,R,C);
77 
78 
79  if (P_current.GetHinf(Hinf)!=0){
80  BIASERR("Error computing Hinf");
81  };
82  cout<<"C is "<<P_current.GetC()<<" R is "<<P_current.GetR()
83  <<" K is "<<P_current.GetK()<<" Hinf is "<<Hinf<<endl;
84  PMatrix Pose=P_current;
85  cout<<"C is "<<Pose.GetC()<<" R is "<<Pose.GetR()
86  <<" K is "<<Pose.GetK()<<endl;
87 
88  bool TestP = false;
89  if (TestP) {
90  for (unsigned int i=0; i<4; i++)
91  for (unsigned int j=0; j<3; j++) {
92  double noise = 0.995+(0.01*rand()/(double)RAND_MAX);
93  P_current[j][i] *= noise;
94  cout << noise <<" ";
95  }
96  P_current *= 7.5;
97  P_current.InvalidateDecomposition();
98 
99  cout <<"Pcurrent is "<<P_current<<endl;
100  P_current.Normalize();
101  cout <<"Pcurrent after normalizing is "<<P_current<<endl;
102 
103  if (P_current.GetHinf(Hinf)!=0){
104  BIASERR("Error computing Hinf");
105  };
106 
107  cout<<"C is "<<P_current.GetC()<<" R is "<<P_current.GetR()
108  <<" K is "<<P_current.GetK()<<" Hinf is "<<Hinf<<endl;
109  }
110  cout <<" +++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
111  cout <<" ++++++++++++++ linear p estimation ++++++++++++++++++++++"<<endl;
112  BIAS::Random Rand;
113  vector<HomgPoint3D> vecX;
114  vector<HomgPoint2D> vecx;
115  vector<HomgPoint3D*> vecpX;
116  vector<HomgPoint2D*> vecpx;
117  unsigned int n=6;
118  vecX.reserve(n);
119  vecx.reserve(n);
120  bool WantPlane=true, WantNoise=true;
121  unsigned int NumberOffPlane = 0;
122  HomgPoint3D X,
123  a(Rand.GetUniformDistributed(-100.0,100.0),
124  Rand.GetUniformDistributed(-100.0,100.0),
125  Rand.GetUniformDistributed(1.0,100.0), 1.0),
126  b(Rand.GetUniformDistributed(-100.0,100.0),
127  Rand.GetUniformDistributed(-100.0,100.0),
128  Rand.GetUniformDistributed(1.0,100.0), 1.0),
129  c(Rand.GetUniformDistributed(-100.0,100.0),
130  Rand.GetUniformDistributed(-100.0,100.0),
131  Rand.GetUniformDistributed(1.0,100.0), 1.0);
132 
133  for (unsigned int i=0; i<n; i++) {
134  if (WantPlane) {
135  double weight1 = Rand.GetUniformDistributed(0.0,1.0);
136  double weight2 = Rand.GetUniformDistributed(0.0,weight1);
137  X = a*weight1 + b*weight2 + c*(1.0-weight1-weight2);
138  X[3] = 1.0;
139  } else {
140  X.Set(Rand.GetUniformDistributed(-100.0,100.0),
141  Rand.GetUniformDistributed(-100.0,100.0),
142  Rand.GetUniformDistributed(1.0,100.0), 1.0);
143  }
144  if (i<NumberOffPlane) {
145  X.Set(Rand.GetUniformDistributed(-100.0,100.0),
146  Rand.GetUniformDistributed(-100.0,100.0),
147  Rand.GetUniformDistributed(1.0,100.0), 1.0);
148  }
149  cout <<"using point X="<<X<<endl;
150  HomgPoint2D x;
151  x = P_current * X;
152  x.Homogenize();
153  if (WantNoise) {
154  x[0] *= Rand.GetUniformDistributed(0.999, 1.001);
155  x[1] *= Rand.GetUniformDistributed(0.999, 1.001);
156  }
157  vecX.push_back(X);
158  vecx.push_back(x);
159  }
160  for (unsigned int i=0; i<n; i++) {
161  vecpX.push_back(&vecX[i]);
162  vecpx.push_back(&vecx[i]);
163  }
164 
165  PMatrixLinear PLin;
166  PLin.SetDebugLevel("PMATRIXLINEAR");
167 
168  Pose.SetZero();Pose.InvalidateDecomposition();
169  cout <<"Pose estimation:"<<endl;
170  if (!PLin.ComputeCalibrated(vecpX, vecpx, Pose)) BIASERR("failed !");
171 
172  cout <<"Pose is "<<Pose<<endl;
173  cout <<"P_current is "<<P_current<<endl;
174  cout <<"Diff is "<<Pose-P_current<<endl;
175  cout<<endl;
176 
177  Pose.SetZero();Pose.InvalidateDecomposition();
178  cout <<"complete PMatrix estimation:"<<endl;
179  PLin.SetDebugLevel(0);
180  if (!PLin.Compute(vecpX, vecpx, Pose)) BIASERR("failed !");
181  Pose.Normalize();
182  if (Pose[2][0]*P_current[2][0]<0) Pose *= -1.0;
183  cout <<"Pose is "<<Pose<<endl;
184  cout <<"P_current is "<<P_current<<endl;
185  cout <<"Diff is "<<Pose-P_current<<endl;
186  cout<<endl;
187 
188  return 0;
189 }
190 
191 
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void Set(const HOMGPOINT3D_TYPE &x, const HOMGPOINT3D_TYPE &y, const HOMGPOINT3D_TYPE &z)
set elementwise with given 3 euclidean scalar values.
Definition: HomgPoint3D.hh:321
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
bool Compute(const std::vector< HomgPoint3D * > &points3D, const std::vector< HomgPoint2D * > &points2D, PMatrix &P)
computes a least squares solution P via SVD, if at least 6 2d-3d correspondences (pointers) are provi...
int GetR(Matrix3x3< double > &R)
Definition: PMatrix.cpp:204
This class computes a PMatrix from 2D/3D correspondences with linear methods.
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
3D rotation matrix
Definition: RMatrix.hh:49
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
void Set(const Vector3< ROTATION_MATRIX_TYPE > &w, const ROTATION_MATRIX_TYPE phi)
Set from rotation axis w and angle phi (in rad)
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
void SetIdentity()
set the elements of this matrix to the matrix 1 0 0 0 0 1 0 0 0 0 1 0 which is no strict identity (!)...
Definition: Matrix3x4.hh:240
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
void InvalidateDecomposition()
to re-Decompose_() after filling with data use this.
Definition: PMatrix.hh:499
bool ComputeCalibrated(const std::vector< HomgPoint3D * > &points3D, const std::vector< HomgPoint2D * > &points2D, PMatrix &Pose)
given n&gt;=6 2D/3D correspondences, compute approximate R and C using linear methods (thats why we need...
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
int GetC(Vector3< double > &C)
computes translation vector origin world coo -&gt; origin camera coo (center), uses decomposition, which is cached
Definition: PMatrix.cpp:165
int GetHinf(BIAS::Matrix3x3< double > &Hinf)
Definition: PMatrix.cpp:151
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
class for producing random numbers from different distributions
Definition: Random.hh:51
void Compose(const Matrix3x3< double > &K, const Matrix3x3< double > &R, const Vector3< double > &C)
composes this from K, R and C using P = [ K R&#39; | -K R&#39; C ] with R&#39; = transpose(R) ...
Definition: PMatrix.cpp:581
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429
void Normalize()
scale P such that optical axis (first three entries of last row) has unit length
Definition: PMatrix.hh:509
int GetK(KMatrix &K)
calibration matrix
Definition: PMatrix.cpp:220