Article 9 — Appendix A.1
Gaussian filter, or Gaussian blur source code
Download Gaussian filter, or Gaussian blur C++ source code (zip, 4 Kb)
gaussianblur.h
#ifndef _GAUSSIANBLUR_H_
#define _GAUSSIANBLUR_H_
template <class T = double> class TGaussianBlur
{
public:
bool Filter(T *pSignal, T *pResult, unsigned int N, unsigned int W) const;
bool Filter(T *pImage, T *pResult,
unsigned int N, unsigned int M, unsigned int W) const;
protected:
struct CSize
{
unsigned int x;
unsigned int y;
CSize(): x(0), y(0) {}
CSize(unsigned int _x, unsigned int _y): x(_x), y(_y) {}
void Set(unsigned int _x, unsigned int _y) { x = _x; y = _y; }
unsigned int Area() const { return x * y; }
};
struct CArray
{
CSize Size;
T *Buffer;
CArray(): Buffer(NULL) {}
CArray(T *_Buffer, const CSize &_Size): Buffer(_Buffer), Size(_Size) {}
CArray(T *_Buffer, unsigned int _N): Buffer(_Buffer), Size(_N, 1) {}
};
struct CWindow
{
T *Weights;
unsigned int Size;
CWindow(): Weights(NULL), Size(0), Correction(.5 - double(T(.5))) {}
~CWindow() { if (Weights) delete[] Weights; }
bool Create(unsigned int _Size);
T Apply(const T *_Element) const
{
double Sum = 0.;
const double *WeightIter = Weights;
const T *ElIter = _Element;
const double *const End = Weights + Size;
while (WeightIter < End)
Sum += *(WeightIter++) * double(*(ElIter++));
return T(Sum + Correction);
}
protected:
const double Correction;
};
bool Consistent(const T *_Image, const CSize &_Size, unsigned int _W) const
{
return _Image && _Size.x && _Size.y && _W &&
_Size.x > (_W >> 1) && _Size.y > (_W >> 1) && _W & 1;
}
bool Consistent(const T *_Signal, unsigned int _N, unsigned int _W) const
{
return _Signal && _N && _W && _N > (_W >> 1) && _W & 1;
}
};
#include "gaussianblur.cpp"
#endif
|