Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biaspmatrix2projection.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 
27 #include <iostream>
28 #include <vector>
29 
30 #include <Geometry/PMatrix.hh>
31 #include <Geometry/Projection.hh>
32 #include <Geometry/ProjectionParametersPerspective.hh>
33 #include <Utils/Param.hh>
34 
35 using namespace std;
36 using namespace BIAS;
37 
38 
39 /**
40  @file
41  @ingroup g_tools
42  @brief Convert PMatrix to BIAS::Projection, see biaspmatrix2projection.cpp
43  @author bartczak
44 */
45 Param param(true);
46 
47 void usage(char* name)
48 {
49  param.Usage();
50  cout<<"\nConverts PMatrix files into Projection readable files.\n";
51 
52  cout<<"\n usage:\n";
53  cout<<name<<"params <PMatrix> ...\n";
54 }
55 
56 string GetBaseName(const std::string& FileName)
57 {
58  int begin = FileName.find_last_of('/');
59  int end = FileName.find_last_of('.');
60  return FileName.substr(begin+1, end-begin-1);
61 }
62 
63 
64 void PtoPPP(Projection &proj,const string &file,
65  unsigned int w, unsigned int h, bool overwriteIntrinsics)
66 {
67  PMatrix P;
69  params =dynamic_cast<ProjectionParametersPerspective*>(proj.GetParameters());
70  if(params==NULL) {
71  cout<<"no perspective parameters?\n";
72  exit(0) ;
73  }
74  Quaternion<double> quat;
75 
76  if(!P.Load(file)) {
77  cout<<"could not read "<<file<<endl;
78  return;
79  }
80 #ifdef BIAS_DEBUG
81  BIASASSERT(static_cast<RMatrixBase>(P.GetR()).GetQuaternion(quat)==0);
82 #else
83  static_cast<RMatrixBase>(P.GetR()).GetQuaternion(quat);
84 #endif
85  params->SetQ(quat);
86  params->SetC(P.GetC());
87  if (overwriteIntrinsics) {
88  params->SetK(P.GetK());
89  params->SetImageSize(w, h);
90  }
91 #ifdef BIAS_HAVE_XML2
92  proj.XMLWrite(GetBaseName(file)+".xml");
93  cout<<GetBaseName(file)+".xml"<<endl;
94 #endif //BIAS_HAVE_XML2
95 }
96 
97 
98 
99 
100 /** @author bartczak */
101 int main(int argc, char* argv[])
102 {
103  int* width;
104  int* height;
105  string* listFile;
106  string *CameraParamFile;
107  width = param.AddParamInt("width",
108  "width of corresponding image",
109  0, 0, INT_MAX, 'w');
110  height = param.AddParamInt("height",
111  "height of corresponding image",
112  0, 0, INT_MAX, 'h');
113  listFile = param.AddParamString("listFile",
114  "file containing a list of pamtrices");
115  CameraParamFile = param.AddParamString("cameraParam",
116  "XML-file with camera parameters");
117 
118  int firstPMatrix = param.ParseCommandLine(argc, argv);
119  if( argc<2) {
120  usage(argv[0]);
121  return 0;
122  }
123 
124 
125  PMatrix P;
126 
127  std::vector< std::string > LinesInFile;
128  if(*listFile!="") {
129  Param::ParseListFile (*listFile, LinesInFile);
130  cout<<"found"<<LinesInFile.size()<<"entries in specified listFile"<<endl;
131  }
132 
133  bool UseKFromP = false;
134  Projection *proj;
135 
136  if (*CameraParamFile == "") {
137  cout<<"width = "<<*width<<" height = "<<*height<<endl;
139  proj = new Projection(pp);
140  UseKFromP = true;
141  }
142  else {
143  proj = new Projection;
144 #ifdef BIAS_HAVE_XML2
145  proj->ReadFromCamParamFile(*CameraParamFile);
146 #endif //BIAS_HAVE_XML2
147  }
148 
149 
150  int i;
151  for(i=0; i<(int)LinesInFile.size(); i++) {
152  PtoPPP(*proj, LinesInFile[i], *width, *height, UseKFromP);
153  }
154 
155  for(i=firstPMatrix; i<argc; i++) {
156  PtoPPP(*proj, argv[i], *width, *height, UseKFromP);
157  }
158 
159 
160  return 0;
161 }
bool Load(const std::string &filename)
overload Load because it has to invalidate decomposition! JW 09/2003
Definition: PMatrix.hh:348
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).
int GetR(Matrix3x3< double > &R)
Definition: PMatrix.cpp:204
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
Definition: Param.cpp:1028
virtual void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
int XMLWrite(const std::string &Filename, int CompressionLevel=0, bool AutoAddCompressionSuffix=true, std::string encoding="UTF-8") const
call this to add the class to a new xml tree and write it to the file Filename.
Definition: XMLBase.cpp:40
int GetC(Vector3< double > &C)
computes translation vector origin world coo -&gt; origin camera coo (center), uses decomposition, which is cached
Definition: PMatrix.cpp:165
This class Param provides generic support for parameters.
Definition: Param.hh:231
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
virtual void SetK(const KMatrix &K)
sets the internal parameters from a given KMatrix and updates the cached K and its inverse ...
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:327
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
int ReadFromCamParamFile(const std::string &filename, const bool silent=false)
Reads a camera data XML file and constructs the ProjectionParametersBase-pointer, using the Projectio...
Definition: Projection.cpp:99
int GetK(KMatrix &K)
calibration matrix
Definition: PMatrix.cpp:220