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     
00047     template<class KERNEL_TYPE>
00048     Kernel<KERNEL_TYPE>
00049     getGaussianKernel(double rowSigma, double columnSigma);
00050 
00051 
00080     template<class KERNEL_TYPE>
00081     Kernel<KERNEL_TYPE>
00082     getGaussianKernel(size_t rows, size_t columns,
00083                       double rowSigma=-1.0, double columnSigma=-1.0);
00084 
00085 
00086   } // namespace computerVision
00087 
00088 } // namespace dlr
00089 
00090 
00091 /* =============== Implementation follows =============== */
00092 
00093 #include <dlrNumeric/utilities.h>
00094 
00095 namespace dlr {
00096 
00097   namespace computerVision {
00098 
00100     namespace privateCode {
00101       
00102       template <class TYPE>
00103       Array1D<TYPE>
00104       getGaussian1D(size_t size, double sigma, bool normalize=false)
00105       {
00106         const double myPi = 3.14159265359;
00107         Array1D<TYPE> result(size);
00108         double x = (1.0 - static_cast<double>(size))/2.0;
00109         double twoSigmaSq = 2.0 * sigma * sigma;
00110         double k = 1.0 / (std::sqrt(2.0 * myPi) * sigma);
00111         for(size_t index0 = 0; index0 < size; ++index0) {
00112           result[index0] = k * exp(-x * x / twoSigmaSq);
00113           x += 1.0;
00114         }
00115         if(normalize) {
00116           result /= sum(result);
00117         }
00118         return result;
00119       }
00120       
00121     } // namespace privateCode
00123 
00124 
00125     // This function generates and returns a separable Gaussian kernel.
00126     template<class KERNEL_TYPE>
00127     Kernel<KERNEL_TYPE>
00128     getGaussianKernel(double rowSigma, double columnSigma)
00129     {
00130       size_t rows = static_cast<size_t>(6.0 * rowSigma + 1.0);
00131       size_t columns = static_cast<size_t>(6.0 * columnSigma + 1.0);
00132       if(rows % 2 == 0) {
00133         ++rows;
00134       }
00135       if(columns % 2 == 0) {
00136         ++columns;
00137       }
00138       return getGaussianKernel<KERNEL_TYPE>(
00139         rows, columns, rowSigma, columnSigma);
00140     }
00141 
00142     
00143     // This function generates and returns a separable Gaussian kernel.
00144     template<class KERNEL_TYPE>
00145     Kernel<KERNEL_TYPE>
00146     getGaussianKernel(size_t rows, size_t columns,
00147                       double rowSigma, double columnSigma)
00148     {
00149       // Argument checking.
00150       if(rows == 0 || columns == 0) {
00151         DLR_THROW(ValueException, "getGaussianKernel()",
00152                   "Arguments rows and columns may not have value of zero.");
00153       }
00154       if(rowSigma < 0.0) {
00155         rowSigma = rows / 6.0;
00156       }
00157       if(columnSigma < 0.0) {
00158         columnSigma = columns / 6.0;
00159       }
00160 
00161       Array1D<KERNEL_TYPE> rowComponent =
00162         privateCode::getGaussian1D<KERNEL_TYPE>(columns, columnSigma, true);
00163       Array1D<KERNEL_TYPE> columnComponent =
00164         privateCode::getGaussian1D<KERNEL_TYPE>(rows, rowSigma, true);
00165       return Kernel<KERNEL_TYPE>(rowComponent, columnComponent);
00166     }
00167 
00168 
00169   } // namespace computerVision
00170   
00171 } // namespace dlr
00172 
00173 #endif /* #ifndef _DLRCOMPUTERVISION_KERNEL_H_ */

Generated on Wed Nov 25 12:15:05 2009 for dlrComputerVision Utility Library by  doxygen 1.5.8