vil_correlate_2d.h
Go to the documentation of this file.
1 // This is core/vil/algo/vil_correlate_2d.h
2 #ifndef vil_correlate_2d_h_
3 #define vil_correlate_2d_h_
4 //:
5 // \file
6 // \brief 2D Convolution
7 // \author Tim Cootes
8 
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 #include <cassert>
13 #include <vil/vil_image_view.h>
14 
15 //: Evaluate dot product between kernel and src_im
16 // Returns sum_ijp src_im[i*istep+j*jstep+p*pstep]*kernel(i,j,p)
17 // \relatesalso vil_image_view
18 template <class srcT, class kernelT, class accumT>
19 inline accumT vil_correlate_2d_at_pt(const srcT *src_im, std::ptrdiff_t s_istep,
20  std::ptrdiff_t s_jstep, std::ptrdiff_t s_pstep,
21  const vil_image_view<kernelT>& kernel,
22  accumT)
23 {
24  unsigned ni = kernel.ni();
25  unsigned nj = kernel.nj();
26  unsigned np = kernel.nplanes();
27 
28  std::ptrdiff_t k_istep = kernel.istep(), k_jstep = kernel.jstep();
29 
30  accumT sum=0;
31  for (unsigned p = 0; p<np; ++p)
32  {
33  // Select first row of p-th plane
34  const srcT* src_row = src_im + p*s_pstep;
35  const kernelT* k_row = kernel.top_left_ptr() + p*kernel.planestep();
36 
37  for (unsigned int j=0;j<nj;++j,src_row+=s_jstep,k_row+=k_jstep)
38  {
39  const srcT* sp = src_row;
40  const kernelT* kp = k_row;
41  // Sum over j-th row
42  for (unsigned int i=0;i<ni;++i, sp += s_istep, kp += k_istep)
43  sum += accumT(*sp)*accumT(*kp);
44  }
45  }
46 
47  return sum;
48 }
49 
50 //: Correlate kernel with srcT
51 // dest is resized to (1+src_im.ni()-kernel.ni())x(1+src_im.nj()-kernel.nj())
52 // (a one plane image).
53 // On exit dest(x,y) = sum_ij src_im(x+i,y+j)*kernel(i,j)
54 // \relatesalso vil_image_view
55 template <class srcT, class destT, class kernelT, class accumT>
56 inline void vil_correlate_2d(const vil_image_view<srcT>& src_im,
57  vil_image_view<destT>& dest_im,
58  const vil_image_view<kernelT>& kernel,
59  accumT ac)
60 {
61  int ni = 1+src_im.ni()-kernel.ni(); assert(ni >= 0);
62  int nj = 1+src_im.nj()-kernel.nj(); assert(nj >= 0);
63  std::ptrdiff_t s_istep = src_im.istep(), s_jstep = src_im.jstep();
64  std::ptrdiff_t s_pstep = src_im.planestep();
65 
66  dest_im.set_size(ni,nj,1);
67  std::ptrdiff_t d_istep = dest_im.istep(),d_jstep = dest_im.jstep();
68 
69  // Select first row of p-th plane
70  const srcT* src_row = src_im.top_left_ptr();
71  destT* dest_row = dest_im.top_left_ptr();
72 
73  for (int j=0;j<nj;++j,src_row+=s_jstep,dest_row+=d_jstep)
74  {
75  const srcT* sp = src_row;
76  destT* dp = dest_row;
77  for (int i=0;i<ni;++i, sp += s_istep, dp += d_istep)
78  *dp = (destT)vil_correlate_2d_at_pt(sp,s_istep,s_jstep,s_pstep,kernel,ac);
79  // Correlate at src(i,j)
80  }
81 }
82 
83 #endif // vil_correlate_2d_h_
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.
accumT vil_correlate_2d_at_pt(const srcT *src_im, std::ptrdiff_t s_istep, std::ptrdiff_t s_jstep, std::ptrdiff_t s_pstep, const vil_image_view< kernelT > &kernel, accumT)
Evaluate dot product between kernel and src_im.
void vil_correlate_2d(const vil_image_view< srcT > &src_im, vil_image_view< destT > &dest_im, const vil_image_view< kernelT > &kernel, accumT ac)
Correlate kernel with srcT.
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.
A base class reference-counting view of some image data.
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.