auxiliary
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros
integral.hpp
Go to the documentation of this file.
1 
38 #pragma once
39 
40 namespace auxiliary
41 {
43  template <typename T1, typename T2>
44  void integral(const arma::Mat<T1>& A, arma::Mat<T2>& I)
45  {
46  typedef typename arma::uword size_type;
47 
48  // set size
49  I.set_size(A.n_rows, A.n_cols);
50 
51  // Naive implementation
52  const T1* ptr = A.memptr();
53  T2* iptr = I.memptr();
54 
55  iptr[0] = static_cast<T2>(ptr[0]);
56 
57  // 0-th column
58  for (size_type y = 1 ; y < A.n_rows ; y++)
59  iptr[y] = iptr[y - 1] + static_cast<T2>(ptr[y]);
60 
61  // 0-th row
62  for (size_type x = 1 ; x < A.n_cols ; x++)
63  I.at(0, x) = I.at(0, x - 1) + static_cast<T2>(A.at(0, x));
64 
65  const T2* iptr0 = iptr;
66  for (size_type x = 1 ; x < A.n_cols ; x++) {
67  ptr = A.colptr(x);
68  T2* iptr1 = I.colptr(x);
69  T2 s = 0;
70  for (size_type y = 1 ; y < A.n_rows ; y++) {
71  s += static_cast<T2>(ptr[y]);
72  iptr1[y] = iptr0[y] + s;
73  }
74  }
75  }
76 
83  template <typename T1, typename T2, typename T3>
84  void integral(const Image<T1>& img, Image<T2>& sum, Image<T3>& sqsum)
85  {
86  typedef typename Image<T1>::size_type size_type;
87 
88  // allocate images
89  sum.resize(img.width(), img.height());
90  sqsum.resize(img.width(), img.height());
91 
92  // simplest implementation
93  const T1* ptr = img.memptr();
94  T2* sumptr = sum.memptr();
95  T3* sqsumptr = sqsum.memptr();
96 
97  T2 s = 0;
98  T3 sq = 0;
99 
100  // image is column major,
101  // 0-th column accumulated
102  for (size_type y = 0 ; y < img.height() ; y++) {
103  const T1 it = ptr[y];
104  s += it;
105  sq += (T3)it * it;
106  sumptr[y] = s;
107  sqsumptr[y] = sq;
108  }
109 
110  s = sumptr[0];
111  sq = sqsumptr[0];
112  // 0-th row accumulated
113  for (size_type x = 1 ; x < img.width() ; x++) {
114  T2 it = img(0, x);
115  s += it;
116  sq += (T3)it * it;
117  sum.at(0, x) = s;
118  sqsum.at(0, x) = sq;
119  }
120 
121  T2* sptr0 = sum.colptr(0);
122  T3* sqptr0 = sqsum.colptr(0);
123 
124  for (size_type x = 1 ; x < img.width() ; x++) {
125  T2 s = 0;
126  T3 sq = 0;
127 
128  T2* sptr1 = sum.colptr(x);
129  T3* sqptr1 = sqsum.colptr(x);
130 
131  for (size_type y = 0 ; y < img.height() ; y++) {
132  T2 it = img(y, x);
133  s += it;
134  sq += (T3)it * it;
135  //sum.at(y, x) = sum.at(y, x - 1) + s;
136  sptr1[y] = sptr0[y] + s;
137  //sqsum.at(y, x) = sqsum.at(y, x - 1) + sq;
138  sqptr1[y] = sqptr0[y] + sq;
139  }
140 
141  sptr0 = sptr1;
142  sqptr0 = sqptr1;
143  }
144  }
145 }
void integral(const arma::Mat< T1 > &A, arma::Mat< T2 > &I)
Compute integral.
Definition: integral.hpp:44
A template image class.
Definition: imgproc_aux.hpp:86
void resize(size_type width, size_type height)
Resize image.
Definition: imgproc_aux.hpp:136
size_type height() const
Get image height.
Definition: imgproc_aux.hpp:133
size_type width() const
Get image width.
Definition: imgproc_aux.hpp:130
arma::uword size_type
Definition: imgproc_aux.hpp:92