Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleUndistort.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  * @example ExampleUndistort.cpp
26  * @relates UndistortionMapping
27  * @brief shows usage of the changed UndistortionMapping class
28  * @author bangerer (updated version)
29  * @date 09/2009
30  */
31 
32 
33 #include <bias_config.h>
34 
35 #ifndef BIAS_HAVE_XML2
36 # error You need XML2 to compile this code. Please enable USE_XML2 and recompile BIAS.
37 #endif // BIAS_HAVE_XML2
38 
39 
40 #include <Image/ProjectionMapping.hh>
41 #include <Geometry/Projection.hh>
42 #include <Geometry/ProjectionParametersPerspective.hh>
43 #include <Geometry/ProjectionParametersSpherical.hh>
44 #include <Base/Image/ImageIO.hh>
45 #include <Base/Image/ImageConvert.hh>
46 #include <iostream>
47 
48 using namespace BIAS;
49 using namespace std;
50 
51 
52 
53 int main(int argc, char** argv )
54 {
55  if (argc<3 ) {
56  cout <<"Usage: ExampleUndistort <image> <projection.xml>"<<endl;
57  exit(1);
58  }
59 
61  Image<unsigned char> src, dst;
62 
63  // read projection from file
64  string camfile = argv[2];
65  BIAS::Projection proj;
66  int ret = proj.XMLRead(camfile);
67  if(ret < 0){
68  BIASERR("Error reading Projection:"<<camfile);
69  return -1;
70  }
71  else cout<<"Read source projection:"<<camfile<<endl;
72 
73 
74  //decide if image is perspective or spherical
76  ppp = dynamic_cast<ProjectionParametersPerspective *>(proj.GetParameters());
77 
78  if(ppp ==NULL){
79  BIASERR("Example only implemented for perspective cameras.");
80  return -1;
81  }
82 
83 
84  BIAS::Projection projSink(proj);
85  dynamic_cast<ProjectionParametersPerspective *>(projSink.GetParameters())->
86  SetUndistortion(0.0,0.0,0.0,0.0);
87 
88  pm.SetSourceCam(proj);
89  pm.SetSinkCam(projSink);
90 
91  // load source image
92  if (ImageIO::Load(argv[1], src)!=0){
93  BIASERR("error loading image "<<argv[1]);
94  return -1;
95  }
96  if(src.GetColorModel() != ImageBase::CM_RGB){
97  cout<<"Converting to RGB:"<<endl;
98  Image<unsigned char> tmp(src);
99  ImageConvert::ToRGB(tmp,src);
100  }
101  else{
102  cout<<"Colormodel is RGB:"<<endl;
103  }
104  dst.Init(src.GetWidth(),src.GetHeight(),src.GetChannelCount());
105  //you can either use lookuptable
106  //pm.PrepareLookupTableMapping(src,dst,MapTrilinear);
107  //pm.MapWithLookupTable(src,dst,MapTrilinear);
108  //or map without lookuptable
109  pm.Map(src,dst,MapTrilinear);
110 
111  Image<float> disp;
112  disp.Init(src.GetWidth(),src.GetHeight(),3);
113  pm.GetDisplacementMap(disp,src);
114 
115  ImageIO::Save("UndistortedImage.mip", dst);
116  ImageIO::Save("DisplacementImage.mip", disp);
117  cout << " created: "<<"UndistortedImage.mip"<<endl;
118  cout << " created: "<<"DisplacementImage.mip"<<endl;
119 
120  return 0;
121 }
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 SetSinkCam(const Projection &P, const Image< float > *sinkdepth=NULL)
Set your sink projection before calling Map(),.
void SetSourceCam(const Projection &P)
Set your source projection before calling Map()
unsigned int GetWidth() const
Definition: ImageBase.hh:312
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
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
unsigned int GetHeight() const
Definition: ImageBase.hh:319
int Map(const Image< InputStorageType > &src, Image< OutputStorageType > &sink, InterpolationMethod=MapTrilinear, bool newSink=false, double SuperSampling=1.0)
backward mapping with various interpolations
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
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
int GetDisplacementMap(Image< float > &dismap, int width, int height)
precomputes lookup coordinates and computes displacement map int TEXTURE coordinates, according to the size of src (or width,height)
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.