Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExamplePyramidImage.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 ExamplePyramidImage.cpp
28  @relates PyramidImage
29  @brief example for using the pyramid images
30  @ingroup g_examples
31  @author MIP
32 */
33 
34 #include <Base/Debug/Debug.hh>
35 #include <Base/Image/ImageConvert.hh>
36 #include <Base/Image/ImageIO.hh>
37 #include <Base/Debug/TimeMeasure.hh>
38 #include <Image/PyramidImage.hh>
39 #include <Utils/Param.hh>
40 
41 using namespace BIAS;
42 using namespace std;
43 
44 int main(int argc, char *argv[])
45 {
46  // initialize parameters
47  BIAS::Param param;
48 
49  param.DisableDestructorWarning(); // no warning at exit
50 
51  // add parameters (as unclassified parameter group)
52  param.AddParamBool("help","prints info about all parameters", false, 'h');
53  param.AddParamString("image", "image file to load", "", 'i');
54  param.AddParamString("output", "output file prefix", "", 'o');
55  param.AddParamInt("size", "pyramid size", 4, 1, 256, 's');
56  param.AddParamDouble("factor", "resampling factor per step", 2.0, 1.0, 1e6, 'f');
57 
58  // get command line parameters
59  param.ParseCommandLine(argc, argv);
60 
61  // print help information
62  if (*param.GetParamBool("help") == true) {
63  cout << endl;
64  param.Usage();
65  exit(0);
66  }
67 
68  // show image if given
69  const std::string imageName = *param.GetParamString("image");
70  if (imageName.empty()) {
71  cout << "no input image specified (try --help)" << endl;
72  return 0;
73  }
74 
75  // read and show image
77  int res = BIAS::ImageIO::Load(imageName, image);
78  if (res != 0) {
79  BIASERR("error loading image " << imageName);
80  return -1;
81  } else {
82  cout << "loaded image " << imageName << endl;
83  }
84  if (image.GetColorModel() != ImageBase::CM_Grey) {
85  ImageConvert::IP_ToGrey(image); // convert to grey image
86  }
87 
88  // initialize time measurement
89  BIAS::TimeMeasure timer;
90 
91  // initialize pyramid image
92  int pyramidSize = *param.GetParamInt("size");
93  double pyramidDownsamplingFactor = *param.GetParamDouble("factor");
95  pyramid.SetRescaleFactor(pyramidDownsamplingFactor);
96  pyramid.Init(image.GetWidth(), image.GetHeight(), 1, pyramidSize);
97  pyramid.SetZero();
98 
99  // downsampling
100  timer.Start();
101  *pyramid[0] = image;
102  pyramid.Downsample();
103  timer.Stop();
104 
105  cout << "calculation of pyramid image with size " << pyramid.size()
106  << " for image with size " << image.GetWidth() << "x"
107  << image.GetHeight() << " took " << 1e-3*timer.GetRealTime()
108  << " ms" << endl;
109 
110  // write output
111  const std::string outputPrefix = *param.GetParamString("output");
112  if (!outputPrefix.empty())
113  {
114  int res = pyramid.WriteImages(outputPrefix);
115  if (res != 0) {
116  BIASERR("error writing pyramid image " << outputPrefix);
117  } else {
118  cout << "saved pyramid image to " << outputPrefix << "-pl*.mip" << endl;
119  }
120  } else {
121  cout << "use --output to save output images to disk" << endl;
122  }
123 
124  return 0;
125 }
void SetZero()
sets all pixels in all images to zero
double * GetParamDouble(const std::string &name) const
Definition: Param.cpp:665
gray values, 1 channel
Definition: ImageBase.hh:130
unsigned size() const
deprecated interface
void DisableDestructorWarning()
Uses this just before end of your program to avoid error from destructor.
Definition: Param.hh:513
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:305
double * AddParamDouble(const std::string &name, const std::string &help, double deflt=0.0, double min=-DBL_MAX, double max=DBL_MAX, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:351
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
Definition: Param.cpp:1028
int Downsample()
downsamples from (*this)[0] assumes that all pointers e.g.
bool * GetParamBool(const std::string &name) const
Definition: Param.cpp:633
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
std::string * GetParamString(const std::string &name) const
Definition: Param.cpp:649
void Init(const Image< StorageType > &image, const unsigned py_size=0)
copy image into level 0 and create other levels according to parameters set so far (pyramidsize...
int * GetParamInt(const std::string &name) const
Definition: Param.cpp:618
unsigned int GetHeight() const
Definition: ImageBase.hh:319
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
int WriteImages(const std::string &prefix) const
writes a file in mip format per pyramid level using prefix
This class Param provides generic support for parameters.
Definition: Param.hh:231
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 * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
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
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111
void SetRescaleFactor(const double factor)
static int IP_ToGrey(Image< StorageType > &img)
In place conversion to gray image.