contrib/mul/vil3d/algo/vil3d_grad_1x3.txx
Go to the documentation of this file.
00001 // This is mul/vil3d/algo/vil3d_grad_1x3.txx
00002 #ifndef vil3d_grad_1x3_txx_
00003 #define vil3d_grad_1x3_txx_
00004 //:
00005 // \file
00006 // \brief Apply grad gradient filter to an image
00007 // \author Tim Cootes
00008 
00009 #include "vil3d_grad_1x3.h"
00010 #include <vil3d/vil3d_plane.h>
00011 #include <vil3d/vil3d_slice.h>
00012 
00013 
00014 
00015 
00016 
00017 
00018 //: Compute 1 gradient of single plane of 3D data using 1x3 grad filters
00019 template <class srcT, class gradT, class accumT>
00020 void vil3d_grad_1x3_1dir(const srcT *src,
00021                          gradT * grad, vcl_ptrdiff_t delta_step,
00022                          vcl_ptrdiff_t isrc, vcl_ptrdiff_t jsrc, vcl_ptrdiff_t ksrc,
00023                          vcl_ptrdiff_t igrad, vcl_ptrdiff_t jgrad, vcl_ptrdiff_t kgrad,
00024                          unsigned ni, unsigned nj, unsigned nk, accumT /*dummy*/ )
00025 {
00026   for (unsigned k=0;k<nk;++k)    
00027     for (unsigned j=0;j<nj;++j)
00028     {      
00029       const srcT* s = src + ksrc*k + jsrc*j;
00030       gradT* g = grad + kgrad*k + jgrad*j;
00031 
00032       for (unsigned i=0; i<ni; ++i)
00033       {
00034         // Compute gradient
00035         // Note: Multiply each element individually
00036         //      to ensure conversion to float before subtraction
00037         *g = static_cast<gradT>(
00038              (static_cast<accumT>(s[delta_step]) - static_cast<accumT>(s[-delta_step]))
00039               / static_cast<accumT>(2));
00040 
00041         s += isrc;
00042         g += igrad;
00043       }
00044     }
00045 }
00046 
00047 
00048 
00049 
00050 //: Compute gradients of an image using (-0.5 0 0.5) grad filters
00051 //  Computes both i,j and k gradients of an ni x nj x nk plane of data
00052 //  1 pixel border around grad images is set to zero
00053 template<class srcT, class destT>
00054 void vil3d_grad_1x3(const vil3d_image_view<srcT>& src,
00055                     vil3d_image_view<destT>& grad_ijk)
00056 {
00057   unsigned np = src.nplanes();
00058   unsigned ni = src.ni();
00059   unsigned nj = src.nj();
00060   unsigned nk = src.nk();
00061   grad_ijk.set_size(ni,nj,nk,3*np);
00062   for (unsigned p=0;p<np;++p)
00063   {
00064     vil3d_image_view<destT> grad_i_plane = vil3d_plane(grad_ijk,3*p);
00065     vil3d_image_view<destT> grad_j_plane = vil3d_plane(grad_ijk,3*p+1);
00066     vil3d_image_view<destT> grad_k_plane = vil3d_plane(grad_ijk,3*p+2);
00067     vil3d_grad_1x3_1dir(&src(1,0,0,p), &grad_i_plane(1,0,0), src.istep(),
00068                         src.istep(), src.jstep(), src.kstep(),
00069                         grad_i_plane.istep(), grad_i_plane.jstep(), grad_i_plane.kstep(),
00070                         ni-2, nj, nk, srcT());
00071     vil3d_grad_1x3_1dir(&src(0,1,0,p), &grad_j_plane(0,1,0), src.jstep(),
00072                         src.istep(), src.jstep(), src.kstep(),
00073                         grad_j_plane.istep(), grad_j_plane.jstep(), grad_j_plane.kstep(),
00074                         ni, nj-2, nk, srcT());
00075     vil3d_grad_1x3_1dir(&src(0,0,1,p), &grad_k_plane(0,0,1), src.kstep(),
00076                         src.istep(), src.jstep(), src.kstep(),
00077                         grad_k_plane.istep(), grad_k_plane.jstep(), grad_k_plane.kstep(),
00078                         ni, nj, nk-2, srcT());
00079     
00080     vil3d_slice_jk(grad_i_plane,0).fill(0.0f);
00081     vil3d_slice_jk(grad_i_plane,ni-1).fill(0.0f);
00082     vil3d_slice_ik(grad_j_plane,0).fill(0.0f);
00083     vil3d_slice_ik(grad_j_plane,nj-1).fill(0.0f);
00084     vil3d_slice_ij(grad_k_plane,0).fill(0.0f);
00085     vil3d_slice_ij(grad_k_plane,nk-1).fill(0.0f);
00086   }
00087 }
00088 
00089 //: Apply grad 1x3 gradient filter to 3D image
00090 template<class srcT, class destT>
00091 void vil3d_grad_1x3(const vil3d_image_view<srcT>& src,
00092                     vil3d_image_view<destT>& grad_i,
00093                     vil3d_image_view<destT>& grad_j,
00094                     vil3d_image_view<destT>& grad_k)
00095 {
00096   unsigned ni = src.ni();
00097   unsigned nj = src.nj();
00098   unsigned nk = src.nk();
00099   unsigned np = src.nplanes();
00100   grad_i.set_size(ni,nj,nk,np);
00101   grad_j.set_size(ni,nj,nk,np);
00102   grad_k.set_size(ni,nj,nk,np);
00103   for (unsigned p=0;p<np;++p)
00104   {
00105     vil3d_grad_1x3_1dir(&src(1,0,0,p), &grad_i(1,0,0,p), src.istep(),
00106                         src.istep(), src.jstep(), src.kstep(),
00107                         grad_i.istep(), grad_i.jstep(), grad_i.kstep(),
00108                         ni-2, nj, nk, srcT());
00109     vil3d_grad_1x3_1dir(&src(0,1,0,p), &grad_j(0,1,0,p), src.jstep(),
00110                         src.istep(), src.jstep(), src.kstep(),
00111                         grad_j.istep(), grad_j.jstep(), grad_j.kstep(),
00112                         ni, nj-2, nk, srcT());
00113     vil3d_grad_1x3_1dir(&src(0,0,1,p), &grad_k(0,0,1,p), src.kstep(),
00114                         src.istep(), src.jstep(), src.kstep(),
00115                         grad_k.istep(), grad_k.jstep(), grad_k.kstep(),
00116                         ni, nj, nk-2, srcT());
00117   }
00118   vil3d_slice_jk(grad_i,0).fill(0.0f);
00119   vil3d_slice_jk(grad_i,ni-1).fill(0.0f);
00120   vil3d_slice_ik(grad_j,0).fill(0.0f);
00121   vil3d_slice_ik(grad_j,nj-1).fill(0.0f);
00122   vil3d_slice_ij(grad_k,0).fill(0.0f);
00123   vil3d_slice_ij(grad_k,nk-1).fill(0.0f);
00124 
00125 }
00126 
00127 //: Compute square gradient magnitude of 3D image
00128 //  Use (-0.5,0,+0.5) filters in i,j,k
00129 template<class srcT, class destT>
00130 void vil3d_grad_1x3_mag_sq(const vil3d_image_view<srcT>& src,
00131                            vil3d_image_view<destT>& grad_mag2)
00132 {
00133   unsigned np = src.nplanes();
00134   unsigned ni = src.ni();
00135   unsigned nj = src.nj();
00136   unsigned nk = src.nk();
00137   grad_mag2.set_size(ni,nj,nk,np);
00138   for (unsigned p=0;p<np;++p)
00139   {
00140     vil3d_image_view<destT> grad_plane = vil3d_plane(grad_mag2,p);
00141     vil3d_grad_1x3_mag_sq_1plane(vil3d_plane(src,p),grad_plane);
00142   }
00143 }
00144 
00145 
00146 #undef vil3d_GRAD_1X3_INSTANTIATE
00147 #define vil3d_GRAD_1X3_INSTANTIATE(srcT, destT) \
00148 template void vil3d_grad_1x3(const vil3d_image_view< srcT >& src, \
00149                                    vil3d_image_view<destT >& grad_ijk); \
00150 template void vil3d_grad_1x3(const vil3d_image_view< srcT >& src, \
00151                                    vil3d_image_view<destT >& grad_i, \
00152                                    vil3d_image_view<destT >& grad_j, \
00153                                    vil3d_image_view<destT >& grad_k); \
00154 template void vil3d_grad_1x3_mag_sq(const vil3d_image_view<srcT >& src, \
00155                                    vil3d_image_view<destT >& grad_mag2)
00156 
00157 #endif // vil3d_grad_1x3_txx_