Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
clfGradientGauss.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 <Base/Common/BIASpragma.hh>
26 #include <OpenCLFramework/Filter/clfGradientGauss.hh>
27 #include <Base/Math/Vector.hh>
28 #include <Filter/FilterMask.hh>
29 
30 using BIAS::FilterMask;
31 
32 namespace BIAS {
33 
34  template <class InputStorageType, class OutputStorageType>
36  clfGradientGauss(clfContext *ctx) : clfGauss<InputStorageType, OutputStorageType>(ctx) {
37  GaussSigma_=1.0;
38  GaussRatio_=0.01;
39  filterMaskVert_ = ctx->CreateBuffer();
40  try {
41  program_->AddKernel("ConvolveGradGaussXY");
42  } catch (clfException &e) {
43 // cout << "no good" << endl;
44  }
45  }
46 
47  template <class InputStorageType, class OutputStorageType>
50  delete filterMaskVert_;
51  }
52 
53  template <class InputStorageType, class OutputStorageType>
56  CalculateKernels_(GaussSigma_, GaussRatio_);
57  tmpImage_->AllocateFromTemplate( *src, false, false);
58  program_->KernelSetArgument( "ConvolveGradGaussXY", 0, *src );
59  program_->KernelSetArgument( "ConvolveGradGaussXY", 1, *filterMask_ );
60  program_->KernelSetArgument( "ConvolveGradGaussXY", 2, *filterMaskVert_ );
61  program_->KernelSetArgument( "ConvolveGradGaussXY", 3, (int)winSize_ );
62  program_->KernelSetArgument( "ConvolveGradGaussXY", 4, *tmpImage_ );
63  unsigned int w,h;
64  dst->GetImageDim(w,h);
65 
66  unsigned int localx=1;
67  unsigned int localy=1;
68  if (w%8==0) localx = 8;
69  if (w%16==0) localx = 16;
70  if (h%8==0) localy = 8;
71  if (h%16==0) localy = 16;
72  context_->RunOn2DRange( *program_, "ConvolveGradGaussXY", w, h, localx, localy);
73 
74  program_->KernelSetArgument( "ConvolveGradGaussXY", 0, *tmpImage_ );
75  program_->KernelSetArgument( "ConvolveGradGaussXY", 2, *filterMask_ );
76  program_->KernelSetArgument( "ConvolveGradGaussXY", 1, *filterMaskVert_ );
77  program_->KernelSetArgument( "ConvolveGradGaussXY", 4, *dst );
78 
79  context_->RunOn2DRange( *program_, "ConvolveGradGaussXY", w, h, localx, localy);
80 
81  return 0;
82  }
83 
84  template <class InputStorageType, class OutputStorageType>
86  SetFilterVert_(unsigned int size, const float *mask) {
87  vertSize_ = size;
88 // std::cout << vertSize_ << std::endl;
89  try {
90  filterMaskVert_->Allocate( vertSize_ * sizeof(float), true, false );
91  filterMaskVert_->WriteToBuffer( (float*)mask );
92  } catch (clfException &e) {
93  BIASERR(e.GetDetailedString());
94  return 1;
95  }
96  return 0;
97  }
98 
99  template <class InputStorageType, class OutputStorageType>
101  SetFilterHoriz_(unsigned int size, const float *mask) {
103  }
104 
105  template <class InputStorageType, class OutputStorageType>
107  CalculateKernels_(double Sigma, double Ratio) {
108  if (LastSigma_ == GaussSigma_ && LastRatio_ == GaussRatio_) return;
109  LastSigma_ = GaussSigma_;
110  LastRatio_ = GaussRatio_;
111  const int hws=(int)(floor(sqrt(-2.0*Sigma*Sigma*log(Ratio))));
112  const int size=(hws<<1)+1;
113  BIAS::Vector<float> H(size), V(size);
114 
115  int i, ip, im;
116  double fac=1.0/(2.0*Sigma*Sigma);
117  register double g;
118  double sum=0.0, dsum=0.0;
119  // calculate the filter coefficients as gauss function
120  // this is a filtermask in the sense of convolution
121  // it is reflected at its symmetry center before being "dropped"
122  // onto the image !
123  for (i=0; i<=hws; i++){
124  ip=hws-i;
125  im=hws+i;
126  g=expf((float)(-(double)(i*i)*fac));
127  V[ip] = V[im] = (float) g;
128  sum += g*2.0;
129  // why, it is now not a symmetric filter??
130  g = double(i)*g;
131  H[im] = (float) g;
132  H[ip] = (float) -g;
133  dsum+=fabs(2.0*double(i)*g);
134  }
135  // added double, sub one here for consistency
136  sum -= H[hws];
137 
138  // normalize filter, so that the sum is 1.0
139  sum = 1.0 / sum;
140  dsum = 1.0 / dsum;
141  for (i=0; i<=hws; i++){
142  ip = hws+i;
143  im = hws-i;
144  H[ip] *= (float)dsum;
145  H[im] *= (float)dsum;
146  V[ip] = V[im] = V[im]*(float)sum;
147  }
148 
149 // std::cout << "horiz: " << H << std::endl << "vert: "<< V << std::endl;
150 //
151  SetFilterHoriz_(size, H.GetData());
152  SetFilterVert_(size, V.GetData());
153  }
154 
155  template class BIASOpenCLFramework_EXPORT clfGradientGauss<float, float>;
156 
157 } /* namespace DECS */
int Filter(clfImage2D *src, clfImage2D *dst)
clfBuffer * CreateBuffer()
create buffer object
Definition: clfContext.hh:79
void GetImageDim(unsigned int &width, unsigned int &height) const
Definition: clfImage2D.hh:110
clfGradientGauss(clfContext *ctx)
OpenCL Image2D wrapper.
Definition: clfImage2D.hh:46
int SetFilterHoriz_(unsigned int size, const float *mask)
double GaussRatio_
Definition: clfGauss.hh:89
const std::string & GetDetailedString() const
detailed combination of all info available
int SetFilterVert_(unsigned int size, const float *mask)
OpenCL Context wrapper.
Definition: clfContext.hh:49
int SetFilter1D_(unsigned int size, const float *mask)
Definition: clfFilter.cpp:73
clf Exception wrapper, is thrown in case of most clf errors
Definition: clfException.hh:48
T * GetData() const
get the pointer to the data array of the vector (for faster direct memory access) ...
Definition: Vector.hh:219
void AddKernel(std::string kernelname)
adds a kernel to the program.
Definition: clfProgram.cpp:158
clfProgram * program_
Definition: clfFilter.hh:59
A filter mask (or a kernel) used for convolution.
Definition: FilterMask.hh:61
virtual void CalculateKernels_(double Sigma, double Ratio)
calculates the kernel
double GaussSigma_
Definition: clfGauss.hh:88