vil_exp_grad_filter_1d.h
Go to the documentation of this file.
1 #ifndef vil_exp_grad_filter_1d_h_
2 #define vil_exp_grad_filter_1d_h_
3 //:
4 // \file
5 // \brief Apply exponential gradient filter
6 // \author Tim Cootes
7 
8 #include <vil/vil_image_view.h>
9 
10 //: Apply exponential gradient filter to 1D data. Form: sign(i)*exp(c*|i|)
11 // Apply filter to n values src[i*sstep] to produce output dest[i*dstep]
12 // Exponential gradient filter of the form
13 // sign(i)*exp(c*|i|) applied. c=log(k)
14 // Uses fast recursive implementation.
15 template <class srcT, class destT, class accumT>
16 inline void vil_exp_grad_filter_1d(const srcT* src, std::ptrdiff_t sstep,
17  destT* dest, std::ptrdiff_t dstep,
18  int n, accumT k)
19 {
20  const srcT* s = src;
21  const srcT* src_end = src + (n-1)*sstep;
22 
23  // Zero first element
24  dest[0]=0; dest+=dstep;
25 
26  // Forward pass to compute -ive part of filter response
27  // Initialise for first element
28  accumT rt= -1*(accumT) *s; s+=sstep;
29  accumT k_sum=(accumT) 1;
30 
31  while (s!=src_end)
32  {
33  *dest = (destT)(rt/k_sum); // Set value for -ive half of filter
34  rt *= k; k_sum *= k; // Scale sums
35  rt -= *s; k_sum += 1.0f; // Increment with next element
36  s+=sstep; dest+=dstep; // Move to next element
37  }
38 
39  // Backward pass to compute +ive part of filter response
40  dest[0]=0; dest-=dstep;
41  rt= (accumT) *s; s-=sstep;
42  k_sum=(accumT) 1;
43  src_end = src;
44  while (s!=src_end)
45  {
46  *dest += (destT)(rt/k_sum); // Add in value for +ive half of filter
47  rt *= k; k_sum *= k; // Scale sums
48  rt += *s; k_sum += 1.0f; // Increment with next element
49  s-=sstep; dest-=dstep; // Move to next element
50  }
51 }
52 
53 //: Apply exponential gradient filter to src_im (along i direction).
54 // Exponential gradient filter of the form sign(i)*exp(c*|i|) applied. c=log(k)
55 // Uses fast recursive implementation.
56 // \relatesalso vil_image_view
57 template <class srcT, class destT, class accumT>
58 inline void vil_exp_grad_filter_i(const vil_image_view<srcT>& src_im,
59  vil_image_view<destT>& dest_im,
60  accumT k)
61 {
62  unsigned ni = src_im.ni();
63  unsigned nj = src_im.nj();
64  dest_im.set_size(ni,nj,src_im.nplanes());
65  std::ptrdiff_t s_istep = src_im.istep(), s_jstep = src_im.jstep();
66  std::ptrdiff_t d_istep = dest_im.istep(),d_jstep = dest_im.jstep();
67 
68  for (unsigned p=0;p<src_im.nplanes();++p)
69  {
70  const srcT* src_row = src_im.top_left_ptr()+p*src_im.planestep();
71  destT* dest_row = dest_im.top_left_ptr()+p*dest_im.planestep();
72  // Filter every row
73  for (unsigned j=0;j<nj;++j,src_row+=s_jstep,dest_row+=d_jstep)
74  vil_exp_grad_filter_1d(src_row,s_istep, dest_row,d_istep, ni, k);
75  }
76 }
77 
78 //: Apply exponential gradient filter to src_im (along j direction).
79 // Exponential gradient filter of the form sign(j)*exp(c*|j|) applied. c=log(k)
80 // Uses fast recursive implementation.
81 // \relatesalso vil_image_view
82 template <class srcT, class destT, class accumT>
83 inline void vil_exp_grad_filter_j(const vil_image_view<srcT>& src_im,
84  vil_image_view<destT>& dest_im,
85  accumT k)
86 {
87  unsigned ni = src_im.ni();
88  unsigned nj = src_im.nj();
89  dest_im.set_size(ni,nj,src_im.nplanes());
90  std::ptrdiff_t s_istep = src_im.istep(), s_jstep = src_im.jstep();
91  std::ptrdiff_t d_istep = dest_im.istep(),d_jstep = dest_im.jstep();
92 
93  for (unsigned p=0;p<src_im.nplanes();++p)
94  {
95  const srcT* src_col = src_im.top_left_ptr()+p*src_im.planestep();
96  destT* dest_col = dest_im.top_left_ptr()+p*dest_im.planestep();
97  // Filter every column
98  for (unsigned i=0;i<ni;++i,src_col+=s_istep,dest_col+=d_istep)
99  vil_exp_grad_filter_1d(src_col,s_jstep, dest_col,d_jstep, nj, k);
100  }
101 }
102 
103 #endif // vil_exp_grad_filter_1d_h_
void vil_exp_grad_filter_1d(const srcT *src, std::ptrdiff_t sstep, destT *dest, std::ptrdiff_t dstep, int n, accumT k)
Apply exponential gradient filter to 1D data. Form: sign(i)*exp(c*|i|).
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_exp_grad_filter_j(const vil_image_view< srcT > &src_im, vil_image_view< destT > &dest_im, accumT k)
Apply exponential gradient filter to src_im (along j direction).
A base class reference-counting view of some image data.
void vil_exp_grad_filter_i(const vil_image_view< srcT > &src_im, vil_image_view< destT > &dest_im, accumT k)
Apply exponential gradient filter to src_im (along i direction).
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.