Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
exampleDebayerCL.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS 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 <Utils/Param.hh>
26 #include <Utils/IOUtils.hh>
27 #include <Base/Image/Image.hh>
28 #include <Base/Image/ImageIO.hh>
29 #include <Base/Image/ImageConvert.hh>
30 #include <OpenCLFramework/clfContext.hh>
31 #include <iostream>
32 #include <OpenCLFramework/Filter/clfGauss.hh>
33 #include <Filter/Mean.hh>
34 #include <Filter/Gauss.hh>
35 #include <OpenCLFramework/Filter/clfColorConversion.hh>
36 #include <OpenCLFramework/Filter/clfSimpleFilter.hh>
37 #include <Filter/StructureTensor.hh>
38 #include <FeatureDetector/CornerDetectorKLT.hh>
39 #include <Filter/GradientSimple.hh>
40 #include <Base/Debug/TimeMeasure.hh>
41 
42 
43 using namespace BIAS;
44 using namespace std;
45 
46 /**
47  * @brief mean filtering an image using opencl
48  *
49  * test program for clfImage2D objects. The kernel mean3x3 filters an image
50  *
51  */
52 int main(int argc, char* argv[]) {
53 
54  Param par;
55  string *inname = par.AddParamString("image", "image", "", 'i');
56  string *outname = par.AddParamString("out", "result", "climage.mip", 'o');
57  bool *bench = par.AddParamBool("benchmark", "benchmark 100 (default) conversions on gpu againt cpu", false, 'b');
58  int *numFrames = par.AddParamInt("numframes", "number of frames for benchmark", 100, 1, 10000, 'n');
59 
60  if (IOUtils::ParseCommandLineEvalHelp(par, argc, argv) == 0) {
61  par.Usage();
62  }
63 
66  ImageIO::Load(*inname, in);
67 
68  unsigned int w = in.GetWidth();
69  unsigned int h = in.GetHeight();
70 
71  out.Init(w,h,4);
72 
73  try {
74 
75  // create context on first GPU found
76  clfContext context(false);
77 
78  clfColorConversion convert(&context);
79 
80  clfImage2D *greyImage = context.CreateImage2D();
81  clfImage2D *dstImage = context.CreateImage2D();
82  greyImage->AllocateFromBiasImage(in, true, false, true);
83  dstImage->AllocateFromBiasImage(out, false, true);
84 
86  cout << "doing bayer rggb to rgb" << endl;
87  convert.UCharBayerToRGBA(greyImage, dstImage, ImageBase::CM_Bayer_RGGB);
88 
89  if (*bench) {
90  // benchmark
91  TimeMeasure tm;
92  tm.Reset();
93  tm.Start();
94  for (int i=0;i<*numFrames;i++) {
95  greyImage->WriteToImage( in.GetImageData() );
96  convert.UCharBayerToRGBA(greyImage, dstImage, ImageBase::CM_Bayer_RGGB);
97  dstImage->CopyToBiasImage(out);
98  }
99  tm.Stop();
100  tm.Print(cout);
101  double gputime = tm.GetRealTime();
102  tm.Reset();
104  cpu.Init(w,h,3);
105  tm.Start();
106  for (int i=0;i<*numFrames;i++) {
107  ImageConvert::ToRGB(in , cpu);
108  }
109  tm.Stop();
110  tm.Print(cout);
111  double cputime = tm.GetRealTime();
112 
113  cout << "GPU conversion runs at " << (1e6) / (gputime/(float)(*numFrames)) << " frames/sec." << endl;
114  cout << "CPU conversion runs at " << (1e6) / (cputime/(float)(*numFrames)) << " frames/sec." << endl;
115  cout << "GPU speed factor: " << cputime/gputime << "x"<<endl;
116  }
117 
118  } else if (in.GetColorModel() == ImageBase::CM_YUYV422) {
119  cout << "doing yuyv422 to rgb" << endl;
120  convert.UCharYUYV422ToRGBA(greyImage, dstImage);
121 
122  if (*bench) {
123  // benchmark
124  TimeMeasure tm;
125  tm.Reset();
126  tm.Start();
127  for (int i=0;i<*numFrames;i++) {
128  greyImage->WriteToImage( in.GetImageData() );
129  convert.UCharYUYV422ToRGBA(greyImage, dstImage);
130  dstImage->CopyToBiasImage(out);
131  }
132  tm.Stop();
133  tm.Print(cout);
134  double gputime = tm.GetRealTime();
135  tm.Reset();
137  cpu.Init(w,h,3);
138  tm.Start();
139  for (int i=0;i<*numFrames;i++) {
140  ImageConvert::ToRGB(in , cpu);
141  }
142  tm.Stop();
143  tm.Print(cout);
144  double cputime = tm.GetRealTime();
145 
146  cout << "GPU conversion runs at " << (1e6) / (gputime/(float)(*numFrames)) << " frames/sec." << endl;
147  cout << "CPU conversion runs at " << (1e6) / (cputime/(float)(*numFrames)) << " frames/sec." << endl;
148  cout << "GPU speed factor: " << cputime/gputime << "x"<<endl;
149  }
150  }
151 
152  dstImage->CopyToBiasImage(out);
153  } catch (clfException &err) {
154  // the detailed error string
155  cout << err.GetDetailedString() << endl;
156  }
157  // save result
158  ImageIO::Save(*outname, out);
159 
160  return 0;
161 }
YUYV422, 2 channels, full luminance Y, subsampled half U,V.
Definition: ImageBase.hh:133
void Print(std::ostream &os=std::cout) const
OpenCL Image2D wrapper.
Definition: clfImage2D.hh:46
Bayer_RGGB, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:143
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:305
unsigned int GetWidth() const
Definition: ImageBase.hh:312
void AllocateFromBiasImage(const BIAS::ImageBase &image, bool readonly=false, bool writeonly=false, bool copy=false)
Allocation of a memory buffer as 2D image, either call directly or use wrapper for BIAS::ImageBase...
Definition: clfImage2D.cpp:123
const std::string & GetDetailedString() const
detailed combination of all info available
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
OpenCL Context wrapper.
Definition: clfContext.hh:49
clf Exception wrapper, is thrown in case of most clf errors
Definition: clfException.hh:48
unsigned int GetHeight() const
Definition: ImageBase.hh:319
static bool ParseCommandLineEvalHelp(Param &params, int argc, char *argv[])
parses the command line, adds parameter &quot;help&quot;
Definition: IOUtils.cpp:176
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...
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
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
This class Param provides generic support for parameters.
Definition: Param.hh:231
void CopyToBiasImage(BIAS::ImageBase &image, unsigned int originX=0, unsigned int originY=0, unsigned int regionX=0, unsigned int regionY=0)
Definition: clfImage2D.cpp:283
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
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111
void WriteToImage(const void *data, unsigned int originX=0, unsigned int originY=0, unsigned int regionX=0, unsigned int regionY=0, bool blocking=true)
write from host memory to image
Definition: clfImage2D.cpp:195