Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestHMatrix.cpp
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 
22 #include <Base/Math/Random.hh>
23 #include <Geometry/HMatrix.hh>
24 #include <Geometry/HMatrixEstimation.hh>
25 #include <Geometry/RMatrix.hh>
26 
27 using namespace BIAS;
28 using namespace std;
29 
30 /**
31  @file TestHMatrix.cpp
32  @brief testing HMatrix and HMatrixEstimation
33  @relates HMatrix HMatrixEstimation
34  @ingroup g_tests
35  @author MIP
36 */
37 int main(int argc, char *argv[])
38 {
39 
40  Random rand;
41 
42  // compute PMatrices for testing
43  KMatrix k1;
44  KMatrix k2;
45  k1.SetIdentity();
46  k1[0][0] = k1[1][1] = rand.GetUniformDistributed(200, 400);
47  k1[0][2] = k1[1][2] = k1[0][0]/2.0;
48  k2.SetIdentity();
49  k2[0][0] = k2[1][1] = rand.GetUniformDistributed(200, 400);
50  k2[0][2] = k2[1][2] = k2[0][0]/2.0;
51 
52  RMatrix r1;
53  RMatrix r2;
54  r1.SetXYZ(rand.GetUniformDistributed(0, 180) * M_PI/180.0,
55  rand.GetUniformDistributed(0, 180) * M_PI/180.0,
56  rand.GetUniformDistributed(0, 180) * M_PI/180.0);
57  r2.SetXYZ(rand.GetUniformDistributed(0, 180) * M_PI/180.0,
58  rand.GetUniformDistributed(0, 180) * M_PI/180.0,
59  rand.GetUniformDistributed(0, 180) * M_PI/180.0);
60 
61  Vector3<double> c1(rand.GetUniformDistributed(-100, 100),
62  rand.GetUniformDistributed(-100, 100),
63  rand.GetUniformDistributed(-100, 100));
64  Vector3<double> c2(rand.GetUniformDistributed(-100, 100),
65  rand.GetUniformDistributed(-100, 100),
66  rand.GetUniformDistributed(-100, 100));
67 
68  PMatrix p1(k1, r1, c1);
69  PMatrix p2(k2, r2, c2);
70 
71 
72  // testing Normalization
73  HMatrix H(p1, p2);
74  H = 1.0/H[2][2] * H;
75 
76 
77  // map across plane at infinity
78  HMatrix H1(p1, p2);
79  H1 = 1.0/H1[2][2] * H1;
80  HMatrix H_gt = k2*r2*r1.Transpose()*k1.Invert();
81  H_gt = 1.0/H_gt[2][2] * H_gt;
82 
83  if(H1 != H_gt){
84  return -1;
85  }
86 
87 
88 
89  // create ground truth HMatrix and point sets for estimation
90  HMatrix H_gt2;
91  for(unsigned int i=0; i < 3; i++){
92  for(unsigned int j=0; j < 3; j++){
93  H_gt2[i][j] = rand.GetUniformDistributed(-100, 100);
94  }
95  }
96  H_gt2[2][2] = 1.0;
97 
98  unsigned int num_points = 5;
99  vector<HomgPoint2D> points2D_1;
100  vector<HomgPoint2D> points2D_2;
101  HomgPoint2D p2d;
102  for(unsigned int i=0; i < num_points; i++){
103  p2d = HomgPoint2D(rand.GetUniformDistributed(-100, 100),
104  rand.GetUniformDistributed(-100, 100));
105  points2D_1.push_back(p2d);
106  p2d = H_gt2 * points2D_1[i];
107  p2d.Homogenize();
108  points2D_2.push_back(p2d);
109  }
110 
111  // estimation of H based on points
112  HMatrixEstimation estimator;
113  HMatrix Hest;
114  estimator.Compute(points2D_1, points2D_2, Hest);
115  Hest = 1.0/Hest[2][2] * Hest;
116  double residual = Hest.GetReprojectionError(points2D_1, points2D_2);
117  if(residual > 1e-12){
118  return -1;
119  }
120 
121  HMatrix Hopt = Hest;
122  estimator.Optimize(Hopt, points2D_1, points2D_2);
123  Hopt = 1.0/Hopt[2][2] * Hopt;
124  residual = Hopt.GetReprojectionError(points2D_1, points2D_2);
125  if(residual > 1e-12){
126  return -1;
127  }
128 
129  cout << "done, passed" << endl;
130 
131  return 0;
132 }
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.
a 3x3 Matrix describing projective transformations between planes
Definition: HMatrix.hh:39
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
int Optimize(BIAS::HMatrix &H, const std::vector< BIAS::HomgPoint2D > &p1, const std::vector< BIAS::HomgPoint2D > &p2)
Optimize a given homography matrix from 2d point correspondences.
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
int Compute(const std::vector< BIAS::HomgPoint2D > &fromPoints, const std::vector< BIAS::HomgPoint2D > &toPoints, HMatrix &H)
Calculate homography between 2d points using the DLT.
double GetReprojectionError(std::vector< BIAS::HomgPoint2D > &p1, std::vector< BIAS::HomgPoint2D > &p2)
compute reprojection error for both vectors of HomgPoint2D
Definition: HMatrix.cpp:303
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Definition: Matrix3x3.cpp:167
Estimate 3x3 matrix relating image coordinates with each other, i.e.
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
KMatrix Invert() const
returns analyticaly inverted matrix
Definition: KMatrix.cpp:31
class for producing random numbers from different distributions
Definition: Random.hh:51
class BIASGeometryBase_EXPORT HomgPoint2D
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429