Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasrescale.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 #include <Base/Image/Image.hh>
26 #include <Base/Image/ImageConvert.hh>
27 #include <Base/Image/ImageIO.hh>
28 #include <Image/Camera.hh>
29 #include <Utils/IOUtils.hh>
30 #include <Utils/Param.hh>
31 #include <Filter/Rescale.hh>
32 #include <Base/Common/FileHandling.hh>
33 
34 using namespace std;
35 using namespace BIAS;
36 
37 
38 /**
39  @file
40  @ingroup g_tools
41  @brief rescales images by given ratio (including projection parameters), see biasrescale.cpp
42  @author fkellner
43 */
44 int main(int argc, char* argv[]) {
45 
46  Param p(true);
47  double *ratio = p.AddParamDouble("ratio", "rescale ratio", 2.0, 0.1, 1000.0, 'r');
48 
49  Vector<int> *newsize = p.AddParamVecInt("size", "new size, (0 0) will use ratio", "0 0", 's');
50 
51  string *input = p.AddParamString("input", "input image or image list", "", 'i');
52  string *output = p.AddParamString("output", "output image", "", 'o');
53 
54  if(!IOUtils::ParseCommandLineEvalHelp(p, argc, argv) )
55  return 0;
56 
57  if(*input ==""){
58  BIASERR("Give input image with -i option");
59  return -1;
60  }
61  if(*output ==""){
62  BIASERR("Give ouput image with -o option");
63  return -1;
64  }
65 
66 
67  bool typeF = false;
68  bool listMode = false;
69 
74  scaler.SetFactor(*ratio);
75 
76  Camera<float> fin;
77  Camera<float> fout;
78  Rescale<float, float> fscaler;
80  fscaler.SetFactor(*ratio);
81 
82  Projection pIn;
83  ImageBase inBase;
84 
85  //check for an image list
86  std::string dir;
87  std::string base;
88  std::string suffix;
89  vector<string> intFiles;
90 
91  BIAS::FileHandling::SplitName(*input,dir,base,suffix);
92  //if suffix is list handle list
93  if(suffix==".lst"){
94  int res = BIAS::Param::ParseListFile(*input,intFiles );
95  if(res==0) listMode=true;
96  else {
97  BIASERR("Input suggest list of images, but parsing the list failes.");
98  return -1;
99  }
100  }
101  else //not a list, just pudh back one image
102  intFiles.push_back(*input);
103 
104  // now go over vector of image names
105  for(unsigned i=0;i<intFiles.size();i++){
106  ImageIO::Load(intFiles[i], inBase);
107 
108  if (inBase.GetStorageType()==ImageBase::ST_float) {
109  IOUtils::LoadCamera(&intFiles[i], fin, pIn);
110  typeF = true;
111  }
112  else if(inBase.GetStorageType()==ImageBase::ST_unsignedchar) {
113  IOUtils::LoadCamera(&intFiles[i], in, pIn);
114  typeF=false;
115  }
116  else{
117  BIASERR("Unhandled image storage type, please add switch.");
118  return -1;
119  }
120  //check if we scale to a new size or with a factor
121  //scale by factor
122  if ((*newsize)[0] == 0 || ((*newsize)[1] == 0)) {
123 
124  if (!pIn.IsEmpty())
125  pIn.Rescale(float(*ratio));
126 
127  if (typeF) {
128  fscaler.Filter(fin, fout);
129  } else {
130  scaler.Filter(in, out);
131  }
132 
133  }//end if ((*newsize)[0] == 0 || ((*newsize)[1] == 0)) {
134  //scale to new size
135  else {
136  int w = in.GetWidth();
137  int h = in.GetWidth();
138  if ((*newsize)[0] > w && (*newsize)[1] > h) {
139  if (typeF) {
140  fscaler.Upsample(fin, fout, (*newsize)[0], (*newsize)[1]);
141  } else {
142  scaler.Upsample(in, out, (*newsize)[0], (*newsize)[1]);
143  }
144  } else if ((*newsize)[0] <= w && (*newsize)[1] <= h) {
145  if (typeF) {
146  fscaler.Downsample(fin, fout, (*newsize)[0], (*newsize)[1]);
147  } else {
148  scaler.Downsample(in, out, (*newsize)[0], (*newsize)[1]);
149  }
150  } else {
151  cout << "can only Up- or Downsample" << endl;
152  }
153  if (!pIn.IsEmpty()) {
154  cout << "WARNING: Projections are only scaled in rescale-by-ratio-mode. Will leave ProjectionParameters as they are" << endl;
155  }
156  }
157 
158  string outputFile=*output;
159 
160  if(listMode){
161  BIAS::FileHandling::SplitName(*output,dir,base,suffix);
162  stringstream str;
163  str<<dir<<base<<"-"<<FileHandling::LeadingZeroString(i,5)<<suffix;
164  outputFile = str.str();
165  }
166 
167  if (!pIn.IsEmpty()) {
168  if (typeF) {
169  fout.UnsetROI();
170  IOUtils::SaveCamera(outputFile, fout, pIn);
171  } else {
172  out.UnsetROI();
173  IOUtils::SaveCamera(outputFile, out, pIn);
174  }
175  } else {
176  if (typeF) {
177  fout.UnsetROI();
178  IOUtils::SaveCamera(outputFile, fout);
179  } else {
180  out.UnsetROI();
181  IOUtils::SaveCamera(outputFile, out);
182  }
183  }
184  }
185  return 0;
186 }
virtual parent class for API definition of all (future) filters
Definition: FilterBase.hh:77
void Rescale(float ratio, unsigned int cam=0)
adapt internal params to resampled image
Definition: Projection.hh:471
int Downsample(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
generic downsample function.
Definition: Rescale.cpp:111
unsigned int GetWidth() const
Definition: ImageBase.hh:312
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
scales the src to dst using the downsampling factor from SetFactor()
Definition: Rescale.cpp:89
static void SplitName(const std::string &fullname, std::string &dir, std::string &base, std::string &suffix)
Split full path into:
void SetBorderHandling(const int bh)
Definition: FilterBase.hh:127
int Upsample(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
generic upsample function
Definition: Rescale.cpp:1157
static int ParseListFile(const std::string &ListFileName, std::vector< std::string > &LinesInFile)
Extracts lines from passed file.
Definition: Param.cpp:1853
This class Param provides generic support for parameters.
Definition: Param.hh:231
bool IsEmpty() const
Determine whether the Projection is Empty.
Definition: Projection.hh:174
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
void SetFactor(double factor)
the downsampling factor
Definition: Rescale.hh:361
void UnsetROI()
deprecated, use GetROI()-&gt;UnsetROI()
Definition: ImageBase.cpp:1057