Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
clfGauss.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 <iostream>
26 #include "clfGauss.hh"
27 #include <Base/Math/Vector.hh>
28 #include <Base/Common/BIASpragma.hh>
29 using namespace std;
30 using namespace BIAS;
31 
32 namespace BIAS {
33 
34  template <class InputStorageType, class OutputStorageType>
36  clfGauss(clfContext *ctx) : clfFilter<InputStorageType, OutputStorageType>(ctx) {
37 
38  GaussSigma_ = 0.7;
39  GaussRatio_ = 0.01;
40 
41  LastSigma_ = 0.0;
42  LastRatio_ = 0.0;
43 
45 
46  try {
47  program_->AddKernel("ConvolveX");
48  program_->AddKernel("ConvolveY");
49  } catch (clfException &e) {
50  cout << "no good:" << e.GetMessageString()<<endl;
51  }
52 
53  }
54 
55  template <class InputStorageType, class OutputStorageType>
58 
59  }
60 
61  template <class InputStorageType, class OutputStorageType>
64  return clfFilter<InputStorageType, OutputStorageType>::FilterSep_("ConvolveX", "ConvolveY", src, dst);
65  }
66 
67  template <class InputStorageType, class OutputStorageType>
69  Build(unsigned int size) {
70  SetWinSize(size, true);
71  CalculateKernels_(GaussSigma_, GaussRatio_);
72  return 0;
73  }
74 
75  template <class InputStorageType, class OutputStorageType>
77  CalculateKernels_(double Sigma, double Ratio)
78  {
79  if (LastSigma_ == GaussSigma_ && LastRatio_ == GaussRatio_) return;
80  LastSigma_ = GaussSigma_;
81  LastRatio_ = GaussRatio_;
82 
83  const int hws = (int)(floor(sqrt(-2.0*Sigma*Sigma*log(Ratio))));
84  const int size = (hws<<1)+1;
85 
86  BIAS::Vector<float> H(size), V(size);
87 
88  double sum=0.0;
89  int i, ip, im;
90  if (Sigma==0.0){
91  H[0]=1.0; V[0]=1.0;
92  } else {
93  double fac=1.0/(2.0*Sigma*Sigma);
94  register double g;
95  // calculate the filter coefficients as gauss function
96  for (i=0; i<=hws; i++){
97  ip = hws+i;
98  im = hws-i;
99  g = expf((float)(-(double)(i*i)*fac));
100  V[ip] = V[im] = H[im] = H[ip] = (float)g;
101  sum += g*2.0;
102  }
103  sum -= V[hws]; // added twice
104 
105  // normalize filter, so that the sum is 1.0
106  sum = 1.0/sum;
107  for (i=0; i<=hws; i++){
108  ip=hws+i;
109  im=hws-i;
110  H[ip] = H[im] = V[ip] = V[im] = V[im] * (float)sum;
111  }
112  }
113 //
114 // sum = 0.0;
115 // for (i=-hws; i<=hws; i++){
116 // sum += H[i+hws];
117 // }
118 // sum = 0.0;
119 // for (i=-hws; i<=hws; i++){
120 // sum += V[i+hws];
121 // }
122 //
124  }
125 
126  template class BIASOpenCLFramework_EXPORT clfGauss<float, float>;
127 
128 } /* namespace DECS */
virtual ~clfGauss()
Definition: clfGauss.cpp:57
virtual int Filter(clfImage2D *src, clfImage2D *dst)
Definition: clfGauss.cpp:63
double LastRatio_
Definition: clfGauss.hh:92
OpenCL Image2D wrapper.
Definition: clfImage2D.hh:46
int FilterSep_(std::string nameX, std::string nameY, clfImage2D *src, clfImage2D *dst)
Definition: clfFilter.cpp:134
double GaussRatio_
Definition: clfGauss.hh:89
OpenCL Context wrapper.
Definition: clfContext.hh:49
clf Exception wrapper, is thrown in case of most clf errors
Definition: clfException.hh:48
double LastSigma_
Definition: clfGauss.hh:91
virtual void CalculateKernels_(double Sigma, double Ratio)
calculates the kernel
Definition: clfGauss.cpp:77
void AddKernel(std::string kernelname)
adds a kernel to the program.
Definition: clfProgram.cpp:158
clfProgram * program_
Definition: clfFilter.hh:59
virtual int Build(unsigned int size)
Definition: clfGauss.cpp:69
const std::string & GetMessageString() const
clf error message (usually name of failing c function)
int SetFilter_(unsigned int size, const float *mask)
Definition: clfFilter.cpp:59
double GaussSigma_
Definition: clfGauss.hh:88