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