Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ProjectionParametersFactory.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 #include <Geometry/Projection.hh>
26 #include <Geometry/ProjectionParametersFactory.hh>
27 #include <Geometry/ProjectionParametersPerspective.hh>
28 #include <Geometry/ProjectionParametersPerspectiveDepth.hh>
29 #include <Geometry/ProjectionParametersProjective.hh>
30 #include <Geometry/ProjectionParametersSphericalFast.hh>
31 #include <Geometry/ProjectionParametersSpherical.hh>
32 #include <Geometry/ProjectionParametersCylindric.hh>
33 #include <Geometry/ProjectionParametersGreatCircles.hh>
34 #include <Geometry/ProjectionParametersSphericalSimple.hh>
35 #include <Geometry/ProjectionParametersZoom.hh>
36 #ifndef WIN32
37 #ifdef BUILD_DYNAMIC_LOAD_PROJECTION
38 #include <Geometry/ProjectionParametersDynamicLoad.hh>
39 #endif
40 #endif
41 using namespace BIAS;
42 using namespace std;
43 
45 {
46  ValidClassNames_.push_back("ProjectionParametersPerspective");
47  ValidClassNames_.push_back("ProjectionParametersPerspectiveDepth");
48  ValidClassNames_.push_back("ProjectionParametersProjective");
49  ValidClassNames_.push_back("ProjectionParametersSpherical");
50  ValidClassNames_.push_back("ProjectionParametersSphericalFast");
51  ValidClassNames_.push_back("ProjectionParametersCylindric");
52  ValidClassNames_.push_back("ProjectionParametersGreatCircles");
53  ValidClassNames_.push_back("ProjectionParametersZoom");
54  ValidClassNames_.push_back("ProjectionParametersSphericalSimple");
55 
56 }
58  ValidClassNames_.clear();
59 }
60 
62 ProjectionParametersFactory::Create(const std::string& className)
63 {
64  if (className == "ProjectionParametersPerspective")
66  if (className == "ProjectionParametersPerspectiveDepth")
68  if (className == "ProjectionParametersProjective")
70  if (className == "ProjectionParametersSphericalFast")
72  if (className == "ProjectionParametersSpherical")
74  if (className == "ProjectionParametersCylindric")
76  if (className == "ProjectionParametersGreatCircles")
78  if (className == "ProjectionParametersZoom")
79  return new ProjectionParametersZoom;
80  if (className == "ProjectionParametersSphericalSimple")
82 #ifndef WIN32
83 #ifdef BUILD_DYNAMIC_LOAD_PROJECTION
85  if(ppdl->GetDynPtr() != NULL){
86  return ppdl;
87  }
88 #endif
89 #endif
90 
91  BIASERR("Invalid projection parameters class: " << className);
92  return NULL;
93 }
94 
97 {
98  ProjectionParametersBase* ppb = NULL;
99  if (camData.LensIsSpherical_) {
100  //initialize fisheye camera
102  pps->SetRadius(camData.radius_);
103  pps->SetUndistortion(camData.AngleCorrX_, camData.AngleCorrY_,
104  camData.radius_);
105  ppb = (ProjectionParametersBase*) pps;
106  } else {
107  if (camData.zoomCamera_){
108  // initialize zoom camera
111  std::vector<CPDiscreteParam>::iterator it;
112  ppz->Clear();
113  for (unsigned int j=0; j<camData.knownparams_vect_.size(); j++) {
114  ppz->AddZoomStep(camData.knownparams_vect_[j].zoom,
115  camData.knownparams_vect_[j].focallength,
116  camData.knownparams_vect_[j].kc1,
117  camData.knownparams_vect_[j].kc2,
118  camData.knownparams_vect_[j].kc3,
119  camData.knownparams_vect_[j].kc4);
120  }
121  ppb = (ProjectionParametersBase*) ppz;
122  } else {
123  // initialize perspective camera
126  // convert fl to meter
128  camData.aspectratio_);
129  ppp->SetUndistortion(camData.kc1Def_,camData.kc2Def_,
130  camData.kc3Def_,camData.kc4Def_);
131  ppb = (ProjectionParametersBase*) ppp;
132  }
133  }
134  // Set base parameters
135  ppb->SetImageSize(camData.width_, camData.height_);
136  ppb->SetPrincipal(camData.principalX_, camData.principalY_);
137  double eps = 1e-8;
138  if ((camData.aspectratio_ < camData.cellSizeX_/camData.cellSizeY_-eps) ||
139  (camData.aspectratio_ > camData.cellSizeX_/camData.cellSizeY_+eps)) {
140  BIASERR("Aspect ratio and cellsizes do not match!");
141  BIASABORT;
142  }
143  ppb->SetAspectratio(camData.aspectratio_);
144  ppb->SetC(camData.PoseInRigCenter_);
145  RMatrix R;
147  R.SetXYZ(camData.PoseInRigPitch_, camData.PoseInRigYaw_,
148  camData.PoseInRigRoll_);
149  R.GetQuaternion(Q);
150  ppb->SetQ(Q);
151 
152  return ppb;
153 }
154 
156 ProjectionParametersFactory::Load(const std::string& fileName)
157 {
158 #ifdef BIAS_HAVE_XML2
159  XMLIO myXML;
160  xmlNodePtr rootNode;
161  rootNode = myXML.read(fileName);
162  if (rootNode==NULL) {
163  BIASERR("Projection parameter XML file "<<fileName<<" not found or parse error!");
164  return NULL;
165  }
166  string className = myXML.getNodeName(rootNode);
167  if (className == "Projection") {
168  BIASERR("Trying to load projection parameters from projection XML file "
169  <<fileName<<", trying to read projection instead!");
170  Projection proj;
171  if (proj.Load(fileName) != 0) {
172  if (proj.Size() > 0) {
173  return proj.GetParameterCloneWithAbsolutePose();
174  }
175  }
176  return NULL;
177  }
178  ProjectionParametersBase *ppb = Create(className);
179  if (!ppb){
180  BIASERR("Unknown projection parameters type "<<className<<" found in XML file "
181  <<fileName<<"!");
182  return NULL;
183  }
184  int res = ppb->XMLIn(rootNode, myXML);
185  if (res!=0){
186  BIASERR("Error reading projection parameters type "<<className<<" from XML file "
187  <<fileName<<"!");
188  delete ppb;
189  return NULL;
190  }
191  if (ppb) {
192  return ppb;
193  } else {
194  BIASERR("Error reading projection parameters from file "<<fileName
195  <<", trying to read camera instead!");
196  CameraData camData;
197  if (ProjectionParametersIO::ReadCameraData(fileName, camData) != 0){
198  return NULL;
199  }
200  return Create(camData);
201  }
202 
203 #else // BIAS_HAVE_XML2
204  BIASERR("you need XML2");
205  BIASBREAK;
206  return NULL;
207 #endif // BIAS_HAVE_XML2
208 }
can load exernally programmed child classes of ProjectionParametersBase
virtual void SetPrincipal(const double x, const double y)
Set principal point (in pixels relative to top left corner).
void SetXYZ(ROTATION_MATRIX_TYPE PhiX, ROTATION_MATRIX_TYPE PhiY, ROTATION_MATRIX_TYPE PhiZ)
Set Euler angles (in rad) in order XYZ.
static int ReadCameraData(const std::string &Filename, CameraData &cd, const bool silent=false)
Read a camera data parameter file and store the data in (*this)
xmlNodePtr read(const std::string &Filename)
Read and parse an XML file from disk, DtD validation is not yet implemented.
Definition: XMLIO.cpp:416
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
additional depth calibration parameters for a perspective depth camera to be used for ToF-(PMD) camer...
spherical camera that uses polynomial only and should therefore be faster than other spherical camera...
projection parameters camera parameters which define the mapping between rays in the camera coordinat...
ProjectionParametersBase * Create(const std::string &className)
Creates a projection parameters object by a class name.
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual void SetImageSize(const unsigned int w, const unsigned int h)
Set image dimensions (in pixels).
virtual int XMLIn(const xmlNodePtr Node, XMLIO &XMLObject)
Specialization of XML read function.
std::vector< double > AngleCorrX_
spherical camera that samples along big circles containig the H vector.
int GetQuaternion(Quaternion< ROTATION_MATRIX_TYPE > &quat) const
Calculates quaternion representation for this rotation matrix.
void SetRadius(const double r)
Set radius of spherical image in pixels.
3D rotation matrix
Definition: RMatrix.hh:49
ProjectionParametersBase * Load(const std::string &fileName)
Loads a projection parameters object from a file.
virtual void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
BIAS::Vector3< double > PoseInRigCenter_
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
12 parametric p for completely projective reconstruction scenarios, unknown focal lengths...
std::vector< CPDiscreteParam > knownparams_vect_
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
int SetUndistortion(const std::vector< double > &undistAngles, const std::vector< double > &distAngles, const double radius)
Set undistortion polynomial from corresponding undistorted and distorted azimuth angles (in rad) and ...
std::string getNodeName(const xmlNodePtr Node) const
Get the name of a given Node.
Definition: XMLIO.cpp:543
std::vector< double > AngleCorrY_
int AddZoomStep(double zoom, double focallength, double kc1, double kc2, double kc3, double kc4)
unsigned int Size() const
Determine number of ProjectionParameterBase pointers in Projection.
Definition: Projection.hh:178
virtual void SetAspectratio(const double AspectRatio)
Set CCD aspect ratio (i.e.
void SetFocalLengthAndAspect(double f, double AspectRatio)
Set the current camera focal length in pixel and the a spect ratio.
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
ProjectionParametersBase * GetParameterCloneWithAbsolutePose(unsigned int cam=0) const
Allocates memory for parameters base type and returns pointer to it!
Definition: Projection.hh:218
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
ProjectionParametersBase * GetDynPtr()
get base pointer to dynamically loaded object in order to handle it directly