1 // convkernel_gaussian.h - a class to create a gaussian convolution kernel.
2 // Copyright (C) 2008 by Alastair M. Robinson
3 
4 #ifndef CONVKERNEL_GAUSSIAN_1D
5 #define CONVKERNEL_GAUSSIAN_1D
6 
7 #include <cmath>
8 #include <iostream>
9 
10 #include "convkernel.h"
11 
12 class ConvKernel_Gaussian_1D : public ConvKernel
13 {
14 	public:
ConvKernel_Gaussian_1D(float radius)15 	ConvKernel_Gaussian_1D(float radius) : ConvKernel()
16 	{
17 		// Sanity-check the provided radius.  Any radius less than 0.5 will result in
18 		// a single cell kernel, containing just a "1".  Radius of zero leads to
19 		// a divide-by-zero later, though, so we avoid it by clamping the radius.
20 		if(radius<=0.1) radius=0.1;
21 
22 		// Calculate the standard deviation of the gaussian distribution.
23 		// This is calculated such that the extreme north, south, east and west
24 		// cells will contain the value 0.01 - or whatever's in the log() function.
25 		sd=sqrt(-(radius*radius)/(2*log(0.01)));
26 
27 		Initialize(int(radius+0.5)*2+1);
28 	}
29 
~ConvKernel_Gaussian_1D()30 	virtual ~ConvKernel_Gaussian_1D()
31 	{
32 	}
33 
Initialize(int width)34 	virtual void Initialize(int width)
35 	{
36 		// Use the superclass to allocate the kernel itself;
37 		ConvKernel::Initialize(width,1);
38 		for(int x=0;x<width;++x)
39 		{
40 			int xo=x-(width/2);
41 			Kernel(x,0)=exp(-(xo*xo)/(2*sd*sd));
42 		}
43 	}
44 	protected:
45 	float sd;
46 };
47 
48 #endif
49