Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
InvestigatePRelations.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  @example InvestigatePRelations.cpp
27  @relates PMatrixLinear
28  @brief example investigating PR (?)
29  @ingroup g_examples
30  @author MIP
31 */
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 #include <Utils/ThreeDOut.hh>
42 #include <vector>
43 #include <Geometry/PMatrixLinear.hh>
44 
45 #include <MathAlgo/SVD.hh>
46 
47 using namespace BIAS;
48 using namespace std;
49 
50 
51 //int main(int argc, char *argv[])
52 int main()
53 {
54 
55  unsigned int numPs = 50;
56 
57  vector<PMatrix> P_origs;
58  vector<PMatrix> P_transformed;
59  PMatrix P;
60 
61  KMatrix K;
62  K.SetIdentity();
63  K[0][0] = 400;
64  K[1][1] = 400;
65  K[0][2] = 200;
66  K[1][2] = 200;
67 
69  double phiX, phiY, phiZ;
70  RMatrix R;
71 
72  Random randomizer;
73 
74  // build transformation matrix T
76  C[0] = randomizer.GetUniformDistributed(-10.0,10.0);
77  C[1] = randomizer.GetUniformDistributed(-10.0,10.0);
78  C[2] = randomizer.GetUniformDistributed(-10.0,10.0);
79  phiX = randomizer.GetUniformDistributed(-30.0,30.0) * M_PI/180.0;
80  phiY = randomizer.GetUniformDistributed(-30.0,30.0) * M_PI/180.0;
81  phiZ = randomizer.GetUniformDistributed(-30.0,30.0) * M_PI/180.0;
82  R.SetXYZ(phiX, phiY, phiZ);
83  for(int i=0; i < 3; i++){
84  for(int j=0; j < 3; j++){
85  T[i][j] = R[i][j];
86  }
87  T[i][3] = C[i];
88  }
89  T[3][0] = randomizer.GetUniformDistributed(-5.0, 5.0);
90  T[3][1] = randomizer.GetUniformDistributed(-5.0, 5.0);
91  T[3][2] = randomizer.GetUniformDistributed(-5.0, 5.0);
92  T[3][3] = randomizer.GetUniformDistributed(-5.0, 5.0);
93 
94  cout << "T " << T << endl;
95 
96  // build Ps and transform them
97  for(unsigned int i = 0; i < numPs; i++){
98  C[0] = randomizer.GetUniformDistributed(-10.0,10.0);
99  C[1] = randomizer.GetUniformDistributed(-10.0,10.0);
100  C[2] = randomizer.GetUniformDistributed(-10.0,10.0);
101  phiX = randomizer.GetUniformDistributed(-30.0,30.0) * M_PI/180.0;
102  phiY = randomizer.GetUniformDistributed(-30.0,30.0) * M_PI/180.0;
103  phiZ = randomizer.GetUniformDistributed(-30.0,30.0) * M_PI/180.0;
104  R.SetXYZ(phiX, phiY, phiZ);
105 // cout << "orig angles " << phiX << " " << phiY << " " << phiZ << endl;
106  P.Compose(K, R, C);
107  P_origs.push_back(P);
108  P_transformed.push_back(PMatrix(P*T));
109  R = RMatrix(P_transformed[i].GetR());
110 
111  // put noise on transformed matrices
112  (P_transformed[i])[0][0] = (P_transformed[i])[0][0] + randomizer.GetUniformDistributed(-2.0,2.0); // fx
113  (P_transformed[i])[0][1] = (P_transformed[i])[0][1] + randomizer.GetUniformDistributed(-0.2,0.2); // s
114  (P_transformed[i])[0][2] = (P_transformed[i])[0][2] + randomizer.GetUniformDistributed(-2.0,2.0); // ux
115  (P_transformed[i])[1][1] = (P_transformed[i])[1][1] + randomizer.GetUniformDistributed(-2.0,2.0); // fy
116  (P_transformed[i])[1][2] = (P_transformed[i])[1][2] + randomizer.GetUniformDistributed(-2.0,2.0); // uy
117  }
118 
119 
120  // least squares with one equation system per T column
121  Matrix<double> A(numPs*3,4);
122  A.SetIdentity();
123  Vector<double> b(numPs*3);
124  Matrix4x4<double> T_leastSquares;
125  for(int col = 0; col < 4; col++){
126  for(unsigned int camCount = 0; camCount < numPs; camCount++){
127  for(int i=0; i < 3; i++){
128  for(int j = 0; j < 4; j++){
129  A[i+camCount*3][j] = (P_origs[camCount])[i][j];
130  }
131  b[i+camCount*3] = (P_transformed[camCount])[i][col];
132  }
133  }
134 // cout <<"A " << A << endl;
135 // cout <<"b " << b << endl;
136  SVD svdA(A);
137  Matrix<double> A_inv = svdA.Invert();
138  Vector<double> T_col = A_inv * b;
139  cout << T_col << endl;
140  T_leastSquares[0][col] = T_col[0];
141  T_leastSquares[1][col] = T_col[1];
142  T_leastSquares[2][col] = T_col[2];
143  T_leastSquares[3][col] = T_col[3];
144  }
145  cout << "Least squares T " << T_leastSquares << endl;
146 
147  SVD svdT(T_leastSquares);
148  Matrix4x4<double> T_invLS = svdT.Invert();
149 
150  vector<PMatrix> P_leastSquares;
151  PMatrix P_leastSquare;
152  for(unsigned int i=0; i < numPs; i++){
153  P_leastSquare = PMatrix(P_transformed[i] * T_invLS);
154  P_leastSquare.InvalidateDecomposition();
155  P_leastSquares.push_back(P_leastSquare);
156  }
157 
158  ThreeDOutParameters param3DO;
159  param3DO.CameraStyle = PyramidMesh;
160  param3DO.PointStyle = Box;
161  param3DO.PointSize = 0.1;
162  param3DO.LineStyle = Solid;
163  param3DO.DrawUncertaintyEllipsoids = false;
164  ThreeDOut threeDOut(param3DO);
165 
166  for(unsigned int i = 0; i < numPs; i++){
167  threeDOut.AddPMatrix(P_origs[i], 400, 400, RGBAuc(255, 0, 0, 127), 0.1);
168 // P_transformed[i].InvalidateDecomposition();
169 // threeDOut.AddPMatrix(P_transformed[i], 400, 400, RGBAuc(0, 255, 0, 127), 0.1);
170  threeDOut.AddPMatrix(P_leastSquares[i], 400, 400, RGBAuc(0, 0, 255, 127), 0.1);
171  }
172 
173  Vector3<double> start(0.0, 0.0, 0.0);
174  Vector3<double> end(1.0, 0.0, 0.0);
175 
176  threeDOut.AddLine(start, end, RGBAuc(255, 0, 0, 127));
177  end.Set(0.0, 1.0, 0.0);
178  threeDOut.AddLine(start, end, RGBAuc(0, 255, 0, 127));
179  end.Set(0.0, 0.0, 1.0);
180  threeDOut.AddLine(start, end, RGBAuc(0, 0, 255, 127));
181  threeDOut.VRMLOut("test.wrl");
182  threeDOut.Dump();
183 
184  return 0;
185 }
186 
187 
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
Unified output of 3D entities via OpenGL or VRML.
Definition: ThreeDOut.hh:349
configuration struct for drawing styles of various 3d objects
Definition: ThreeDOut.hh:309
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
CameraDrawingStyle CameraStyle
Definition: ThreeDOut.hh:313
3D rotation matrix
Definition: RMatrix.hh:49
void InvalidateDecomposition()
to re-Decompose_() after filling with data use this.
Definition: PMatrix.hh:499
LineDrawingStyle LineStyle
Definition: ThreeDOut.hh:314
BIAS::Vector4< unsigned char > RGBAuc
Definition: RGBA.hh:47
class BIASGeometry_EXPORT PMatrix
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
PointDrawingStyle PointStyle
Definition: ThreeDOut.hh:312
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