Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExamplePMDImageIO.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  * @example ExamplePMDImageIO.cpp
27  @relates PMDImageProc, PMDImageIO
28  @brief example for usage of PMD (ToF)-ImgeIO
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 #include <iostream>
34 
35 #include <Base/Image/ImageBase.hh>
36 #include <Base/Image/ImageConvert.hh>
37 #include <Base/Image/ImageIO.hh>
38 #include <Image/PMDImageProc.hh>
39 
40 using namespace BIAS;
41 using namespace std;
42 
43 int main(int argc, char *argv[]) {
44 
45  Image<unsigned char> im, im2;
46  Image<float> dim, dim2, mcim, mcim2;
47  string PMDName = "img.tif";
48  string PMDNameXML = "";
49  PMDImageMetaData meta, meta2, metad, metad2, metadum;
50  cout<<"ExamplePMDImageIO:"<<endl;
51  if (argc>1) {
52  if (strstr(argv[1],".xml")!=NULL) {
53  PMDNameXML = argv[1];
54  } else if (strstr(argv[1],".tif")!=NULL) {
55  PMDName = argv[1];
56  } else if (strstr(argv[1],".bmp")!=NULL) {
57  PMDImageIO::LoadFromTXT(48, 64, argv[2], argv[3], argv[4],
58  im, dim, mcim, metad);
59  if (argc<=1 || ImageIO::Load(argv[1], im)!=0){
60  cerr << argv[1] << " <image>\n";
61  im.Init(640, 480, 3);
63  } else {
64  cout << "Read 2D-image:" << argv[1] << endl;
65  }
66  meta.width = im.GetWidth();
67  meta.height = im.GetHeight();
68  meta.channels = 1;
69  meta.PrincipalX = (double)im.GetWidth()/2.0;
70  meta.PrincipalY = (double)im.GetHeight()/2.0;
71  meta.FocallengthX = metad.FocallengthX*((double)im.GetWidth()/(double)dim.GetWidth());
72  meta.AspectRatio = 1;
73  meta.UndistCoeff[0] = 0;
74  meta.UndistCoeff[1] = 0;
75  meta.UndistCoeff[2] = 0;
76  meta.UndistCoeff[3] = 0;
77  meta.CameraCenter[0] = 0;
78  meta.CameraCenter[1] = 0;
79  meta.CameraCenter[2] = 0;
80  meta.CameraRotation[0] = 0;
81  meta.CameraRotation[1] = 0;
82  meta.CameraRotation[2] = 0;
83  metadum = metad;
85  meta, metadum);
86  PMDImageProc::FitDepthTo2DImage(dim, meta, metad);
87 
88  PMDImageIO::Save("img.tif", im, dim, mcim, meta, metad);
89  cout << "Wrote img.tif" << endl;
90  cout << "Metadata set: " << endl
91  << meta.PrincipalX << " " << meta.PrincipalY << " "
92  << meta.FocallengthX << " " << meta.AspectRatio << " " ;
93  } else {
94  if (argc<=1 || ImageIO::Load(argv[1], im)!=0){
95  cerr << argv[1] << " <image>\n";
96  im.Init(640, 480, 3);
98  } else {
99  cout << "Read 2D-image:" << argv[1] << endl;
100  }
101  if (argc<=2 || ImageIO::Load(argv[2], dim)!=0){
102  cerr << argv[2] << " <image>\n";
103  dim.Init(640, 480, 1, ImageBase::ST_unsignedshortint);
104  dim.FillImageWithXValue();
105  } else {
106  cout << "Read depth-image:" << argv[2] << endl;
107  }
108  if (argc<=3 || ImageIO::Load(argv[3], mcim)!=0){
109  cerr << argv[3] << " <image>\n";
110  mcim.Init(640, 480, 1, ImageBase::ST_unsignedshortint);
111  mcim.FillImageWithXValue();
112  } else {
113  cout << "Read modulation-coefficients-image:" << argv[3] << endl;
114  }
115 
116  meta.PrincipalX = 100;
117  meta.PrincipalY = 200;
118  meta.FocallengthX = 300;
119  meta.AspectRatio = 1;
120  meta.UndistCoeff[0] = 1;
121  meta.UndistCoeff[1] = 1.3;
122  meta.UndistCoeff[2] = 1.6;
123  meta.UndistCoeff[3] = 1.9;
124 
125  metad.PrincipalX = 1100;
126  metad.PrincipalY = 1200;
127  metad.FocallengthX = 1300;
128  metad.AspectRatio = 1.8;
129  metad.UndistCoeff[0] = 1.8;
130  metad.UndistCoeff[1] = 1.34;
131  metad.UndistCoeff[2] = 1.64;
132  metad.UndistCoeff[3] = 1.94;
133 
134  PMDImageIO::Save("img.tif", im, dim, mcim, meta, metad);
135  cout << "Wrote img.tif" << endl;
136  cout << "Metadata set: " << endl
137  << meta.PrincipalX << " " << meta.PrincipalY << " "
138  << meta.FocallengthX << " " << meta.AspectRatio << " " ;
139  for (int i=0; i<4; i++) {
140  cout << meta.UndistCoeff[i] << " " ;
141  }
142  cout << endl;
143  }
144  }
145 
146  if (PMDNameXML!="") {
147 #ifdef BIAS_HAVE_XML2
148  PMDImageIO::Load(PMDNameXML, im2, dim2, mcim2, meta2, metad2);
149  PMDImageIO::Save(PMDName, im2, dim2, mcim2, meta2, metad2);
150 #else
151  BIASERR("USE_XML2 switched off, cannot parse xml-file!");
152  BIASABORT;
153 #endif
154  } else {
155  PMDImageIO::Load(PMDName, im2, dim2, mcim2, meta2, metad2);
156  }
157  cout << "Read " << PMDNameXML << " or " << PMDName << endl;
158  cout << "Metadata read: " << endl
159  << meta2.PrincipalX << " " << meta2.PrincipalY << " "
160  << meta2.FocallengthX << " " << meta2.AspectRatio << " "
161  << meta2.width << " " << meta2.height << " "
162  << meta2.channels << " ";
163  cout << "|";
164  for (int i=0; i<4; i++) {
165  cout << meta2.UndistCoeff[i] << " " ;
166  }
167  cout << "|";
168  for (int i=0; i<3; i++) {
169  cout << meta2.CameraCenter[i] << " ";
170  }
171  cout << "|";
172  for (int i=0; i<3; i++) {
173  cout << meta2.CameraRotation[i] << " ";
174  }
175  cout << endl << "Metadata depth read: " << endl
176  << metad2.PrincipalX << " " << metad2.PrincipalY << " "
177  << metad2.FocallengthX << " " << metad2.AspectRatio << " "
178  << metad2.width << " " << metad2.height << " "
179  << metad2.channels ;
180 
181  cout << "|";
182  for (int i=0; i<4; i++) {
183  cout << metad2.UndistCoeff[i] << " " ;
184  }
185  cout << "|";
186  for (int i=0; i<3; i++) {
187  cout << metad2.CameraCenter[i] << " ";
188  }
189  cout << "|";
190  for (int i=0; i<3; i++) {
191  cout << metad2.CameraRotation[i] << " ";
192  }
193  cout << endl;
194 
195  if (!im2.IsEmpty())
196  ImageIO::Save("img.pgm", im2);
197  if (!dim2.IsEmpty())
198  ImageIO::Save("imgdepth.mip", dim2);
199  if (!mcim2.IsEmpty())
200  ImageIO::Save("imgModCoeff.mip", mcim2);
201 
202 }
(16bit) unsigned integer image storage type
Definition: ImageBase.hh:114
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
static int FitDepthTo2DImage(Image< float > &ImgDepth, const PMDImageMetaData &MetaData2D, PMDImageMetaData &MetaDataDepth)
convert a depth image such that the depth and the grey value at the same pixel position belong to the...
static int Save(const std::string &FileName, uint8 *Img2D, uint16 *ImgDepth, uint16 *ImgModCoeff, const PMDImageMetaData &MetaData2D, const PMDImageMetaData &MetaDataDepth)
Save a PMD-data 2d/3d-tiff image to disk.
Definition: PMDImageIO.cpp:235
unsigned int GetWidth() const
Definition: ImageBase.hh:312
double CameraRotation[3]
Definition: PMDImageIO.hh:80
static int LoadFromTXT(int width, int height, const std::string &FileName2D, const std::string &FileNameDepth, const std::string &FileNameModCoeff, Image< unsigned char > &Img2D, Image< float > &ImgDepth, Image< float > &ImgModCoeff, PMDImageMetaData &MetaDataDepth)
Load PMD-image from 3 space separated text files.
Definition: PMDImageIO.cpp:978
static int Load(const std::string &FileName, uint8 *&Img2D, uint16 *&ImgDepth, uint16 *&ImgModCoeff, PMDImageMetaData &MetaData2D, PMDImageMetaData &MetaDataDepth)
Load a PMD-data 2d/3d-tiff image from disk.
Definition: PMDImageIO.cpp:570
unsigned int GetHeight() const
Definition: ImageBase.hh:319
unsigned int channels
Definition: PMDImageIO.hh:73
void FillImageWithXValue()
fills image with value depending on x coordinate
Definition: Image.cpp:608
unsigned int width
Definition: PMDImageIO.hh:71
unsigned int height
Definition: PMDImageIO.hh:72
Store extrinsic and intrinsic camera parameters for Rotation is in axis * angle representation image ...
Definition: PMDImageIO.hh:70
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
static int FitModCoeffTo2DImage(Image< float > &ImgModCoeff, const Image< float > &ImgDepth, const PMDImageMetaData &MetaData2D, PMDImageMetaData &MetaDataDepth)
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