vil_threshold.hxx
Go to the documentation of this file.
1 #ifndef vil_threshold_hxx_
2 #define vil_threshold_hxx_
3 //:
4 // \file
5 // \brief Apply thresholds to image data
6 // \author Tim Cootes
7 
8 #include "vil_threshold.h"
9 
10 //: Apply threshold such that dest(i,j,p)=true if src(i,j,p)>=t
11 template<class srcT>
13  vil_image_view<bool>& dest, srcT t)
14 {
15  unsigned ni = src.ni(),nj = src.nj(),np = src.nplanes();
16  dest.set_size(ni,nj,np);
17 
18  std::ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),pstepA = src.planestep();
19  std::ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),pstepB = dest.planestep();
20  const srcT* planeA = src.top_left_ptr();
21  bool* planeB = dest.top_left_ptr();
22  for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
23  {
24  const srcT* rowA = planeA;
25  bool* rowB = planeB;
26  for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
27  {
28  const srcT* pixelA = rowA;
29  bool* pixelB = rowB;
30  for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
31  *pixelB = *pixelA>=t;
32  }
33  }
34 }
35 
36 //: Apply threshold such that dest(i,j,p)=true if src(i,j,p)<=t
37 template<class srcT>
39  vil_image_view<bool>& dest, srcT t)
40 {
41  unsigned ni = src.ni(),nj = src.nj(),np = src.nplanes();
42  dest.set_size(ni,nj,np);
43 
44  std::ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),pstepA = src.planestep();
45  std::ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),pstepB = dest.planestep();
46  const srcT* planeA = src.top_left_ptr();
47  bool* planeB = dest.top_left_ptr();
48  for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
49  {
50  const srcT* rowA = planeA;
51  bool* rowB = planeB;
52  for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
53  {
54  const srcT* pixelA = rowA;
55  bool* pixelB = rowB;
56  for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
57  *pixelB = *pixelA<=t;
58  }
59  }
60 }
61 
62 //: Apply threshold such that dest(i,j,p)=true if t0<=src(i,j,p)<=t1
63 template<class srcT>
65  vil_image_view<bool>& dest, srcT t0, srcT t1)
66 {
67  unsigned ni = src.ni(),nj = src.nj(),np = src.nplanes();
68  dest.set_size(ni,nj,np);
69 
70  std::ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),pstepA = src.planestep();
71  std::ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),pstepB = dest.planestep();
72  const srcT* planeA = src.top_left_ptr();
73  bool* planeB = dest.top_left_ptr();
74  for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
75  {
76  const srcT* rowA = planeA;
77  bool* rowB = planeB;
78  for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
79  {
80  const srcT* pixelA = rowA;
81  bool* pixelB = rowB;
82  for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
83  *pixelB = (t0<=*pixelA) && (*pixelA<=t1);
84  }
85  }
86 }
87 
88 //: Apply threshold such that dest(i,j,p)=true if src(i,j,p)<=t0 or src(i,j,p)>=t1
89 template<class srcT>
91  vil_image_view<bool>& dest, srcT t0, srcT t1)
92 {
93  unsigned ni = src.ni(),nj = src.nj(),np = src.nplanes();
94  dest.set_size(ni,nj,np);
95 
96  std::ptrdiff_t istepA=src.istep(),jstepA=src.jstep(),pstepA = src.planestep();
97  std::ptrdiff_t istepB=dest.istep(),jstepB=dest.jstep(),pstepB = dest.planestep();
98  const srcT* planeA = src.top_left_ptr();
99  bool* planeB = dest.top_left_ptr();
100  for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
101  {
102  const srcT* rowA = planeA;
103  bool* rowB = planeB;
104  for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
105  {
106  const srcT* pixelA = rowA;
107  bool* pixelB = rowB;
108  for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
109  *pixelB = (*pixelA<=t0) || (*pixelA>=t1);
110  }
111  }
112 }
113 
114 #undef VIL_THRESHOLD_INSTANTIATE
115 #define VIL_THRESHOLD_INSTANTIATE(srcT) \
116 template void vil_threshold_above(const vil_image_view<srcT >& src, \
117  vil_image_view<bool >& dest, srcT t); \
118 template void vil_threshold_below(const vil_image_view<srcT >& src, \
119  vil_image_view<bool >& dest, srcT t); \
120 template void vil_threshold_inside(const vil_image_view<srcT >& src, \
121  vil_image_view<bool >& dest, srcT t0, srcT t1); \
122 template void vil_threshold_outside(const vil_image_view<srcT >& src, \
123  vil_image_view<bool >& dest, srcT t0, srcT t1)
124 
125 #endif // vil_threshold_hxx_
Apply thresholds to image data.
void vil_threshold_below(const vil_image_view< srcT > &src, vil_image_view< bool > &dest, srcT t)
Apply threshold such that dest(i,j,p)=true if src(i,j,p)<=t.
void vil_threshold_above(const vil_image_view< srcT > &src, vil_image_view< bool > &dest, srcT t)
Apply threshold such that dest(i,j,p)=true if src(i,j,p)>=t.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
void set_size(unsigned ni, unsigned nj) override
resize current planes to ni x nj.
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
unsigned nj() const
Height.
std::ptrdiff_t planestep() const
Add this to your pixel pointer to get pixel on next plane.
void vil_threshold_inside(const vil_image_view< srcT > &src, vil_image_view< bool > &dest, srcT t0, srcT t1)
Apply threshold such that dest(i,j,p)=true if t0<=src(i,j,p)<=t1.
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
unsigned nplanes() const
Number of planes.
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.
void vil_threshold_outside(const vil_image_view< srcT > &src, vil_image_view< bool > &dest, srcT t0, srcT t1)
Apply threshold such that dest(i,j,p)=true if src(i,j,p)<=t0 or src(i,j,p)>=t1.