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