Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IOUtils.hh
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 #ifndef _IO_UTILS__HH_
26 #define _IO_UTILS__HH_
27 
28 #include <bias_config.h>
29 #include <Utils/Param.hh>
30 #include <Base/Image/ImageIO.hh>
31 #include <Base/Image/MetaData.hh>
32 #include <Image/Camera.hh>
33 #include <Geometry/Projection.hh>
34 #include <iostream>
35 #include <sstream>
36 
37 namespace BIAS{
38  /**
39  * \class IOUtils
40  * \brief Class grouping together some IO code often used in (small) application,
41  * like loading images, parsing their meta data etc.
42  * \ingroup g_utils
43  * \author bartczak 01/2008
44  */
45  class BIASUtils_EXPORT IOUtils {
46  public:
47 
48  /** Takes in the name of a text file, that contains an image file-name per line, tries to load these
49  * images into the vector.
50  * \returns false if not usccesfull in loading any image.
51  **/
52  template<class type>
53  static bool LoadImageList(const std::string& listFileName,
54  std::vector<Image<type> >& imgs,
55  const bool verbose = false) {
56  std::vector<std::string> imageNameList;
57  if(Param::ParseListFile(listFileName, imageNameList)!=0) {
58  BIASERR("could not read list file");
59  return false;
60  }
61  imgs.clear();
62  unsigned int num = imageNameList.size();
63  imgs.resize(num);
64  for(unsigned int i=0; i<num; i++) {
65  if(verbose) std::cout<<"loading "<<imageNameList[i]<<"... "<<std::flush;
66  if(ImageIO::Load(imageNameList[i], imgs[i])!=0){
67  if(verbose) std::cout<<"failed!\n";
68  imgs.clear();
69  return false;
70  }
71  if(verbose) std::cout<<"ok\n";
72  }
73  return true;
74  }
75 
76 #ifdef BIAS_HAVE_XML2
77  template<class type>
78  static bool LoadImageList(const std::string& listFileName,
79  std::vector<Image<type> >& imgs,
80  std::vector<ProjectionParametersPerspective*>& ppp,
81  const bool verbose = false) {
82  std::vector<std::string> imageNameList;
83  if(Param::ParseListFile(listFileName, imageNameList)!=0) {
84  BIASERR("could not read list file");
85  return false;
86  }
87  imgs.clear();
88  unsigned int num = imageNameList.size();
89  imgs.resize(num);
90  ppp.resize(num, NULL);
91  for(unsigned int i=0; i<num; i++) {
92  if(verbose) std::cout<<"loading "<<imageNameList[i]<<"... "<<std::flush;
93  if(!IOUtils::LoadCamera(&(imageNameList[i]), imgs[i], ppp[i])){
94  if(verbose) std::cout<<"failed!\n";
95  imgs.clear();
96  for(unsigned int j=0; j<i; j++) {
97  delete ppp[j];
98  }
99  ppp.clear();
100  return false;
101  }
102  if(verbose) std::cout<<"ok\n";
103  }
104  return true;
105  }
106 
107 
108  static bool LoadImageList(const std::string& listFileName,
109  std::vector<ImageBase>& imgs,
110  std::vector<ProjectionParametersPerspective*>& ppp,
111  const bool verbose = false) {
112  std::vector<std::string> imageNameList;
113  if(Param::ParseListFile(listFileName, imageNameList)!=0) {
114  BIASERR("could not read list file");
115  return false;
116  }
117  imgs.clear();
118  unsigned int num = (unsigned int)imageNameList.size();
119  imgs.resize(num);
120  ppp.resize(num, NULL);
121  for(unsigned int i=0; i<num; i++) {
122  if(verbose) std::cout<<"loading "<<imageNameList[i]<<"... "<<std::flush;
123  if(!IOUtils::LoadCamera(&(imageNameList[i]), imgs[i], ppp[i])){
124  if(verbose) std::cout<<"failed!\n";
125  imgs.clear();
126  for(unsigned int j=0; j<i; j++) {
127  delete ppp[j];
128  }
129  ppp.clear();
130  return false;
131  }
132  if(verbose) std::cout<<"ok\n";
133  }
134  return true;
135  }
136 
137  static bool LoadImageList(const std::string& listFileName,
138  std::vector<ImageBase>& imgs,
139  std::vector<ProjectionParametersPerspective>& ppp,
140  const bool verbose = false) {
141  std::vector<ProjectionParametersPerspective*> pppPntr;
142  if(!LoadImageList(listFileName, imgs, pppPntr, verbose)) return false;
143 
144  ppp.clear();
145  ppp.reserve(pppPntr.size());
146  for(unsigned int i=0; i<pppPntr.size(); i++)
147  ppp.push_back(*(pppPntr[i]));
148 
149  return true;
150  }
151 
152 #endif
153 
154  /** Appends 2D points read in from file to vector.
155  **/
156  static bool ReadIn2DPointList(const std::string& fileName,
157  std::vector<BIAS::Vector2<int> >& pointLst);
158 
159  static bool ReadIn2DPointList(const std::string& fileName,
160  std::vector<unsigned int>& xPos,
161  std::vector<unsigned int>& yPos);
162 
163  template<class T>
164  static bool Write2DPointWithDatumList(const std::string& fileName,
165  std::vector<unsigned int>& xPos,
166  std::vector<unsigned int>& yPos,
167  std::vector<T>& datum) {
168 
169  BIASASSERT(xPos.size() == yPos.size());
170  BIASASSERT(xPos.size() == datum.size());
171 
172  std::ofstream ofs(fileName.c_str());
173  if (!ofs) {
174  ofs.close();
175  return false; // error, could not open file.
176  }
177  else {
178  for(unsigned int i=0; i<xPos.size(); i++) {
179 
180  if(!ofs) {
181  ofs.close();
182  return false;
183  }
184  ofs << xPos[i] <<"\t";
185  ofs << yPos[i]<<"\t";
186  ofs << datum[i]<<"\n";
187 
188  }
189  ofs.close();
190  return true;
191 
192  }
193  }
194 
195 
196  /**
197  * @brief Reads a list of 2D points from a text file and appends them to the
198  * given vector.
199  *
200  * @param fileName
201  * the name of the file to read from
202  * @param pointList
203  * the list to add the points, note that it is not cleared so the
204  * points are appended
205  *
206  * @return true on success or false if the file was not found
207  */
208  template<class T>
209  static bool ReadIn2DPointList(const std::string &fileName,
210  std::vector<BIAS::Vector2<T> > &pointList) {
211  std::ifstream ifs(fileName.c_str());
212 
213  if (!ifs) { // could not open file
214  ifs.close();
215  return false;
216  }
217 
218  Vector2<T> pt;
219 
220  while (ifs) {
221  ifs >> pt[0];
222  ifs >> pt[1];
223 
224  pointList.push_back(pt);
225  }
226 
227  ifs.close();
228  return true;
229  }
230 
231 
232  /**
233  * @brief Reads a list of 3D points from a text file and appends them to the
234  * given vector.
235  *
236  * @param fileName
237  * the name of the file to read from
238  * @param pointList
239  * the list to add the points, note that it is not cleared so the
240  * points are appended
241  *
242  * @return true on success or false if the file was not found
243  */
244  template<class T>
245  static bool ReadIn3DPointList(const std::string &fileName,
246  std::vector<BIAS::Vector3<T> > &pointList) {
247  std::ifstream ifs(fileName.c_str());
248 
249  if (!ifs) { // could not open file
250  ifs.close();
251  return false;
252  }
253 
254  Vector3<T> pt;
255 
256  while (ifs) {
257  ifs >> pt[0];
258  ifs >> pt[1];
259  ifs >> pt[2];
260 
261  pointList.push_back(pt);
262  }
263 
264  ifs.close();
265  return true;
266  }
267 
268 
269  /** Will take outname which is assumed to be of path/base.ext structure and will
270  build path/base_extension.suffix from it.
271  **/
272  static std::string ExtendOutName(const std::string& outname, const std::string& suffix,
273  const std::string& extension);
274 
275  /** Will take outname which is assumed to be of path/base.ext structure and will
276  build path/base_extension.ext from it.
277  **/
278  static std::string ExtendOutName(const std::string& outname, const std::string& extension);
279 
280 
281  /** Seperates out the paths that are contained in a list of
282  * directories that are stored in an environment variable
283  **/
284  static bool ExtractSubPaths(const std::string& environmentVariable,
285  std::vector<std::string>& subPaths);
286 
287  /** Searches for the file specified by baseFileName in the
288  * specified searchPaths.
289  * \param searchPaths is a string like returned by getenv, i.e. a path list seperated by a colon
290  * for Linux or a path list seperated by a semicolon for windows
291  * \param returns true if file was found;
292  **/
293  static bool FindFile(const std::string& searchPaths,
294  const std::string& baseFileName,
295  std::string& fullFileName);
296 
297  /** Like FindFile() but reads out the searchPath from the specified environmentVariable.
298  **/
299  static bool FindFileEnv(const std::string& environmentVariable,
300  const std::string& baseFileName,
301  std::string& fullFileName);
302 
303  /** Convenience wrapper to using SHADERSRC environment variable
304  */
305  static bool FindShaderFile(const std::string& shaderFileName, std::string& res);
306 
307  static bool LoadImage(
308  const std::string& fileName,
309  ImageBase& img,
310  unsigned int& width,
311  unsigned int& height,
312  unsigned int& channels);
313 
314  template<class dataType>
315  static bool LoadCamera(std::string* fileName,
317  {
318  if(ImageIO::Load(*fileName, cam)!=0) {
319  BIASERR("could not load camera: "<<*fileName);
320  return false;
321  }
322  return true;
323  }
324 
325  template<class dataType>
326  static bool LoadCamera(const std::string& fileName,
328  {
329  if(ImageIO::Load(fileName, cam)!=0) {
330  BIASERR("could not load camera: "<<fileName);
331  return false;
332  }
333  return true;
334  }
335 
336 
337  template<class dataType>
338  static bool LoadCamera(std::string* fileName,
340  BIAS::Projection& proj)
341  {
342  if(!LoadCamera<dataType>(fileName, cam))
343  return false;
344 
345  if(cam.ParseMetaData()<0) {
346  BIASERR("could not parse metadata from file: "<<*fileName);
347  return false;
348  }
349  proj = cam.GetProj();
350  return true;
351  }
352 
353  template<class dataType>
354  static bool LoadCamera(const std::string& fileName,
356  BIAS::Projection& proj)
357  {
358  if(!LoadCamera<dataType>(fileName, cam))
359  return false;
360 
361  if(cam.ParseMetaData()<0) {
362  BIASERR("could not parse metadata from file: "<<fileName);
363  return false;
364  }
365  proj = cam.GetProj();
366  return true;
367  }
368 
369  template<class dataType>
370  static bool LoadCamera(const std::string& fileName,
372  BIAS::Projection& proj,
373  unsigned int& width,
374  unsigned int& height,
375  unsigned int& channels)
376  {
377  std::string fileNameBak = fileName;
378  if(!LoadCamera<dataType>(&fileNameBak, cam, proj))
379  return false;
380 
381  width = cam.GetWidth();
382  height = cam.GetHeight();
383  channels = cam.GetChannelCount();
384 
385  return true;
386  }
387 
388  template<class dataType>
389  static bool SaveCamera(const std::string& fileName,
390  const BIAS::Camera<dataType>& cam)
391  {
392  if(ImageIO::Save(fileName, cam)!=0) {
393  BIASERR("could not save camera: "<<fileName);
394  return false;
395  }
396  return true;
397  }
398 
399  template<class dataType>
400  static bool SaveCamera(const std::string& fileName,
402  const BIAS::Projection& proj)
403  {
404  cam.SetProj(proj);
405  cam.UpdateMetaData();
406  if(!SaveCamera<dataType>(fileName, cam))
407  return false;
408 
409  return true;
410  }
411 
412  /** \brief parses the command line, adds parameter "help" */
413  static bool ParseCommandLineEvalHelp(Param& params,
414  int argc, char* argv[]);
415  /** \brief parses the command line, adds parameter "help"
416  * and return first unknown parameter in fup */
417  static bool ParseCommandLineEvalHelp(Param& params,
418  int argc, char* argv[],
419  int& fup);
420 
421 
422  template<class ptType, class inPtType>
423  static bool CheckType(inPtType* checked, ptType*& result) {
424  result = dynamic_cast<ptType*>(checked);
425  if(result == NULL) {
426  return false;
427  }
428  return true;
429  }
430 
431  template<class ptType, class inPtType>
432  static bool CheckTypeDelete(inPtType* checked, ptType*& result) {
433  result = dynamic_cast<ptType*>(checked);
434  if(result == NULL) {
435  delete [] checked;
436  return false;
437  }
438  return true;
439  }
440 
441  static bool LoadParameters(const std::string* fileName,
443  Projection tempProj;
444  if(tempProj.Load(*fileName)!=0) {
445  BIASERR("could not load projection from "<<fileName);
446  return false;
447  }
448  if(!CheckTypeDelete(tempProj.GetParameters()->Clone(),ppp)) {
449  BIASERR("projection file is not of type projection parameters perspective!");
450  return false;
451  }
452  return true;
453  }
454 
455  static void PrintWithLineNumbers(const std::string& text, std::ostream& out = std::cout);
456 
457  static int GetApplicationMemory() {
458 #ifndef WIN32
459  long int totalmemory = 0;
460  std::ifstream memfile("/proc/self/statm");
461  memfile >> totalmemory;
462  return totalmemory;
463 #else
464  return 0;
465 #endif
466  }
467 
468 #ifdef BIAS_HAVE_XML2
469  /** This is the implementation found in Camera::ParseMetaData() !
470  * It allows to extract the Projection from ImageBase without the unconvenient
471  * type determination.
472  * \returns true if a valid pose was found.
473  **/
474  static bool GetProjection(ImageBase& imgBase, Projection& projOut);
475  static bool GetProjectionParametersPerspective(ImageBase& imgBase,
477  static bool LoadCamera(const std::string* fileName,
478  BIAS::ImageBase& cam,
479  BIAS::Projection& proj)
480  {
481  if(ImageIO::Load(*fileName, cam)!=0) {
482  BIASERR("could not load the image file! : "<<*fileName);
483  return false;
484  }
485 
486  if(!GetProjection(cam, proj)) {
487  BIASERR("could not retrive projection.");
488  return false;
489  }
490 
491  return true;
492  }
493 
494  static bool LoadCamera(const std::string* fileName,
495  BIAS::ImageBase& cam,
497  {
498  Projection tempProj;
499  if(!LoadCamera(fileName, cam, tempProj)) return false;
500  if(!CheckTypeDelete(tempProj.GetParameters()->Clone(),ppp)) {
501  BIASERR("meta data do not contain projection parameters perspective!");
502  return false;
503  }
504  return true;
505  }
506 
507  template<class dataType>
508  static bool LoadCamera(const std::string& fileName,
511  unsigned int& width,
512  unsigned int& height,
513  unsigned int& channels)
514  {
515  std::string fileNameBak = fileName;
516  if(!LoadCamera(&fileNameBak, cam, proj))
517  return false;
518 
519  width = cam.GetWidth();
520  height = cam.GetHeight();
521  channels = cam.GetChannelCount();
522 
523  return true;
524  }
525 
526  static bool LoadCamera(const std::string* fileName,
527  BIAS::ImageBase& cam,
529  {
530  Projection tempProj;
531  if(!LoadCamera(fileName, cam, tempProj)) return false;
532  if(!CheckTypeDelete(tempProj.GetParameters()->Clone(),ppp)) {
533  BIASERR("meta data do not contain projection parameters perspective depth!");
534  return false;
535  }
536  return true;
537  }
538 
539  template<class dataType>
540  static bool LoadCamera(const std::string& fileName,
543  unsigned int& width,
544  unsigned int& height,
545  unsigned int& channels)
546  {
547  std::string fileNameBak = fileName;
548  if(!LoadCamera(&fileNameBak, cam, proj))
549  return false;
550 
551  width = cam.GetWidth();
552  height = cam.GetHeight();
553  channels = cam.GetChannelCount();
554 
555  return true;
556  }
557 
558 
559 
560 
561 #endif //BIAS_HAVE_XML2
562 
563  struct BIASUtils_EXPORT ProgressIndicator {
564 
565  ProgressIndicator() : indicatorState(0) {};
566  void Reset();
567  void operator++(int);
568  unsigned int indicatorState;
569  };
570 
571  class BIASUtils_EXPORT ProgressBar {
572  public:
573  ProgressBar(bool cl=false) :
574  clear_(cl), incrementThreshold_(1), incrementPos_(0) {};
575  void Init(const int steps);
576  /** From **/
577  void Init(const int steps, const int maxLength);
578  void operator++(int);
579 
580  private:
581  bool clear_;
582  unsigned int incrementThreshold_;
583  unsigned int incrementPos_;
584 
585  };
586 
587  static bool LoadFloat(const std::string& filename, BIAS::Image<float>& outcome);
588 
589  static bool SaveForPlot(const std::string& filename, const std::vector<double>& x_vector, const std::vector<double>& y_vector);
590 
591  };
592 
593  } //end of namespace
594 
595 #endif
static bool LoadCamera(const std::string *fileName, BIAS::ImageBase &cam, BIAS::ProjectionParametersPerspective *&ppp)
Definition: IOUtils.hh:494
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
additional depth calibration parameters for a perspective depth camera to be used for ToF-(PMD) camer...
static int GetApplicationMemory()
Definition: IOUtils.hh:457
extends the Image by MetaData support (e.g.
Definition: Camera.hh:74
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
static bool LoadCamera(const std::string &fileName, BIAS::Camera< dataType > &cam, BIAS::ProjectionParametersPerspective *&proj, unsigned int &width, unsigned int &height, unsigned int &channels)
Definition: IOUtils.hh:508
static bool LoadCamera(std::string *fileName, BIAS::Camera< dataType > &cam)
Definition: IOUtils.hh:315
virtual ProjectionParametersBase * Clone() const =0
Covariant virtual copy constructor used in BIAS::Projection.
static bool LoadCamera(const std::string &fileName, BIAS::Camera< dataType > &cam, BIAS::Projection &proj)
Definition: IOUtils.hh:354
static bool LoadImageList(const std::string &listFileName, std::vector< Image< type > > &imgs, std::vector< ProjectionParametersPerspective * > &ppp, const bool verbose=false)
Definition: IOUtils.hh:78
unsigned int GetWidth() const
Definition: ImageBase.hh:312
static bool ReadIn2DPointList(const std::string &fileName, std::vector< BIAS::Vector2< T > > &pointList)
Reads a list of 2D points from a text file and appends them to the given vector.
Definition: IOUtils.hh:209
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
static bool ReadIn3DPointList(const std::string &fileName, std::vector< BIAS::Vector3< T > > &pointList)
Reads a list of 3D points from a text file and appends them to the given vector.
Definition: IOUtils.hh:245
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
static bool LoadCamera(std::string *fileName, BIAS::Camera< dataType > &cam, BIAS::Projection &proj)
Definition: IOUtils.hh:338
Class grouping together some IO code often used in (small) application, like loading images...
Definition: IOUtils.hh:45
static bool Write2DPointWithDatumList(const std::string &fileName, std::vector< unsigned int > &xPos, std::vector< unsigned int > &yPos, std::vector< T > &datum)
Definition: IOUtils.hh:164
static bool LoadImageList(const std::string &listFileName, std::vector< Image< type > > &imgs, const bool verbose=false)
Takes in the name of a text file, that contains an image file-name per line, tries to load these imag...
Definition: IOUtils.hh:53
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
int SetProj(const Projection &Proj)
Definition: Camera.hh:106
unsigned int GetHeight() const
Definition: ImageBase.hh:319
static bool LoadCamera(const std::string &fileName, BIAS::Camera< dataType > &cam)
Definition: IOUtils.hh:326
static int ParseListFile(const std::string &ListFileName, std::vector< std::string > &LinesInFile)
Extracts lines from passed file.
Definition: Param.cpp:1853
The image template class for specific storage types.
Definition: Image.hh:78
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
static bool CheckType(inPtType *checked, ptType *&result)
Definition: IOUtils.hh:423
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
int UpdateMetaData()
copy P_ and co.
Definition: Camera.cpp:446
This class Param provides generic support for parameters.
Definition: Param.hh:231
static bool SaveCamera(const std::string &fileName, const BIAS::Camera< dataType > &cam)
Definition: IOUtils.hh:389
static bool LoadCamera(const std::string &fileName, BIAS::Camera< dataType > &cam, BIAS::Projection &proj, unsigned int &width, unsigned int &height, unsigned int &channels)
Definition: IOUtils.hh:370
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
static bool LoadCamera(const std::string &fileName, BIAS::Camera< dataType > &cam, BIAS::ProjectionParametersPerspectiveDepth *&proj, unsigned int &width, unsigned int &height, unsigned int &channels)
Definition: IOUtils.hh:540
ProgressBar(bool cl=false)
Definition: IOUtils.hh:573
static bool LoadImageList(const std::string &listFileName, std::vector< ImageBase > &imgs, std::vector< ProjectionParametersPerspective > &ppp, const bool verbose=false)
Definition: IOUtils.hh:137
static bool CheckTypeDelete(inPtType *checked, ptType *&result)
Definition: IOUtils.hh:432
const BIAS::Projection & GetProj() const
Definition: Camera.hh:109
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...
Definition: Camera.cpp:154
static bool LoadCamera(const std::string *fileName, BIAS::ImageBase &cam, BIAS::ProjectionParametersPerspectiveDepth *&ppp)
Definition: IOUtils.hh:526
static bool LoadImageList(const std::string &listFileName, std::vector< ImageBase > &imgs, std::vector< ProjectionParametersPerspective * > &ppp, const bool verbose=false)
Definition: IOUtils.hh:108
static bool LoadParameters(const std::string *fileName, BIAS::ProjectionParametersPerspective *&ppp)
Definition: IOUtils.hh:441
static bool LoadCamera(const std::string *fileName, BIAS::ImageBase &cam, BIAS::Projection &proj)
Definition: IOUtils.hh:477
static bool SaveCamera(const std::string &fileName, BIAS::Camera< dataType > &cam, const BIAS::Projection &proj)
Definition: IOUtils.hh:400