core/vil/algo/vil_exp_grad_filter_1d.h
Go to the documentation of this file.
00001 #ifndef vil_exp_grad_filter_1d_h_
00002 #define vil_exp_grad_filter_1d_h_
00003 //:
00004 // \file
00005 // \brief Apply exponential gradient filter
00006 // \author Tim Cootes
00007 
00008 #include <vil/vil_image_view.h>
00009 
00010 //: Apply exponential gradient filter to 1D data. Form: sign(i)*exp(c*|i|)
00011 //  Apply filter to n values src[i*sstep] to produce output dest[i*dstep]
00012 //  Exponential gradient filter of the form
00013 //  sign(i)*exp(c*|i|) applied. c=log(k)
00014 //  Uses fast recursive implementation.
00015 template <class srcT, class destT, class accumT>
00016 inline void vil_exp_grad_filter_1d(const srcT* src, vcl_ptrdiff_t sstep,
00017                                    destT* dest, vcl_ptrdiff_t dstep,
00018                                    int n, accumT k)
00019 {
00020   const srcT* s = src;
00021   const srcT* src_end = src + (n-1)*sstep;
00022 
00023   // Zero first element
00024   dest[0]=0; dest+=dstep;
00025 
00026   // Forward pass to compute -ive part of filter response
00027   // Initialise for first element
00028   accumT rt= -1*(accumT) *s; s+=sstep;
00029   accumT k_sum=(accumT) 1;
00030 
00031   while (s!=src_end)
00032   {
00033     *dest = (destT)(rt/k_sum); // Set value for -ive half of filter
00034     rt *= k; k_sum *= k;     // Scale sums
00035     rt -= *s; k_sum += 1.0f; // Increment with next element
00036     s+=sstep; dest+=dstep;   // Move to next element
00037   }
00038 
00039   // Backward pass to compute +ive part of filter response
00040   dest[0]=0; dest-=dstep;
00041   rt= (accumT) *s; s-=sstep;
00042   k_sum=(accumT) 1;
00043   src_end = src;
00044   while (s!=src_end)
00045   {
00046     *dest += (destT)(rt/k_sum); // Add in value for +ive half of filter
00047     rt *= k; k_sum *= k;     // Scale sums
00048     rt += *s; k_sum += 1.0f; // Increment with next element
00049     s-=sstep; dest-=dstep;   // Move to next element
00050   }
00051 }
00052 
00053 //: Apply exponential gradient filter to src_im (along i direction).
00054 //  Exponential gradient filter of the form sign(i)*exp(c*|i|) applied. c=log(k)
00055 //  Uses fast recursive implementation.
00056 // \relatesalso vil_image_view
00057 template <class srcT, class destT, class accumT>
00058 inline void vil_exp_grad_filter_i(const vil_image_view<srcT>& src_im,
00059                                   vil_image_view<destT>& dest_im,
00060                                   accumT k)
00061 {
00062   unsigned ni = src_im.ni();
00063   unsigned nj = src_im.nj();
00064   dest_im.set_size(ni,nj,src_im.nplanes());
00065   vcl_ptrdiff_t s_istep = src_im.istep(), s_jstep = src_im.jstep();
00066   vcl_ptrdiff_t d_istep = dest_im.istep(),d_jstep = dest_im.jstep();
00067 
00068   for (unsigned p=0;p<src_im.nplanes();++p)
00069   {
00070     const srcT*  src_row  = src_im.top_left_ptr()+p*src_im.planestep();
00071     destT* dest_row = dest_im.top_left_ptr()+p*dest_im.planestep();
00072     // Filter every row
00073     for (unsigned j=0;j<nj;++j,src_row+=s_jstep,dest_row+=d_jstep)
00074       vil_exp_grad_filter_1d(src_row,s_istep, dest_row,d_istep, ni, k);
00075   }
00076 }
00077 
00078 //: Apply exponential gradient filter to src_im (along j direction).
00079 //  Exponential gradient filter of the form sign(j)*exp(c*|j|) applied. c=log(k)
00080 //  Uses fast recursive implementation.
00081 // \relatesalso vil_image_view
00082 template <class srcT, class destT, class accumT>
00083 inline void vil_exp_grad_filter_j(const vil_image_view<srcT>& src_im,
00084                                   vil_image_view<destT>& dest_im,
00085                                   accumT k)
00086 {
00087   unsigned ni = src_im.ni();
00088   unsigned nj = src_im.nj();
00089   dest_im.set_size(ni,nj,src_im.nplanes());
00090   vcl_ptrdiff_t s_istep = src_im.istep(), s_jstep = src_im.jstep();
00091   vcl_ptrdiff_t d_istep = dest_im.istep(),d_jstep = dest_im.jstep();
00092 
00093   for (unsigned p=0;p<src_im.nplanes();++p)
00094   {
00095     const srcT*  src_col  = src_im.top_left_ptr()+p*src_im.planestep();
00096     destT* dest_col = dest_im.top_left_ptr()+p*dest_im.planestep();
00097     // Filter every column
00098     for (unsigned i=0;i<ni;++i,src_col+=s_istep,dest_col+=d_istep)
00099       vil_exp_grad_filter_1d(src_col,s_jstep, dest_col,d_jstep, nj, k);
00100   }
00101 }
00102 
00103 #endif // vil_exp_grad_filter_1d_h_