Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleProjectionParametersPerspective.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  @example ExampleProjectionParametersPerspective.cpp
28  @relates ProjectionParametersPerspective
29  @brief small example demonstrating the ProjectionParametersPerspective
30  by calculating the lens distortion after brown (?)
31  @ingroup g_examples
32  @author grauel 9/2007
33 */
34 
35 #include <Geometry/ProjectionParametersPerspective.hh>
36 #include <Base/Image/Image.hh>
37 #include <Base/Image/ImageIO.hh>
38 
39 using namespace BIAS;
40 using namespace std;
41 
42 
43 int main(int argc, char *argv[])
44 {
46 
47 // used .IOR - parameters
48 // 1
49 //-999
50 //-20.30377 cam const in mm (?)
51 //-0.06444 princpx normalized or mm (?)
52 //-0.15281 princpy
53 //-2.87698e-004 k1
54 //6.73824e-007 k2
55 // .85 r0
56 //-6.23736e-010
57 // 1.97915e-005 k3
58 //-2.11616e-005 k4
59 //-1.38247e-004 ascpect
60 // 8.89027e-006 skew
61 // 23.60000 sensorsize in mm
62 // 15.80000 sensorsize in mm
63 // 3872 pixel x
64 // 2592 pixel y
65 
66  double CamConst = 20.30377; //in mm - perpendicular distance of projection center to image plane
67  double SensorSizeX = 23.6; //in mm -
68  double SensorSizeY = 15.8; //in mm
69 
70  unsigned int ImgWidth = 3872;
71  unsigned int ImgHeight = 2592;
72 
73  double principalX = (double) ImgWidth*0.5 * (1-0.06444/SensorSizeX);
74  double principalY = (double) ImgHeight*0.5 * (1-0.15281/SensorSizeY);
75  double focalX = (double)ImgWidth*0.5*(CamConst/SensorSizeX );//in pixel
76  double aspectrat = (1-1.38247e-004)*(double)ImgWidth/(double)ImgHeight;
77 
78  //ppp.SetDistortionType(DISTYPE_BROWN); //not neede anymore
79  //also set by calling
80  //SetUndistortionBrown()
81  double kc1 = -2.87698e-004;
82  double kc2 = 6.73824e-007 ;
83  double kc3 = 1.97915e-005;
84  double kc4 = -2.11616e-005;
85 
86  double r0 = .85; // second constant root of polynomial
87 
88  ppp.SetUndistortionBrown(kc1,kc2,kc3,kc4,r0);
89  ppp.SetFocalLengthAndAspect(focalX,aspectrat);
90  ppp.SetPrincipal(principalX,principalY);
91  ppp.SetIdealImageSize(ImgWidth,ImgHeight);
92 
93 #ifdef BIAS_HAVE_XML2
94  string fname = "ProjParPersExample01.xml";
95  int res = ppp.XMLWrite(fname);
96 
97  if (res!=0)
98  {
99  cerr << "error writing "<< fname << endl;
100  return -3;
101  }
102  cout << "XMLWrite wrote "<< fname << endl;
103 
104  kc1 = 0;
105  kc2 = 0;
106  kc3 = 0;
107  kc4 = 0;
108  r0 = 0;
109 
110  res = ppp.XMLRead(fname);
111  if (res!=0)
112  {
113  cerr << "error reading "<< fname << endl;
114  return -3;
115  }
116  cout << "XMLWrite read "<< fname << endl;
117 
119  switch(distype)
120  {
121  case DISTYPE_BROWN:
122  cout<<"the file contained brown distortion parameters - as expected. everything went fine."<<endl;
123  ppp.GetUndistortionBrown(kc1,kc2,kc3,kc4,r0);
124  cout<<"parameters are: kc1="<<kc1<<" "<<
125  "kc2="<<kc2<<" "<<
126  "kc3="<<kc3<<" "<<
127  "kc4="<<kc4<<" "<<
128  "r0="<<r0<<" "<<endl;
129  break;
130  default:
131  cerr<<"unexpected parameter type. check io functions!"<<endl;
132  }
133 
134 #else
135  cout<<"did not XMLWrite because BIAS_HAVE_XML2 not available."<<endl;
136 #endif
137 
138  //generate distortion images
139  ImageBase DistImgVert;
140  ImageBase DistImgHorz;
141  ImageBase DistImgMagn;
142 
143  DistImgHorz.Init(ImgWidth, ImgHeight, 1, ImageBase::ST_float);
144  DistImgVert.Init(ImgWidth, ImgHeight, 1, ImageBase::ST_float);
145  DistImgMagn.Init(ImgWidth, ImgHeight, 1, ImageBase::ST_float);
146 
147  float** pImgHorz=((float **)(DistImgHorz.GetImageDataArray()));
148  float** pImgVert=((float **)(DistImgVert.GetImageDataArray()));
149  float** pImgMagn=((float **)(DistImgMagn.GetImageDataArray()));
150 
151  for(unsigned x = 0; x<ImgWidth; x++)
152  for(unsigned y = 0; y<ImgHeight; y++)
153  {
154  HomgPoint2D point2d(x,y);
155 
156  ppp.Undistort(point2d);
157  //ppp.Distort(point2d);
158 
159  //calculate magnitude of horizontal displacement
160  float disx = (float) point2d[0] - (float) x;
161  pImgHorz[y][x*DistImgHorz.GetChannelCount()] = disx;
162  float disy = (float) point2d[1] - (float) y;
163  pImgVert[y][x*DistImgVert.GetChannelCount()] = disy;
164  float magn = sqrt((disx*disx)+(disy*disy));
165  pImgMagn[y][x*DistImgMagn.GetChannelCount()] = magn;
166  }
167 
168  if(ImageIO::Save("DisplacementX.mip",DistImgHorz)==-1)
169  {
170  cerr<<" can't save displacement image." << endl;
171  return -3;
172  }
173  if(ImageIO::Save("DisplacementY.mip",DistImgVert)==-1)
174  {
175  cerr<<" can't save displacement image." << endl;
176  return -3;
177  }
178  if(ImageIO::Save("DisplacementMagn.mip",DistImgMagn)==-1)
179  {
180  cerr<<" can't save displacement image." << endl;
181  return -3;
182  }
183 
184  cout<< " displacement images saved." << endl;
185 
186  return 0;
187 }
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
int XMLRead(const std::string &Filename)
derived classes must implement the function XMLIn which is called by this function XMLRead to read ev...
Definition: XMLBase.cpp:78
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void GetUndistortionBrown(double &kc1, double &kc2, double &kc3, double &kc4, double &r0) const
Get the lens undistortion parameters including the parameter describing root of the polynomial...
virtual void SetPrincipal(const double x, const double y)
Set principal point in pixels relative to top left corner, virtual overload to recalculate K_...
float image storage type
Definition: ImageBase.hh:118
void SetIdealImageSize(unsigned int width, unsigned int height)
void ** GetImageDataArray() const
Get an array of pointers to image data.
Definition: ImageBase.hh:305
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
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
virtual bool Undistort(BIAS::HomgPoint2D &point2d) const
Using the Matlab camera calibration toolbox parameters for an undistortion of distorted coordinates...
void SetUndistortionBrown(double kc1, double kc2, double kc3, double kc4, double r0)
Set the lens undistortion parameters including the root of the polynomial.
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
Definition: ImageBase.cpp:229
void SetFocalLengthAndAspect(double f, double AspectRatio)
Set the current camera focal length in pixel and the a spect ratio.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
BIAS_ProjParaPersp_DISTORTION_TYPE GetDistortionType() const
Get type of distortion parameters.