Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HMatrixTest.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 HMatrixTest.cpp
27  @relates hMatrix, HMatrixEstimation
28  @brief example demonstrating H-Matrix
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 // BIAS
34 #include "Geometry/HMatrixEstimation.hh"
35 #include "Base/Math/Random.hh"
36 
37 using namespace std;
38 using namespace BIAS;
39 
40 // ---------------------------------------------------------------------------
41 int main(int /*argc*/, char** /*argv*/)
42 {
43 
44  HMatrix TheoH(MatrixIdentity), EstH(MatrixZero);
45 
46  // set homography TheoH
47  TheoH[0][0]=1.5; TheoH[0][1]= 0; TheoH[0][2]=10;
48  TheoH[1][0]= 0; TheoH[1][1]=2.1; TheoH[1][2]=100;
49  TheoH[2][0]= 0; TheoH[2][1]= 0; TheoH[2][2]=1;
50  // H-matrix
51  std::vector<HomgPoint2D> points_src;
52  std::vector<HomgPoint2D> points_dest, points_dest_with_out;
53 
54  Random R;
55  const double noise = 0.3;
56 
57  // build pointsets 2D for computation
58  for (int i = 0; i <100; i++){
59  // 2D point
60  HomgPoint2D point2d_im1;
61  for (int k=0; k<2; k++)
62  // point coordinate between 0 and 1000
63  point2d_im1[k]=1000.0*rand()/(RAND_MAX+1.0);
64  point2d_im1[2]=1;
65 
66  // project the point2d to the second image
67  HomgPoint2D point2d_im2 = TheoH * point2d_im1;
68  point2d_im2.Homogenize();
69 
70 
71  point2d_im1[0] += R.GetNormalDistributed(0, noise);
72  point2d_im2[0] += R.GetNormalDistributed(0, noise);
73  point2d_im1[1] += R.GetNormalDistributed(0, noise);
74  point2d_im2[1] += R.GetNormalDistributed(0, noise);
75 
76  // build correspondences
77  points_src.push_back(point2d_im1);
78  points_dest.push_back(point2d_im2);
79  }
80 
81  // estimation of H (linearly)
82  HMatrixEstimation HEstimator;
83  HEstimator.ComputeAffine(points_src, points_dest, EstH);
84 
85  // normalize H to contain a one in the lower right corner
86  if (fabs(EstH[2][2])>1e-50) EstH /= EstH[2][2];
87 
88  cout << "H ground truth is " << TheoH << endl;
89 
90 
91  cout << "H Est is " << EstH << endl;
92 
93  return 0;
94 }
95 
int ComputeAffine(const std::vector< BIAS::HomgPoint2D > &fromPoints, const std::vector< BIAS::HomgPoint2D > &toPoints, HMatrix &H)
Compute affine transformation between 2d points, i.e.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
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
Estimate 3x3 matrix relating image coordinates with each other, i.e.
double GetNormalDistributed(const double mean, const double sigma)
on succesive calls return normal distributed random variable with mean and standard deviation sigma ...
Definition: Random.hh:71
class for producing random numbers from different distributions
Definition: Random.hh:51