Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestProjectionParametersPerspective.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 
23 /**
24  @file TestProjectionParametersPerspective.cpp
25  @brief Unit test for basic functionality of projection parameters perspective.
26  @relates ProjectionParametersPerspective
27  @ingroup g_tests
28  @author MIP
29 */
30 
31 
32 #include <Base/Common/CompareFloatingPoint.hh>
33 
34 #include <Geometry/ProjectionParametersPerspective.hh>
35 #include <Base/Math/Random.hh>
36 #include <string>
37 #include <vector>
38 
39 
40 using namespace BIAS;
41 using namespace std;
42 
43 
44 #define EPSILON 1e-8
45 
46 
47 int main(int argc, char *argv[])
48 {
49  // Future ocean cam
50 // const unsigned int width = 960;
51 // const unsigned int height = 540;
52 
53 // ProjectionParametersPerspective ppp(width, height);
54 // ppp.SetUndistortion(-0.1483904123, 0.02849210998, 0,0);
55 // ppp.SetFocalLengthAndAspect(869.0050672, 0.9909530544);
56 // ppp.SetPrincipal(479.8758225, 258.0573953);
57 
58  // ingos cam
59  const unsigned int width = 1024;
60  const unsigned int height = 768;
61  ProjectionParametersPerspective ppp(width, height);
62  ppp.SetUndistortionInverseRad(-0.2696871916, 0.1517598534, 0.002169664669, 0.001128644153);
63  ppp.SetFocalLengthAndAspect(1129.290664, 0.9905520182);
64  ppp.SetPrincipal(561.5692167, 348.5405689);
65 
66  Quaternion<double> Q(0,0,0,1);
67  ppp.SetQ(Q);
68  Vector3<double> C(0,0,0);
69  ppp.SetC(C);
70 
71  Random rand;
72  int percent = -1;
73 
74  int countError = 0, countAll = 0;
75  double sumError = 0.0, sumSqError = 0.0;
76 
77  //double px = width/2.0-0.5;
78  //double py = height/2.0-0.5;
79 
80  unsigned int numCams = 100; //1000
81  unsigned int numPoints = 100; //1000
82 
83  HomgPoint2D p2d, p2d_p;
84  HomgPoint3D p3d;
85  double maxError = 0.0;
86  double dist=0, error=0;
87 
88  for (unsigned int cams=0; cams < numCams; cams++)
89  {
90  ppp.SetFocalLengthAndAspect(rand.GetUniformDistributed(700, 900), 1.0);
91  ppp.SetPrincipal(rand.GetUniformDistributed(500, 600),
92  rand.GetUniformDistributed(300, 400));
93  ppp.SetUndistortionInverseRad(rand.GetUniformDistributed(-0.2, -0.1),
94  rand.GetUniformDistributed(0.0, 0.03), 0,0);
95 // rand.GetUniformDistributed(-0.015, 0.015),
96 // rand.GetUniformDistributed(-0.015, 0.015));
97 
98  for (unsigned int points=0; points < numPoints; points++)
99  {
100  if (points == 0) {
101  // use origin as first 2d point
102  p2d[0] = p2d[1] = 0.0;
103  p2d[2] = 1.0;
104  dist = 5000.0;
105  } else {
106  // generate random 2d point and depth
107  p2d[0] = rand.GetUniformDistributed(10, width-11);
108  p2d[1] = rand.GetUniformDistributed(10, height-11);
109  p2d[2] = 1.0;
110  dist = rand.GetUniformDistributed(100.0, 5000.0);
111  }
112 
113  p3d = ppp.UnProjectToPoint(p2d, dist);
114  p2d_p = ppp.Project(p3d);
115  error = (p2d_p.GetEuclidean()-p2d.GetEuclidean()).NormL2();
116  cout << "point " << p2d << " error " << error << endl;
117  if(Greater(error, EPSILON)){
118  countError++;
119 /*
120  BIASERR("Pretty high error for unproject-project " << error
121  << " for " << ppp << " point " << p2d << " 3D " << p3d
122  << " projected " << p2d_p << endl);
123  return -1;
124 */
125  }
126  if(maxError < error){
127  maxError = error;
128  }
129  sumError += error;
130  sumSqError += error*error;
131  countAll++;
132 
133  //int pr = int(100.0*double(cams*numPoints+points)/(numCams*numPoints));
134  }
135 
136  // show progress
137  int pr = int(100.0*double(cams)/numCams);
138  if (percent < pr) {
139  while(percent < pr) {
140  cout << ".";
141  percent++;
142  }
143  cout << " " << percent << "% ";
144  }
145  }
146 
147  double meanError = sumError / (double)countAll;
148  double stdError = sqrt(sumSqError / (double)countAll - meanError*meanError);
149 
150  cout << "\n\n----------------------------------------------------------\n"
151  << " count of errors > " << EPSILON << " : " << countError
152  << " (" << 100.0*(double)countError/(double)countAll << "%)\n"
153  << " average error : " << meanError << " +- " << stdError << endl
154  << " max. error : " << maxError << endl
155  << "----------------------------------------------------------\n\n";
156 
157  return 0;
158 }
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void GetEuclidean(Vector2< HOMGPOINT2D_TYPE > &dest) const
calculate affine coordinates of this and write them to dest affine coordinates are projective coordin...
Definition: HomgPoint2D.hh:244
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
bool Greater(const T left, const T right, const T eps=std::numeric_limits< T >::epsilon())
comparison function for floating point values
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
class for producing random numbers from different distributions
Definition: Random.hh:51