Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasProjectionMapping.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 #include <bias_config.h>
27 
28 #ifndef BIAS_HAVE_XML2
29 # error You need XML2 to compile this code. Please enable USE_XML2 and recompile BIAS.
30 #endif // BIAS_HAVE_XML2
31 
32 
33 #include <Image/ProjectionMapping.hh>
34 #include <Geometry/Projection.hh>
35 #include <Geometry/ProjectionParametersPerspective.hh>
36 #include <Geometry/ProjectionParametersSpherical.hh>
37 #include <Base/Image/ImageIO.hh>
38 #include <Base/Image/ImageConvert.hh>
39 #include <Utils/Param.hh>
40 #include <iostream>
41 
42 using namespace BIAS;
43 using namespace std;
44 
45 
46 /**
47  * @ingroup g_tools
48  * @file biasProjectionMapping
49  * @relates ProjectionMapping
50  * @brief the user can give source image and two projections and using the
51  * projection mapping, a new image will be computed, see biasProjectionMapping.cpp
52  * @attention so far only implemented for unsigned char images and perspective projection
53  * @author sedlazeck
54  * @date 05/2010
55  */
56 int main(int argc, char** argv )
57 {
58 
59  Param *param = new Param();
60 
61  string* sourceImg = param->AddParamString("SourceImage",
62  "Absolute path to the source Image",
63  "",
64  'i');
65 
66  string* sinkImg = param->AddParamString("SinkImage",
67  "Absolute path to the sink Image",
68  "image_projection_trilinear",
69  'o');
70 
71  string* sourceCam = param->AddParamString("SourceCam",
72  "Absolute path to the source Camera calibration XML",
73  "",
74  'c');
75  string* sinkCam = param->AddParamString("SinkCam",
76  "Absolute path to the sink Camera calibration XML",
77  "",
78  's');
79 
80  bool* rescaleSourceCamToImageSize = param->AddParamBool("RescaleCam",
81  "Should source cam be scaled to fit image size?",
82  true,
83  'r');
84 
85  param->ParseCommandLine(argc,argv);
86 
87 
88 
89 
91  Image<unsigned char> src, dst;
92 
93  // read projections from files
94  BIAS::Projection source_proj;
95  int ret = source_proj.XMLRead(*sourceCam);
96  if(ret < 0){
97  BIASERR("Error reading Projection:"<<*sourceCam)
98  return -1;
99  }
100  else cout<<"Read source projection:"<<sourceCam<<endl;
101 
102  BIAS::Projection sink_proj;
103  ret = sink_proj.XMLRead(*sinkCam);
104  if(ret < 0){
105  BIASERR("Error reading Projection:"<<*sinkCam)
106  return -1;
107  }
108  else cout<<"Read sink projection:"<<sinkCam<<endl;
109 
110 
111 
112 
113 
114 
115  // load source image
116  Image<unsigned char> source;
117  Image<unsigned char> sink;
118  if (ImageIO::Load(*sourceImg, source)!=0){
119  BIASERR("error loading image "<< *sourceImg)
120  return -1;
121  }
122  if(source.GetColorModel() != ImageBase::CM_RGB){
123  cout<<"Converting to RGB:"<<endl;
124  Image<unsigned char> tmp(source);
125  ImageConvert::ToRGB(tmp,source);
126  }
127  else{
128  cout<<"Colormodel is RGB:"<<endl;
129  }
130 
131 
132  if(*rescaleSourceCamToImageSize){
133  unsigned int width = source.GetWidth();
134  unsigned int height = source.GetHeight();
135 
136  ProjectionParametersBase* ppb = source_proj.GetParameters();
137  ppb->Rescale(width, height);
138  }
139 
140 
141 
142  pm.SetSourceCam(source_proj);
143  pm.SetSinkCam(sink_proj);
144 
145  sink.Init(source.GetWidth(),source.GetHeight(),source.GetChannelCount());
146  //you can either use lookuptable
147  //pm.PrepareLookupTableMapping(src,dst,MapTrilinear);
148  //pm.MapWithLookupTable(src,dst,MapTrilinear);
149  //or map without lookuptable
150  pm.Map(source,sink,MapTrilinear);
151 
152  Image<float> disp;
153  disp.Init(source.GetWidth(),source.GetHeight(),3);
154  pm.GetDisplacementMap(disp,source);
155 
156  ImageIO::Save(*sinkImg, sink);
157  ImageIO::Save("DisplacementImage.mip", disp);
158 
159  return 0;
160 }
Routines for loading and writing all kinds of image formats.
Definition: ImageIO.hh:69
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
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()
virtual void Rescale(double ratio, const double offset=0.0)
Adapt internal parameters to resampled image.
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:305
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
Definition: Param.cpp:1028
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 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
This class Param provides generic support for parameters.
Definition: Param.hh:231
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)
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
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
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.