core/vil/algo/vil_threshold.txx
Go to the documentation of this file.
00001 #ifndef vil_threshold_txx_
00002 #define vil_threshold_txx_
00003 //:
00004 // \file
00005 // \brief Apply thresholds to image data
00006 // \author Tim Cootes
00007 
00008 #include "vil_threshold.h"
00009 
00010 //: Apply threshold such that dest(i,j,p)=true if src(i,j,p)>=t
00011 template<class srcT>
00012 void vil_threshold_above(const vil_image_view<srcT>& src,
00013                          vil_image_view<bool>& dest,  srcT t)
00014 {
00015   unsigned ni = src.ni(),nj = src.nj(),np = src.nplanes();
00016   dest.set_size(ni,nj,np);
00017 
00018   vcl_ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),pstepA = src.planestep();
00019   vcl_ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),pstepB = dest.planestep();
00020   const srcT* planeA = src.top_left_ptr();
00021   bool* planeB = dest.top_left_ptr();
00022   for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00023   {
00024     const srcT* rowA   = planeA;
00025     bool* rowB   = planeB;
00026     for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00027     {
00028       const srcT* pixelA = rowA;
00029       bool* pixelB = rowB;
00030       for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00031         *pixelB = *pixelA>=t;
00032     }
00033   }
00034 }
00035 
00036 //: Apply threshold such that dest(i,j,p)=true if src(i,j,p)<=t
00037 template<class srcT>
00038 void vil_threshold_below(const vil_image_view<srcT>& src,
00039                          vil_image_view<bool>& dest,  srcT t)
00040 {
00041   unsigned ni = src.ni(),nj = src.nj(),np = src.nplanes();
00042   dest.set_size(ni,nj,np);
00043 
00044   vcl_ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),pstepA = src.planestep();
00045   vcl_ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),pstepB = dest.planestep();
00046   const srcT* planeA = src.top_left_ptr();
00047   bool* planeB = dest.top_left_ptr();
00048   for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00049   {
00050     const srcT* rowA   = planeA;
00051     bool* rowB   = planeB;
00052     for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00053     {
00054       const srcT* pixelA = rowA;
00055       bool* pixelB = rowB;
00056       for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00057         *pixelB = *pixelA<=t;
00058     }
00059   }
00060 }
00061 
00062 //: Apply threshold such that dest(i,j,p)=true if t0<=src(i,j,p)<=t1
00063 template<class srcT>
00064 void vil_threshold_inside(const vil_image_view<srcT>& src,
00065                           vil_image_view<bool>& dest,  srcT t0, srcT t1)
00066 {
00067   unsigned ni = src.ni(),nj = src.nj(),np = src.nplanes();
00068   dest.set_size(ni,nj,np);
00069 
00070   vcl_ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),pstepA = src.planestep();
00071   vcl_ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),pstepB = dest.planestep();
00072   const srcT* planeA = src.top_left_ptr();
00073   bool* planeB = dest.top_left_ptr();
00074   for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00075   {
00076     const srcT* rowA   = planeA;
00077     bool* rowB   = planeB;
00078     for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00079     {
00080       const srcT* pixelA = rowA;
00081       bool* pixelB = rowB;
00082       for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00083         *pixelB = (t0<=*pixelA) && (*pixelA<=t1);
00084     }
00085   }
00086 }
00087 
00088 //: Apply threshold such that dest(i,j,p)=true if src(i,j,p)<=t0 or src(i,j,p)>=t1
00089 template<class srcT>
00090 void vil_threshold_outside(const vil_image_view<srcT>& src,
00091                            vil_image_view<bool>& dest,  srcT t0, srcT t1)
00092 {
00093   unsigned ni = src.ni(),nj = src.nj(),np = src.nplanes();
00094   dest.set_size(ni,nj,np);
00095 
00096   vcl_ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),pstepA = src.planestep();
00097   vcl_ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),pstepB = dest.planestep();
00098   const srcT* planeA = src.top_left_ptr();
00099   bool* planeB = dest.top_left_ptr();
00100   for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00101   {
00102     const srcT* rowA   = planeA;
00103     bool* rowB   = planeB;
00104     for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00105     {
00106       const srcT* pixelA = rowA;
00107       bool* pixelB = rowB;
00108       for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00109         *pixelB = (*pixelA<=t0) || (*pixelA>=t1);
00110     }
00111   }
00112 }
00113 
00114 #undef VIL_THRESHOLD_INSTANTIATE
00115 #define VIL_THRESHOLD_INSTANTIATE(srcT) \
00116 template void vil_threshold_above(const vil_image_view<srcT >& src, \
00117                                   vil_image_view<bool >& dest,  srcT t); \
00118 template void vil_threshold_below(const vil_image_view<srcT >& src, \
00119                                   vil_image_view<bool >& dest,  srcT t); \
00120 template void vil_threshold_inside(const vil_image_view<srcT >& src, \
00121                                    vil_image_view<bool >& dest,  srcT t0, srcT t1); \
00122 template void vil_threshold_outside(const vil_image_view<srcT >& src, \
00123                                     vil_image_view<bool >& dest,  srcT t0, srcT t1)
00124 
00125 #endif // vil_threshold_txx_