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