kernels.h

Go to the documentation of this file.
00001 
00015 #ifndef _DLRCOMPUTERVISION_KERNELS_H_
00016 #define _DLRCOMPUTERVISION_KERNELS_H_
00017 
00018 #include <dlrComputerVision/kernel.h>
00019 
00020 namespace dlr {
00021 
00022   namespace computerVision {
00023     
00046     template<class KERNEL_TYPE>
00047     Kernel<KERNEL_TYPE>
00048     getGaussianKernel(size_t rows, size_t columns,
00049                       double rowSigma=-1.0, double columnSigma=-1.0);
00050 
00051 
00052   } // namespace computerVision
00053 
00054 } // namespace dlr
00055 
00056 
00057 /* =============== Implementation follows =============== */
00058 
00059 #include <dlrNumeric/utilities.h>
00060 
00061 namespace dlr {
00062 
00063   namespace computerVision {
00064 
00066     namespace privateCode {
00067       
00068       template <class TYPE>
00069       Array1D<TYPE>
00070       getGaussian1D(size_t size, double sigma, bool normalize=false)
00071       {
00072         const double myPi = 3.14159265359;
00073         Array1D<TYPE> result(size);
00074         double x = (1.0 - static_cast<double>(size))/2.0;
00075         double twoSigmaSq = 2.0 * sigma * sigma;
00076         double k = 1.0 / (std::sqrt(2.0 * myPi) * sigma);
00077         for(size_t index0 = 0; index0 < size; ++index0) {
00078           result[index0] = k * exp(-x * x / twoSigmaSq);
00079           x += 1.0;
00080         }
00081         if(normalize) {
00082           result /= sum(result);
00083         }
00084         return result;
00085       }
00086       
00087     } // namespace privateCode
00089 
00090 
00091     // This function generates and returns a separable Gaussian kernel
00092     // with the same variance along each axis.
00093     template<class KERNEL_TYPE>
00094     Kernel<KERNEL_TYPE>
00095     getGaussianKernel(size_t rows, size_t columns,
00096                       double rowSigma, double columnSigma)
00097     {
00098       // Argument checking.
00099       if(rows == 0 || columns == 0) {
00100         DLR_THROW(ValueException, "getGaussianKernel()",
00101                   "Arguments rows and columns may not have value of zero.");
00102       }
00103       if(rowSigma < 0.0) {
00104         rowSigma = rows / 6.0;
00105       }
00106       if(columnSigma < 0.0) {
00107         columnSigma = columns / 6.0;
00108       }
00109 
00110       Array1D<KERNEL_TYPE> rowComponent =
00111         privateCode::getGaussian1D<KERNEL_TYPE>(columns, columnSigma, true);
00112       Array1D<KERNEL_TYPE> columnComponent =
00113         privateCode::getGaussian1D<KERNEL_TYPE>(rows, rowSigma, true);
00114       return Kernel<KERNEL_TYPE>(rowComponent, columnComponent);
00115     }
00116 
00117 
00118   } // namespace computerVision
00119   
00120 } // namespace dlr
00121 
00122 #endif /* #ifndef _DLRCOMPUTERVISION_KERNEL_H_ */

Generated on Mon Jul 9 20:34:03 2007 for dlrLibs Utility Libraries by  doxygen 1.5.2