Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleUpsampleBy2Grey.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 ExampleUpsampleBy2Grey.cpp
27  @brief Example for upsampling images by factor of 2 on grey images
28  @relates Rescale, ImageConvert
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 #include <Base/Image/Image.hh>
34 #include <Base/Image/ImageIO.hh>
35 #include <Base/Image/ImageConvert.hh>
36 #include <Filter/Rescale.hh>
37 #include <Base/Debug/TimeMeasure.hh>
38 
39 #include <Base/Common/BIASpragma.hh>
40 
41 using namespace BIAS;
42 using namespace std;
43 
44 
45 int main(int argc, char *argv[])
46 {
47 
48  cout<<"This example compares different upsampling methods for factor 2"
49  <<endl;
50 
51  if (argc<2){
52  BIASERR(argv[0]<<" <image-file>");
53  return -1;
54  }
55 
57  if (ImageIO::Load(argv[1], im)!=0){
58  BIASERR("error loading "<<argv[1]);
59  return -2;
60  }
62  Image<unsigned char> imbicubic(im.GetWidth()*2,im.GetHeight()*2,1);
63  Image<unsigned char> imbilinear(im.GetWidth()*2,im.GetHeight()*2,1);
64  Image<unsigned char> imnn(im.GetWidth()*2,im.GetHeight()*2,1);
65 
66  // automatic
68  Upsampler.UpsampleBy2Grey(im, imnn, NearestNeighbour);
69  Upsampler.UpsampleBy2Grey(im, imbilinear, Bilinear);
70  Upsampler.UpsampleBy2Grey(im, imbicubic, Bicubic);
71 
72  //if (ImageIO::Save("image_bicubic_auto.mip", imbicubic)!=0){
73  if (ImageIO::Save("image_bicubic_auto.mip", imbicubic)!=0){
74  BIASERR("error writing cubic image");
75  return -3;
76  }
77  //if (ImageIO::Save("image_bilinear_auto.mip", imbilinear)!=0){
78  if (ImageIO::Save("image_bilinear_auto.mip", imbilinear)!=0){
79  BIASERR("error writing bilinear image");
80  return -3;
81  }
82  //if (ImageIO::Save("image_nearestneighbor_auto.mip", imnn)!=0){
83  if (ImageIO::Save("image_nearestneighbor_auto.mip", imnn)!=0){
84  BIASERR("error writing bilinear image");
85  return -3;
86  }
87 
88  imbicubic.SetZero();
89  imbilinear.SetZero();
90  // manual
91  unsigned char **pDc = imbicubic.GetImageDataArray();
92  unsigned char **pDb = imbilinear.GetImageDataArray();
93 
94  double CurValue = 0;
95  const unsigned int channelcount = im.GetChannelCount();
96  for (unsigned int x=2; x<imbicubic.GetWidth()-6; x++) {
97  for (unsigned int y=2; y<imbicubic.GetHeight()-6; y++) {
98  for (unsigned int c=0; c<channelcount; c++) {
99  CurValue = rint(im.BicubicInterpolation(double(x)*0.5,
100  double(y)*0.5, c));
101  if (CurValue<0.0)CurValue = 0.0;
102  if (CurValue>255.0)CurValue = 255.0;
103  pDc[y][(x*channelcount)+c] = (unsigned char) CurValue;
104  CurValue = rint(im.BilinearInterpolation(double(x)*0.5,
105  double(y)*0.5,
106  c));
107  if (CurValue<0.0)CurValue = 0.0;
108  if (CurValue>255.0)CurValue = 255.0;
109  pDb[y][(x*channelcount)+c] = (unsigned char) CurValue;
110  }
111  }
112  }
113 
114  //if (ImageIO::Save("image_bicubic.mip", imbicubic)!=0){
115  if (ImageIO::Save("image_bicubic.mip", imbicubic)!=0){
116  BIASERR("error writing cubic image");
117  return -3;
118  }
119  //if (ImageIO::Save("image_bilinear.mip", imbilinear)!=0){
120  if (ImageIO::Save("image_bilinear.mip", imbilinear)!=0){
121  BIASERR("error writing bilinear image");
122  return -3;
123  }
124 
125  cout<<"Performing time measurements"<<endl;
126  TimeMeasure b1,b2,b3;
127  // warming up
128  Upsampler.UpsampleBy2Grey(im, imbilinear, Bilinear);
129  Upsampler.UpsampleBy2Grey(im, imbicubic, Bicubic);
130  Upsampler.UpsampleBy2Grey(im, imnn, NearestNeighbour);
131  unsigned int numberruns = 10;
132  for (unsigned int i=0; i<numberruns; i++) {
133  b1.Start();
134  Upsampler.UpsampleBy2Grey(im, imbilinear, Bilinear);
135  b1.Stop();
136  b2.Start();
137  Upsampler.UpsampleBy2Grey(im, imbicubic, Bicubic);
138  b2.Stop();
139  b3.Start();
140  Upsampler.UpsampleBy2Grey(im, imnn, NearestNeighbour);
141  b3.Stop();
142  }
143  cout<<"time nearest neighbor (real/user):"
144  <<b3.GetRealTime()/double(numberruns)
145  <<" "<<b3.GetUserTime()/double(numberruns)<<endl;
146  cout<<"time bilinear (real/user):"<<b1.GetRealTime()/double(numberruns)
147  <<" "<<b1.GetUserTime()/double(numberruns)<<endl;
148  cout<<"time bicubic (real/user):"<<b2.GetRealTime()/double(numberruns)
149  <<" "<<b2.GetUserTime()/double(numberruns)<<endl;
150 
151 
152  return 0;
153 }
double BilinearInterpolation(const double x, const double y, const unsigned short int channel=0) const
Generic bilinear interpolation.
Definition: Image.cpp:1341
gray values, 1 channel
Definition: ImageBase.hh:130
unsigned int GetWidth() const
Definition: ImageBase.hh:312
double BicubicInterpolation(const double &x, const double &y, const unsigned short int channel=0) const
Generic bicubic interpolation.
Definition: Image.cpp:1403
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
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
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
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
double GetUserTime() const
return user time (=system usage time) in msec JW For Win32: user-time is the sum over all processes o...
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111
static int IP_ToGrey(Image< StorageType > &img)
In place conversion to gray image.