Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HessianGauss.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 #include "HessianGauss.hh"
27 #include "Convolution.hh"
28 
29 using namespace BIAS;
30 using namespace std;
31 
32 //////////////////////////////////////////////////////////////////////////
33 // implementation
34 //////////////////////////////////////////////////////////////////////////
35 
36 template <class StorageType, class OutputStorageType>
38 HessianGauss() : _ConvXX(), _ConvYY(), _ConvXY()
39 {
40  _LastSigma=-DBL_MAX;
41  _HessGaussSigma=1.0;
42  _HessGaussRatio=0.01;
43 
44 }
45 
46 template <class StorageType, class OutputStorageType>
49  : _HessGaussSigma(other._HessGaussSigma),
50  _HessGaussRatio(other._HessGaussRatio),
51  _LastSigma(other._LastSigma),
52  _LastRatio(other._LastRatio),
53  _ConvXX(other._ConvXX),
54  _ConvYY(other._ConvYY),
55  _ConvXY(other._ConvXY)
56 {
57 }
58 
59 template <class InputStorageType, class OutputStorageType>
61 }
62 
63 
64 template <class InputStorageType, class OutputStorageType>
68 {
69  // _ConvXX.SetDebugLevel(D_FILTERBASE_CALLSTACK|D_CONV_TYPES|D_WRITE_IMAGES);
70 // _ConvYY.SetDebugLevel(D_FILTERBASE_CALLSTACK|D_CONV_TYPES|D_WRITE_IMAGES);
71 // _ConvXY.SetDebugLevel(D_FILTERBASE_CALLSTACK|D_CONV_TYPES|D_WRITE_IMAGES);
72  BIASCDOUT(D_FILTERBASE_CALLSTACK, "HessianGauss::Filter(src, hxx, gyy, hxy)\n");
73 
74  if ((fabs(_HessGaussSigma-_LastSigma)>=DBL_EPSILON) ||
75  (fabs(_HessGaussRatio-_LastRatio)>=DBL_EPSILON)){
76  _CalculateKernels(_HessGaussSigma, _HessGaussRatio);
77  }
78  if (!src.SamePixelAndChannelCount(hxy)){
79  if (!hxy.IsEmpty()) hxy.Release();
80  hxy.Init(src.GetWidth(), src.GetHeight(), src.GetChannelCount());
81  }
82 
83  if (!hxx.SamePixelAndChannelCount(src)){
84  hxx.Release();
85  hxx.Init(src.GetWidth(), src.GetHeight(), src.GetChannelCount());
86  }
87  if (!hyy.SamePixelAndChannelCount(src)){
88  hyy.Release();
89  hyy.Init(src.GetWidth(), src.GetHeight(), src.GetChannelCount());
90  }
91 
92  int res =0;
93  if ((res =_ConvXX.Filter(src, hxx))!=0) {
94  BIASERR("Error in hessian xx computation...");
95  return -1;
96  }
97  if ((res = _ConvYY.Filter(src, hyy))!=0) {
98  BIASERR("Error in hessian yy computation...");
99  return -1;
100  }
101  //now convolute with xy kernel for 3rd image
102  if ((res = _ConvXY.Filter(src, hxy))!=0) {
103  BIASERR("Error in hessian xy computation...");
104  return -1;
105  }
106 
107  return (res==0)?0:-1;
108 }
109 
110 template <class InputStorageType, class OutputStorageType>
112 _CalculateKernels(double Sigma, double Ratio)
113 {
114  const int hws=(int)(floor(sqrt(-2.0*Sigma*Sigma*log(Ratio))));
115  const int size=(hws<<1)+1;
116  Matrix<FM_FLOAT> Mxx(size, size);
117  Matrix<FM_FLOAT> Myy(size, size);
118  Matrix<FM_FLOAT> Mxy(size, size);
119 
120  BIASCDOUT(D_HESSGAUSS_KERNEL, "HWS = "<<hws<<"\tSize = "<<size<<endl);
121  BIASCDOUT(D_HESSGAUSS_KERNEL, "Sigma = "<<Sigma<<"\tRatio = "<<Ratio<<endl);
122  BIASCDOUT(D_HESSGAUSS_KERNEL, "size = "<<size<<"\thws = "<<hws<<endl);
123 
124  // calculate the filter coefficients as second derivative of gauss function
125  // this is a filtermask in the sense of convolution
126  double fac=-1.0/(2.0*Sigma*Sigma);
127  //normalization factor
128  register double N = 1.0/(Sigma*sqrt(2*M_PI));
129  register double gxx=0,gxy=0,gyy=0;
130  register double g1=0,gxx2=0,gyy2=0;
131  register double eTerm=0;
132  //int xp,xm,yp,ym;
133  // first compute hessian
134  for (int x=-hws; x<=hws; x++){
135  for (int y=-hws; y<=hws; y++ ){
136  //xp= hws+x;
137  //xm= hws-x;
138  //yp= hws+y;
139  //ym= hws-y;
140  eTerm = expf((float)((double)((x*x)+(y*y))*fac));
141  g1=-N/(Sigma*Sigma)*eTerm;
142  gxx2=(N*x*x)/(Sigma*Sigma*Sigma*Sigma)*eTerm;
143  gyy2=(N*y*y)/(Sigma*Sigma*Sigma*Sigma)*eTerm;
144  gxx=g1+gxx2;
145  gyy=g1+gyy2;
146  gxy=(N*x*y)/(Sigma*Sigma*Sigma*Sigma)*eTerm;
147  Mxx[x+hws][y+hws]= (FM_FLOAT)gxx;
148  Myy[x+hws][y+hws]= (FM_FLOAT)gyy;
149  Mxy[x+hws][y+hws]= (FM_FLOAT)gxy;
150  // cout<<"Gxy: "<<gxy<<endl;
151 // cout<<"Pixels: ("<<xp<<","<<yp<<")"<<endl;
152  }
153  }
154  //set the filter masks
155  FilterMask F(Mxx);
156  BIASCDOUT(D_HESSGAUSS_KERNEL,"Filter xx:"<<Mxx);
157  int thebits = F.ComputeIntPrecisionBits(sizeof(InputStorageType)*8,
158  sizeof(CONV_INT)*8-1);
159  F.CreateIntFilter(thebits,thebits,thebits);
160  // F.CreateFloatFilter();
161  _ConvXX.SetKernel(F);
162  FilterMask F2(Myy);
163  BIASCDOUT(D_HESSGAUSS_KERNEL,"Filter yy:"<<Myy);
164 
165  thebits = F2.ComputeIntPrecisionBits(sizeof(InputStorageType)*8,
166  sizeof(CONV_INT)*8-1);
167  F2.CreateIntFilter(thebits,thebits,thebits);
168  // F2.CreateFloatFilter();
169  _ConvYY.SetKernel(F2);
170 
171  FilterMask F3(Mxy);
172  BIASCDOUT(D_HESSGAUSS_KERNEL,"Filter xy:"<<Mxy);
173  thebits = F3.ComputeIntPrecisionBits(sizeof(InputStorageType)*8,
174  sizeof(CONV_INT)*8-1);
175  F3.CreateIntFilter(thebits,thebits,thebits);
176  // F3.CreateFloatFilter();
177  _ConvXY.SetKernel(F3);
178 
179  _LastSigma = Sigma;
180  _LastRatio = Ratio;
181 }
182 
183 template <class InputStorageType, class OutputStorageType>
185 GetBordersValid_(int &border_x, int &border_y) const
186 {
187  _ConvXX.GetBorders(border_x, border_y);
188 }
189 
190 //////////////////////////////////////////////////////////////////////////
191 // instantiation
192 //////////////////////////////////////////////////////////////////////////
193 
194 namespace BIAS{
195 #define FILTER_INSTANTIATION_CLASS HessianGauss
196 #define FILTER_INSTANTIATION_NO_UNSIGNED_OUTPUT
197 #include "Filterinst.hh"
198 }
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
void CreateIntFilter(int rshift, int rshifth, int rshiftv)
create the int filter from the float filter
Definition: FilterMask.cpp:113
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
unsigned int GetWidth() const
Definition: ImageBase.hh:312
double _LastSigma
the parameters at the time of the last call
Definition: HessianGauss.hh:81
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
virtual ~HessianGauss()
double _HessGaussSigma
the parameters
Definition: HessianGauss.hh:79
int ComputeIntPrecisionBits(int NumberOfBitsInInputData, int NumberOfBitsInTempData)
compute the number of shiftable bits for int-from-float mask
Definition: FilterMask.cpp:211
bool SamePixelAndChannelCount(const ImageBase &Image) const
checks if data area has same &quot;size&quot; as Image of other type
Definition: ImageBase.hh:73
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &hx, Image< OutputStorageType > &hy, Image< OutputStorageType > &hxy)
returns images containig hxx hyy and hxy
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
void _CalculateKernels(double Sigma, double Ratio)
Fills _HVec with gaussian function and _VVec with derivative of gaussian function with standard devia...
A filter mask (or a kernel) used for convolution.
Definition: FilterMask.hh:61
gradient calculation with separated gauss masks
Definition: HessianGauss.hh:42
virtual void GetBordersValid_(int &border_x, int &border_y) const