contrib/mul/vil3d/algo/vil3d_gauss_reduce.txx
Go to the documentation of this file.
00001 // This is mul/vil3d/algo/vil3d_gauss_reduce.txx
00002 #ifndef vil3d_gauss_reduce_txx_
00003 #define vil3d_gauss_reduce_txx_
00004 //:
00005 // \file
00006 // \brief Functions to smooth and sub-sample 3D images in one direction
00007 //
00008 //  These are not templated because
00009 //  - Each type tends to need a slightly different implementation
00010 //  - Let's not have too many templates.
00011 // \author Tim Cootes
00012 
00013 #include "vil3d_gauss_reduce.h"
00014 //
00015 #include <vil/algo/vil_gauss_reduce.h>
00016 
00017 //: Smooth and subsample single plane src_im in i to produce dest_im
00018 //  Applies 1-5-8-5-1 filter in i, then samples
00019 //  every other pixel.  Fills [0,(ni+1)/2-1][0,nj-1][0,nk-1] elements of dest
00020 //  Assumes dest_im has sufficient data allocated.
00021 //
00022 //  By applying three times we can obtain a full gaussian smoothed and
00023 //  sub-sampled 3D image
00024 template<class T>
00025 void vil3d_gauss_reduce_i(const T* src_im,
00026                           unsigned src_ni, unsigned src_nj, unsigned src_nk,
00027                           vcl_ptrdiff_t s_i_step, vcl_ptrdiff_t s_j_step,
00028                           vcl_ptrdiff_t s_k_step,
00029                           T* dest_im,
00030                           vcl_ptrdiff_t d_i_step, vcl_ptrdiff_t d_j_step,
00031                           vcl_ptrdiff_t d_k_step)
00032 {
00033   for (unsigned k=0;k<src_nk;++k)
00034   {
00035     vil_gauss_reduce_1plane(src_im, src_ni,src_nj, s_i_step,s_j_step,
00036                             dest_im,d_i_step, d_j_step);
00037     dest_im += d_k_step;
00038     src_im  += s_k_step;
00039   }
00040 }
00041 
00042 
00043 //: Smooth and subsample src_im to produce dest_im
00044 //  Applies filter in i,j and k directions, then samples every other pixel.
00045 //  Resulting image is (ni+1)/2 x (nj+1)/2 x (nk+1)/2
00046 template<class T>
00047 void vil3d_gauss_reduce(const vil3d_image_view<T>& src_im,
00048                               vil3d_image_view<T>& dest_im,
00049                               vil3d_image_view<T>& work_im1,
00050                               vil3d_image_view<T>& work_im2)
00051 {
00052   unsigned ni = src_im.ni();
00053   unsigned nj = src_im.nj();
00054   unsigned nk = src_im.nk();
00055   unsigned n_planes = src_im.nplanes();
00056 
00057   // Output image size
00058   unsigned ni2 = (ni+1)/2;
00059   unsigned nj2 = (nj+1)/2;
00060   unsigned nk2 = (nk+1)/2;
00061 
00062   if (work_im1.ni()<ni2 || work_im1.nj()<nj || work_im1.nk()<nk)
00063     work_im1.set_size(ni2, nj, nk, n_planes);
00064 
00065   if (work_im2.ni()<ni2 || work_im2.nj()<nj2 || work_im2.nk()<nk)
00066     work_im2.set_size(ni2, nj2, nk, n_planes);
00067 
00068 
00069   // Smooth and subsample in i, result in work_im1
00070   for (unsigned p=0;p<n_planes;++p)
00071     vil3d_gauss_reduce_i(
00072       src_im.origin_ptr()+p*src_im.planestep(),ni,nj,nk,
00073       src_im.istep(),src_im.jstep(),src_im.kstep(),
00074       work_im1.origin_ptr(),work_im1.istep(),work_im1.jstep(),work_im1.kstep());
00075 
00076   // Smooth and subsample in j (by implicitly transposing), result in work_im2
00077   for (unsigned p=0;p<n_planes;++p)
00078     vil3d_gauss_reduce_i(
00079       work_im1.origin_ptr(),nj,ni2,nk,
00080       work_im1.jstep(),work_im1.istep(),work_im1.kstep(),
00081       work_im2.origin_ptr(),work_im2.jstep(),work_im2.istep(),work_im2.kstep());
00082 
00083   // Can resize output now, in case it is the same as the input.
00084   dest_im.set_size(ni2, nj2, nk2, n_planes);
00085 
00086   // Smooth and subsample in k (by implicitly transposing)
00087   for (unsigned p=0;p<n_planes;++p)
00088     vil3d_gauss_reduce_i(
00089       work_im2.origin_ptr(),nk,ni2,nj2,
00090       work_im2.kstep(),work_im2.istep(),work_im2.jstep(),
00091       dest_im.origin_ptr()+p*dest_im.planestep(),
00092       dest_im.kstep(),dest_im.istep(),dest_im.jstep());
00093 }
00094 
00095 
00096 //: Smooth and subsample src_im along i and j to produce dest_im
00097 //  Applies filter in i,j directions, then samples every other pixel.
00098 //  Resulting image is (ni+1)/2 x (nj+1)/2 x nk
00099 template<class T>
00100 void vil3d_gauss_reduce_ij(const vil3d_image_view<T>& src_im,
00101                                  vil3d_image_view<T>& dest_im,
00102                                  vil3d_image_view<T>& work_im1)
00103 {
00104   unsigned ni = src_im.ni();
00105   unsigned nj = src_im.nj();
00106   unsigned nk = src_im.nk();
00107   unsigned n_planes = src_im.nplanes();
00108 
00109   // Output image size
00110   unsigned ni2 = (ni+1)/2;
00111   unsigned nj2 = (nj+1)/2;
00112   unsigned nk2 = nk;
00113 
00114   if (work_im1.ni()<ni2 || work_im1.nj()<nj || work_im1.nk()<nk)
00115     work_im1.set_size(ni2,nj,nk);
00116   dest_im.set_size(ni2,nj2,nk2,n_planes);
00117 
00118   for (unsigned p=0;p<n_planes;++p)
00119   {
00120     // Smooth and subsample in i, result in work_im1
00121     vil3d_gauss_reduce_i(
00122       src_im.origin_ptr()+p*src_im.planestep(),ni,nj,nk,
00123       src_im.istep(),src_im.jstep(),src_im.kstep(),
00124       work_im1.origin_ptr(),work_im1.istep(),work_im1.jstep(),work_im1.kstep());
00125 
00126     // Smooth and subsample in j (by implicitly transposing), result in dest_im
00127     vil3d_gauss_reduce_i(
00128       work_im1.origin_ptr(),nj,ni2,nk,
00129       work_im1.jstep(),work_im1.istep(),work_im1.kstep(),
00130       dest_im.origin_ptr()+p*dest_im.planestep(),
00131       dest_im.jstep(),dest_im.istep(),dest_im.kstep());
00132   }
00133 }
00134 
00135 
00136 //: Smooth and subsample src_im along i and k to produce dest_im
00137 //  Applies filter in i,k directions, then samples every other pixel.
00138 //  Resulting image is (ni+1)/2 x nj x (nk+1)/2
00139 template<class T>
00140 void vil3d_gauss_reduce_ik(const vil3d_image_view<T>& src_im,
00141                                  vil3d_image_view<T>& dest_im,
00142                                  vil3d_image_view<T>& work_im1)
00143 {
00144   unsigned ni = src_im.ni();
00145   unsigned nj = src_im.nj();
00146   unsigned nk = src_im.nk();
00147   unsigned n_planes = src_im.nplanes();
00148 
00149   // Output image size
00150   unsigned ni2 = (ni+1)/2;
00151   unsigned nj2 = nj;
00152   unsigned nk2 = (nk+1)/2;
00153 
00154   if (work_im1.ni()<ni2 || work_im1.nj()<nj || work_im1.nk()<nk)
00155     work_im1.set_size(ni2,nj,nk);
00156   dest_im.set_size(ni2,nj2,nk2,n_planes);
00157 
00158   for (unsigned p=0;p<n_planes;++p)
00159   {
00160     // Smooth and subsample in i, result in work_im1
00161     vil3d_gauss_reduce_i(
00162       src_im.origin_ptr()+p*src_im.planestep(),ni,nj,nk,
00163       src_im.istep(),src_im.jstep(),src_im.kstep(),
00164       work_im1.origin_ptr(),work_im1.istep(),work_im1.jstep(),work_im1.kstep());
00165 
00166     // Smooth and subsample in k (by implicitly transposing), result in dest_im
00167     vil3d_gauss_reduce_i(
00168         work_im1.origin_ptr(),nk,ni2,nj,
00169         work_im1.kstep(),work_im1.istep(),work_im1.jstep(),
00170         dest_im.origin_ptr()+p*dest_im.planestep(),
00171         dest_im.kstep(),dest_im.istep(),dest_im.jstep());
00172   }
00173 }
00174 
00175 
00176 //: Smooth and subsample src_im along j and k to produce dest_im
00177 //  Applies filter in j,k directions, then samples every other pixel.
00178 //  Resulting image is ni x (nj+1)/2 x (nk+1)/2
00179 template<class T>
00180 void vil3d_gauss_reduce_jk(const vil3d_image_view<T>& src_im,
00181                                  vil3d_image_view<T>& dest_im,
00182                                  vil3d_image_view<T>& work_im1)
00183 {
00184   unsigned ni = src_im.ni();
00185   unsigned nj = src_im.nj();
00186   unsigned nk = src_im.nk();
00187   unsigned n_planes = src_im.nplanes();
00188 
00189   // Output image size
00190   unsigned ni2 = ni;
00191   unsigned nj2 = (nj+1)/2;
00192   unsigned nk2 = (nk+1)/2;
00193 
00194   if (work_im1.ni()<ni || work_im1.nj()<nj2 || work_im1.nk()<nk)
00195     work_im1.set_size(ni,nj2,nk);
00196 
00197   dest_im.set_size(ni2,nj2,nk2,n_planes);
00198 
00199   for (unsigned p=0;p<n_planes;++p)
00200   {
00201     // Smooth and subsample in j (by implicitly transposing), result in work_im1
00202     vil3d_gauss_reduce_i(
00203       src_im.origin_ptr()+p*src_im.planestep(),nj,ni,nk,
00204       src_im.jstep(),src_im.istep(),src_im.kstep(),
00205       work_im1.origin_ptr(),work_im1.jstep(),work_im1.istep(),work_im1.kstep());
00206 
00207     // Smooth and subsample in k (by implicitly transposing), result in dest_im
00208     vil3d_gauss_reduce_i(
00209       work_im1.origin_ptr(),nk,ni,nj2,
00210       work_im1.kstep(),work_im1.istep(),work_im1.jstep(),
00211       dest_im.origin_ptr()+p*dest_im.planestep(),
00212       dest_im.kstep(),dest_im.istep(),dest_im.jstep());
00213   }
00214 }
00215 
00216 
00217 #undef VIL3D_GAUSS_REDUCE_INSTANTIATE
00218 #define VIL3D_GAUSS_REDUCE_INSTANTIATE(T) \
00219 template void vil3d_gauss_reduce_i(const T* src_im,   \
00220                                    unsigned src_ni, unsigned src_nj, unsigned src_nk, \
00221                                    vcl_ptrdiff_t s_i_step, vcl_ptrdiff_t s_j_step,  \
00222                                    vcl_ptrdiff_t s_k_step,  \
00223                                    T* dest_im,  \
00224                                    vcl_ptrdiff_t d_i_step,  \
00225                                    vcl_ptrdiff_t d_j_step, vcl_ptrdiff_t d_k_step); \
00226 template void vil3d_gauss_reduce(const vil3d_image_view<T >& src_im, \
00227                                  vil3d_image_view<T >& dest_im,  \
00228                                  vil3d_image_view<T >& work_im1, \
00229                                  vil3d_image_view<T >& work_im2);  \
00230 template void vil3d_gauss_reduce_ij(const vil3d_image_view<T >& src_im,  \
00231                                     vil3d_image_view<T >& dest_im, \
00232                                     vil3d_image_view<T >& work_im1); \
00233 template void vil3d_gauss_reduce_ik(const vil3d_image_view<T >& src_im,  \
00234                                     vil3d_image_view<T >& dest_im, \
00235                                     vil3d_image_view<T >& work_im1); \
00236 template void vil3d_gauss_reduce_jk(const vil3d_image_view<T >& src_im,  \
00237                                     vil3d_image_view<T >& dest_im, \
00238                                     vil3d_image_view<T >& work_im1)
00239 
00240 #endif // vil3d_gauss_reduce_txx_