contrib/oxl/osl/osl_kernel.cxx
Go to the documentation of this file.
00001 // This is oxl/osl/osl_kernel.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "osl_kernel.h"
00010 #include <vcl_cmath.h>
00011 #include <vnl/vnl_math.h>
00012 
00013 // Construct one half of a Gaussian convolution kernel.
00014 //
00015 //   kernel_[i] = exp( (i-width_)^2/sigma^2 )/det
00016 void osl_kernel_DOG(float sigma_, float *kernel_, int k_size_, int width_) {
00017   float s2 = 2.0f*sigma_*sigma_;
00018   float det = sigma_*(float)vcl_sqrt(2.0 * vnl_math::pi);
00019 
00020   for (int i=0,x=-width_; i<k_size_; ++i,++x)
00021     kernel_[i] = (float)vcl_exp(-x*x/s2)/det;
00022 }
00023 
00024 // Construct one half of a Gaussian convolution kernel.
00025 // With fancy stuff.
00026 void osl_kernel_DOG(float *kernel_, float *sub_area_, int &k_size_,
00027                     float sigma_, float gauss_tail_,
00028                     int max_width_, int &width_)
00029 {
00030   const float s2 = 2.0f*sigma_*sigma_;
00031 
00032   for (int i=0; i<max_width_; ++i)  {
00033     width_ = i;                             // half Kernel width
00034 
00035     // the value of kernel_[i] is the average of the gaussian over
00036     // 11 points evenly spaced on the interval [i-0.5, i+0.5].
00037     kernel_[i] = 0.0;
00038     for (float x=i-0.5f; x<=i+0.5f; x+=0.1f)
00039       kernel_[i] += (float)vcl_exp(-x*x/s2);
00040     kernel_[i] /= 11.0f;
00041 
00042     if (i>0 && kernel_[i] < gauss_tail_)
00043       break;
00044   }
00045 
00046   // compute area under half-kernel.
00047   float area = 0.0f;
00048   for (int i=0; i<width_; ++i)
00049     area += kernel_[i];
00050 
00051   // Total area under whole profile curve.
00052   float total_area = 2.0f*area - kernel_[0];
00053 
00054   for (int i=0; i<width_; ++i)  {
00055     sub_area_[i] = (total_area - area)/total_area;
00056     area -= kernel_[i];
00057     kernel_[i] /= total_area;
00058   }
00059 
00060   // kernel size
00061   k_size_ = 2*width_ - 1;
00062 }
00063