core/vil/algo/vil_greyscale_dilate.txx
Go to the documentation of this file.
00001 // This is core/vil/algo/vil_greyscale_dilate.txx
00002 #ifndef vil_greyscale_dilate_txx_
00003 #define vil_greyscale_dilate_txx_
00004 //:
00005 // \file
00006 // \brief Perform greyscale dilation on images
00007 // \author Tim Cootes
00008 
00009 #include "vil_greyscale_dilate.h"
00010 #include <vcl_cassert.h>
00011 
00012 //: Dilates src_image to produce dest_image (assumed single plane).
00013 // dest_image(i0,j0) is the maximum value of the pixels under the
00014 // structuring element when it is centred on src_image(i0,j0)
00015 template <class T>
00016 void vil_greyscale_dilate(const vil_image_view<T>& src_image,
00017                           vil_image_view<T>& dest_image,
00018                           const vil_structuring_element& element)
00019 {
00020   assert(src_image.nplanes()==1);
00021   unsigned ni = src_image.ni();
00022   unsigned nj = src_image.nj();
00023   dest_image.set_size(ni,nj,1);
00024 
00025   vcl_ptrdiff_t s_istep = src_image.istep(),  s_jstep = src_image.jstep(),
00026                 d_istep = dest_image.istep(), d_jstep = dest_image.jstep();
00027 
00028   const T* src_row0 = src_image.top_left_ptr();
00029   T* dest_row0 = dest_image.top_left_ptr();
00030 
00031   vcl_vector<vcl_ptrdiff_t> offset;
00032   vil_compute_offsets(offset,element,s_istep,s_jstep);
00033 
00034   // Define box in which all element will be valid
00035   int ilo = -element.min_i();
00036   int ihi = ni-1-element.max_i();
00037   int jlo = -element.min_j();
00038   int jhi = nj-1-element.max_j();
00039 
00040   // Deal with left edge
00041   for (int i=0;i<ilo;++i)
00042     for (unsigned int j=0;j<nj;++j)
00043       dest_image(i,j,0)=vil_greyscale_dilate(src_image,0,element,i,j);
00044   // Deal with right edge
00045   for (unsigned int i=ihi+1;i<ni;++i)
00046     for (unsigned int j=0;j<nj;++j)
00047       dest_image(i,j,0)=vil_greyscale_dilate(src_image,0,element,i,j);
00048   // Deal with bottom edge
00049   for (int i=ilo;i<=ihi;++i)
00050     for (int j=0;j<jlo;++j)
00051       dest_image(i,j,0)=vil_greyscale_dilate(src_image,0,element,i,j);
00052   // Deal with top edge
00053   for (int i=ilo;i<=ihi;++i)
00054     for (unsigned int j=jhi+1;j<nj;++j)
00055       dest_image(i,j,0)=vil_greyscale_dilate(src_image,0,element,i,j);
00056 
00057   for (int j=jlo;j<=jhi;++j)
00058   {
00059     const T* src_p = src_row0 + j*s_jstep + ilo*s_istep;
00060     T* dest_p = dest_row0 + j*d_jstep + ilo * d_istep;
00061 
00062     for (int i=ilo;i<=ihi;++i,src_p+=s_istep,dest_p+=d_istep)
00063       *dest_p=vil_greyscale_dilate(src_p,&offset[0],offset.size());
00064   }
00065 }
00066 
00067 #undef VIL_GREYSCALE_DILATE_INSTANTIATE
00068 #define VIL_GREYSCALE_DILATE_INSTANTIATE(T) \
00069 template void vil_greyscale_dilate(const vil_image_view< T >& src_image, \
00070                                    vil_image_view< T >& dest_image, \
00071                                    const vil_structuring_element& element)
00072 
00073 #endif // vil_greyscale_dilate_txx_