contrib/mul/vil3d/algo/vil3d_binary_erode.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Perform binary erosion on 3D images
00004 // \author Tim Cootes
00005 
00006 #include "vil3d_binary_erode.h"
00007 #include <vcl_cassert.h>
00008 #include <vil3d/vil3d_crop.h>
00009 
00010 //: Erodes src_image to produce dest_image (assumed single plane)
00011 void vil3d_binary_erode(const vil3d_image_view<bool>& src_image,
00012                         vil3d_image_view<bool>& dest_image,
00013                         const vil3d_structuring_element& element)
00014 {
00015   assert(src_image.nplanes()==1);
00016   unsigned ni = src_image.ni(); assert(ni>0);
00017   unsigned nj = src_image.nj(); assert(nj>0);
00018   unsigned nk = src_image.nk(); assert(nk>0);
00019   dest_image.set_size(ni,nj,nk,1);
00020 
00021   vcl_ptrdiff_t s_istep = src_image.istep(),  s_jstep = src_image.jstep();
00022   vcl_ptrdiff_t s_kstep = src_image.kstep();
00023   vcl_ptrdiff_t d_istep = dest_image.istep();
00024 
00025   vcl_vector<vcl_ptrdiff_t> offset;
00026   vil3d_compute_offsets(offset,element,s_istep,s_jstep,s_kstep);
00027 
00028   // Define box in which all element will be valid
00029   int ilo = -element.min_i();
00030   int ihi = ni-1-element.max_i();
00031   int jlo = -element.min_j();
00032   int jhi = nj-1-element.max_j();
00033   int klo = -element.min_k();
00034   int khi = nk-1-element.max_k();
00035 
00036   // Zero low and high i sides
00037   if (ilo>0)    vil3d_crop(dest_image,0,ilo,0,nj,0,nk).fill(false);
00038   if (ihi<int(ni)-1) vil3d_crop(dest_image,ihi+1,ni-ihi-1,0,nj,0,nk).fill(false);
00039 
00040   // Zero low and high j sides
00041   if (jlo>0)    vil3d_crop(dest_image,0,ni,0,jlo,0,nk).fill(false);
00042   if (jhi<int(nj)-1) vil3d_crop(dest_image,0,ni,jhi+1,nj-jhi-1,0,nk).fill(false);
00043 
00044   // Zero low and high k sides
00045   if (klo>0)    vil3d_crop(dest_image,0,ni,0,nj,0,klo).fill(false);
00046   if (khi<int(nk)-1) vil3d_crop(dest_image,0,ni,0,nj,khi+1,nk-khi-1).fill(false);
00047 
00048   for (int k=klo;k<=khi;++k)
00049     for (int j=jlo;j<=jhi;++j)
00050     {
00051       const bool* src_p = &src_image(ilo,j,k);
00052       bool* dest_p = &dest_image(ilo,j,k);
00053 
00054       for (int i=ilo;i<=ihi;++i,src_p+=s_istep,dest_p+=d_istep)
00055         *dest_p=vil3d_binary_erode(src_p,&offset[0],offset.size());
00056     }
00057 }