contrib/mul/vil3d/algo/vil3d_threshold.txx
Go to the documentation of this file.
00001 #ifndef vil3d_threshold_txx_
00002 #define vil3d_threshold_txx_
00003 //:
00004 // \file
00005 // \brief Apply thresholds to image data
00006 // \author Tim Cootes
00007 
00008 #include "vil3d_threshold.h"
00009 
00010 //: Apply threshold such that dest(i,j,k,p)=true if src(i,j,k,p)>=t
00011 template<class srcT>
00012 void vil3d_threshold_above(const vil3d_image_view<srcT>& src,
00013                            vil3d_image_view<bool>& dest,  srcT t)
00014 {
00015   unsigned ni = src.ni(),nj = src.nj(),nk = src.nk(),np = src.nplanes();
00016   dest.set_size(ni,nj,nk,np);
00017 
00018   vcl_ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),kstepA=src.kstep();
00019   vcl_ptrdiff_t pstepA = src.planestep();
00020   vcl_ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),kstepB=dest.kstep();
00021   vcl_ptrdiff_t pstepB = dest.planestep();
00022   const srcT* planeA = src.origin_ptr();
00023   bool* planeB = dest.origin_ptr();
00024   for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00025   {
00026     const srcT* sliceA = planeA;
00027     bool* sliceB = planeB;
00028     for (unsigned k=0;k<nk;++k,sliceA += kstepA,sliceB += kstepB)
00029     {
00030       const srcT* rowA   = sliceA;
00031       bool* rowB   = sliceB;
00032       for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00033       {
00034         const srcT* pixelA = rowA;
00035         bool* pixelB = rowB;
00036         for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00037           *pixelB = *pixelA>=t;
00038       }
00039     }
00040   }
00041 }
00042 
00043 //: Apply threshold such that dest(i,j,k,p)=true if src(i,j,k,p)<=t
00044 template<class srcT>
00045 void vil3d_threshold_below(const vil3d_image_view<srcT>& src,
00046                            vil3d_image_view<bool>& dest,  srcT t)
00047 {
00048   unsigned ni = src.ni(),nj = src.nj(),nk = src.nk(),np = src.nplanes();
00049   dest.set_size(ni,nj,nk,np);
00050 
00051   vcl_ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),kstepA=src.kstep();
00052   vcl_ptrdiff_t pstepA = src.planestep();
00053   vcl_ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),kstepB=dest.kstep();
00054   vcl_ptrdiff_t pstepB = dest.planestep();
00055   const srcT* planeA = src.origin_ptr();
00056   bool* planeB = dest.origin_ptr();
00057   for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00058   {
00059     const srcT* sliceA = planeA;
00060     bool* sliceB = planeB;
00061     for (unsigned k=0;k<nk;++k,sliceA += kstepA,sliceB += kstepB)
00062     {
00063       const srcT* rowA   = sliceA;
00064       bool* rowB   = sliceB;
00065       for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00066       {
00067         const srcT* pixelA = rowA;
00068         bool* pixelB = rowB;
00069         for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00070           *pixelB = *pixelA<=t;
00071       }
00072     }
00073   }
00074 }
00075 
00076 //: Apply threshold such that dest(i,j,k,p)=true if t0<=src(i,j,k,p)<=t1
00077 template<class srcT>
00078 void vil3d_threshold_inside(const vil3d_image_view<srcT>& src,
00079                             vil3d_image_view<bool>& dest,  srcT t0, srcT t1)
00080 {
00081   unsigned ni = src.ni(),nj = src.nj(),nk = src.nk(),np = src.nplanes();
00082   dest.set_size(ni,nj,nk,np);
00083 
00084   vcl_ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),kstepA=src.kstep();
00085   vcl_ptrdiff_t pstepA = src.planestep();
00086   vcl_ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),kstepB=dest.kstep();
00087   vcl_ptrdiff_t pstepB = dest.planestep();
00088   const srcT* planeA = src.origin_ptr();
00089   bool* planeB = dest.origin_ptr();
00090   for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00091   {
00092     const srcT* sliceA = planeA;
00093     bool* sliceB = planeB;
00094     for (unsigned k=0;k<nk;++k,sliceA += kstepA,sliceB += kstepB)
00095     {
00096       const srcT* rowA   = sliceA;
00097       bool* rowB   = sliceB;
00098       for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00099       {
00100         const srcT* pixelA = rowA;
00101         bool* pixelB = rowB;
00102         for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00103           *pixelB = (t0<=*pixelA) && (*pixelA<=t1);
00104       }
00105     }
00106   }
00107 }
00108 
00109 //: Apply threshold such that dest(i,j,k,p)=true if src(i,j,k,p)<=t0 or src(i,j,k,p)>=t1
00110 template<class srcT>
00111 void vil3d_threshold_outside(const vil3d_image_view<srcT>& src,
00112                              vil3d_image_view<bool>& dest,  srcT t0, srcT t1)
00113 {
00114   unsigned ni = src.ni(),nj = src.nj(),nk = src.nk(),np = src.nplanes();
00115   dest.set_size(ni,nj,nk,np);
00116 
00117   vcl_ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),kstepA=src.kstep();
00118   vcl_ptrdiff_t pstepA = src.planestep();
00119   vcl_ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),kstepB=dest.kstep();
00120   vcl_ptrdiff_t pstepB = dest.planestep();
00121   const srcT* planeA = src.origin_ptr();
00122   bool* planeB = dest.origin_ptr();
00123   for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00124   {
00125     const srcT* sliceA = planeA;
00126     bool* sliceB = planeB;
00127     for (unsigned k=0;k<nk;++k,sliceA += kstepA,sliceB += kstepB)
00128     {
00129       const srcT* rowA   = sliceA;
00130       bool* rowB   = sliceB;
00131       for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00132       {
00133         const srcT* pixelA = rowA;
00134         bool* pixelB = rowB;
00135         for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00136           *pixelB = (*pixelA<=t0) || (*pixelA>=t1);
00137       }
00138     }
00139   }
00140 }
00141 
00142 #undef VIL3D_THRESHOLD_INSTANTIATE
00143 #define VIL3D_THRESHOLD_INSTANTIATE(srcT) \
00144 template void vil3d_threshold_above(const vil3d_image_view<srcT >& src, \
00145                           vil3d_image_view<bool >& dest,  srcT t); \
00146 template void vil3d_threshold_below(const vil3d_image_view<srcT >& src, \
00147                           vil3d_image_view<bool >& dest,  srcT t); \
00148 template void vil3d_threshold_inside(const vil3d_image_view<srcT >& src, \
00149                           vil3d_image_view<bool >& dest,  srcT t0, srcT t1); \
00150 template void vil3d_threshold_outside(const vil3d_image_view<srcT >& src, \
00151                           vil3d_image_view<bool >& dest,  srcT t0, srcT t1)
00152 
00153 #endif // vil3d_threshold_txx_