41 #ifndef PCL_INTEGRAL_IMAGE2D_IMPL_H_
42 #define PCL_INTEGRAL_IMAGE2D_IMPL_H_
48 template <
typename DataType,
unsigned Dimension>
void
51 compute_second_order_integral_images_ = compute_second_order_integral_images;
55 template <
typename DataType,
unsigned Dimension>
void
58 if ((width + 1) * (height + 1) > first_order_integral_image_.size () )
62 first_order_integral_image_.resize ( (width_ + 1) * (height_ + 1) );
63 finite_values_integral_image_.resize ( (width_ + 1) * (height_ + 1) );
64 if (compute_second_order_integral_images_)
65 second_order_integral_image_.resize ( (width_ + 1) * (height_ + 1) );
67 computeIntegralImages (data, row_stride, element_stride);
73 unsigned start_x,
unsigned start_y,
unsigned width,
unsigned height)
const
75 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
76 const unsigned upper_right_idx = upper_left_idx + width;
77 const unsigned lower_left_idx = (start_y + height) * (width_ + 1) + start_x;
78 const unsigned lower_right_idx = lower_left_idx + width;
80 return (first_order_integral_image_[lower_right_idx] + first_order_integral_image_[upper_left_idx] -
81 first_order_integral_image_[upper_right_idx] - first_order_integral_image_[lower_left_idx] );
87 unsigned start_x,
unsigned start_y,
unsigned width,
unsigned height)
const
89 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
90 const unsigned upper_right_idx = upper_left_idx + width;
91 const unsigned lower_left_idx = (start_y + height) * (width_ + 1) + start_x;
92 const unsigned lower_right_idx = lower_left_idx + width;
94 return (second_order_integral_image_[lower_right_idx] + second_order_integral_image_[upper_left_idx] -
95 second_order_integral_image_[upper_right_idx] - second_order_integral_image_[lower_left_idx] );
99 template <
typename DataType,
unsigned Dimension>
unsigned
101 unsigned start_x,
unsigned start_y,
unsigned width,
unsigned height)
const
103 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
104 const unsigned upper_right_idx = upper_left_idx + width;
105 const unsigned lower_left_idx = (start_y + height) * (width_ + 1) + start_x;
106 const unsigned lower_right_idx = lower_left_idx + width;
108 return (finite_values_integral_image_[lower_right_idx] + finite_values_integral_image_[upper_left_idx] -
109 finite_values_integral_image_[upper_right_idx] - finite_values_integral_image_[lower_left_idx] );
115 unsigned start_x,
unsigned start_y,
unsigned end_x,
unsigned end_y)
const
117 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
118 const unsigned upper_right_idx = start_y * (width_ + 1) + end_x;
119 const unsigned lower_left_idx = end_y * (width_ + 1) + start_x;
120 const unsigned lower_right_idx = end_y * (width_ + 1) + end_x;
122 return (first_order_integral_image_[lower_right_idx] + first_order_integral_image_[upper_left_idx] -
123 first_order_integral_image_[upper_right_idx] - first_order_integral_image_[lower_left_idx] );
129 unsigned start_x,
unsigned start_y,
unsigned end_x,
unsigned end_y)
const
131 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
132 const unsigned upper_right_idx = start_y * (width_ + 1) + end_x;
133 const unsigned lower_left_idx = end_y * (width_ + 1) + start_x;
134 const unsigned lower_right_idx = end_y * (width_ + 1) + end_x;
136 return (second_order_integral_image_[lower_right_idx] + second_order_integral_image_[upper_left_idx] -
137 second_order_integral_image_[upper_right_idx] - second_order_integral_image_[lower_left_idx] );
141 template <
typename DataType,
unsigned Dimension>
unsigned
143 unsigned start_x,
unsigned start_y,
unsigned end_x,
unsigned end_y)
const
145 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
146 const unsigned upper_right_idx = start_y * (width_ + 1) + end_x;
147 const unsigned lower_left_idx = end_y * (width_ + 1) + start_x;
148 const unsigned lower_right_idx = end_y * (width_ + 1) + end_x;
150 return (finite_values_integral_image_[lower_right_idx] + finite_values_integral_image_[upper_left_idx] -
151 finite_values_integral_image_[upper_right_idx] - finite_values_integral_image_[lower_left_idx] );
155 template <
typename DataType,
unsigned Dimension>
void
157 const DataType *data,
unsigned row_stride,
unsigned element_stride)
159 ElementType* previous_row = first_order_integral_image_.data();
160 ElementType* current_row = previous_row + (width_ + 1);
161 for (
unsigned int i = 0; i < (width_ + 1); ++i)
162 previous_row[i].setZero();
164 unsigned* count_previous_row = finite_values_integral_image_.data();
165 unsigned* count_current_row = count_previous_row + (width_ + 1);
166 std::fill_n(count_previous_row, width_ + 1, 0);
168 if (!compute_second_order_integral_images_)
170 for (
unsigned rowIdx = 0; rowIdx < height_; ++rowIdx, data += row_stride,
171 previous_row = current_row, current_row += (width_ + 1),
172 count_previous_row = count_current_row, count_current_row += (width_ + 1))
174 current_row [0].setZero ();
175 count_current_row [0] = 0;
176 for (
unsigned colIdx = 0, valIdx = 0; colIdx < width_; ++colIdx, valIdx += element_stride)
178 current_row [colIdx + 1] = previous_row [colIdx + 1] + current_row [colIdx] - previous_row [colIdx];
179 count_current_row [colIdx + 1] = count_previous_row [colIdx + 1] + count_current_row [colIdx] - count_previous_row [colIdx];
180 const auto* element =
reinterpret_cast <const InputType*
> (&data [valIdx]);
181 if (std::isfinite (element->sum ()))
183 current_row [colIdx + 1] += element->template cast<typename IntegralImageTypeTraits<DataType>::IntegralType>();
184 ++(count_current_row [colIdx + 1]);
191 SecondOrderType* so_previous_row = second_order_integral_image_.data();
192 SecondOrderType* so_current_row = so_previous_row + (width_ + 1);
193 for (
unsigned int i = 0; i < (width_ + 1); ++i)
194 so_previous_row[i].setZero();
197 for (
unsigned rowIdx = 0; rowIdx < height_; ++rowIdx, data += row_stride,
198 previous_row = current_row, current_row += (width_ + 1),
199 count_previous_row = count_current_row, count_current_row += (width_ + 1),
200 so_previous_row = so_current_row, so_current_row += (width_ + 1))
202 current_row [0].setZero ();
203 so_current_row [0].setZero ();
204 count_current_row [0] = 0;
205 for (
unsigned colIdx = 0, valIdx = 0; colIdx < width_; ++colIdx, valIdx += element_stride)
207 current_row [colIdx + 1] = previous_row [colIdx + 1] + current_row [colIdx] - previous_row [colIdx];
208 so_current_row [colIdx + 1] = so_previous_row [colIdx + 1] + so_current_row [colIdx] - so_previous_row [colIdx];
209 count_current_row [colIdx + 1] = count_previous_row [colIdx + 1] + count_current_row [colIdx] - count_previous_row [colIdx];
211 const auto* element =
reinterpret_cast <const InputType*
> (&data [valIdx]);
212 if (std::isfinite (element->sum ()))
214 current_row [colIdx + 1] += element->template cast<typename IntegralImageTypeTraits<DataType>::IntegralType>();
215 ++(count_current_row [colIdx + 1]);
216 for (
unsigned myIdx = 0, elIdx = 0; myIdx < Dimension; ++myIdx)
217 for (
unsigned mxIdx = myIdx; mxIdx < Dimension; ++mxIdx, ++elIdx)
218 so_current_row [colIdx + 1][elIdx] += (*element)[myIdx] * (*element)[mxIdx];
226 template <
typename DataType>
void
229 if ((width + 1) * (height + 1) > first_order_integral_image_.size () )
233 first_order_integral_image_.resize ( (width_ + 1) * (height_ + 1) );
234 finite_values_integral_image_.resize ( (width_ + 1) * (height_ + 1) );
235 if (compute_second_order_integral_images_)
236 second_order_integral_image_.resize ( (width_ + 1) * (height_ + 1) );
238 computeIntegralImages (data, row_stride, element_stride);
244 unsigned start_x,
unsigned start_y,
unsigned width,
unsigned height)
const
246 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
247 const unsigned upper_right_idx = upper_left_idx + width;
248 const unsigned lower_left_idx = (start_y + height) * (width_ + 1) + start_x;
249 const unsigned lower_right_idx = lower_left_idx + width;
251 return (first_order_integral_image_[lower_right_idx] + first_order_integral_image_[upper_left_idx] -
252 first_order_integral_image_[upper_right_idx] - first_order_integral_image_[lower_left_idx] );
258 unsigned start_x,
unsigned start_y,
unsigned width,
unsigned height)
const
260 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
261 const unsigned upper_right_idx = upper_left_idx + width;
262 const unsigned lower_left_idx = (start_y + height) * (width_ + 1) + start_x;
263 const unsigned lower_right_idx = lower_left_idx + width;
265 return (second_order_integral_image_[lower_right_idx] + second_order_integral_image_[upper_left_idx] -
266 second_order_integral_image_[upper_right_idx] - second_order_integral_image_[lower_left_idx] );
270 template <
typename DataType>
unsigned
272 unsigned start_x,
unsigned start_y,
unsigned width,
unsigned height)
const
274 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
275 const unsigned upper_right_idx = upper_left_idx + width;
276 const unsigned lower_left_idx = (start_y + height) * (width_ + 1) + start_x;
277 const unsigned lower_right_idx = lower_left_idx + width;
279 return (finite_values_integral_image_[lower_right_idx] + finite_values_integral_image_[upper_left_idx] -
280 finite_values_integral_image_[upper_right_idx] - finite_values_integral_image_[lower_left_idx] );
286 unsigned start_x,
unsigned start_y,
unsigned end_x,
unsigned end_y)
const
288 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
289 const unsigned upper_right_idx = start_y * (width_ + 1) + end_x;
290 const unsigned lower_left_idx = end_y * (width_ + 1) + start_x;
291 const unsigned lower_right_idx = end_y * (width_ + 1) + end_x;
293 return (first_order_integral_image_[lower_right_idx] + first_order_integral_image_[upper_left_idx] -
294 first_order_integral_image_[upper_right_idx] - first_order_integral_image_[lower_left_idx] );
300 unsigned start_x,
unsigned start_y,
unsigned end_x,
unsigned end_y)
const
302 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
303 const unsigned upper_right_idx = start_y * (width_ + 1) + end_x;
304 const unsigned lower_left_idx = end_y * (width_ + 1) + start_x;
305 const unsigned lower_right_idx = end_y * (width_ + 1) + end_x;
307 return (second_order_integral_image_[lower_right_idx] + second_order_integral_image_[upper_left_idx] -
308 second_order_integral_image_[upper_right_idx] - second_order_integral_image_[lower_left_idx] );
312 template <
typename DataType>
unsigned
314 unsigned start_x,
unsigned start_y,
unsigned end_x,
unsigned end_y)
const
316 const unsigned upper_left_idx = start_y * (width_ + 1) + start_x;
317 const unsigned upper_right_idx = start_y * (width_ + 1) + end_x;
318 const unsigned lower_left_idx = end_y * (width_ + 1) + start_x;
319 const unsigned lower_right_idx = end_y * (width_ + 1) + end_x;
321 return (finite_values_integral_image_[lower_right_idx] + finite_values_integral_image_[upper_left_idx] -
322 finite_values_integral_image_[upper_right_idx] - finite_values_integral_image_[lower_left_idx] );
326 template <
typename DataType>
void
328 const DataType *data,
unsigned row_stride,
unsigned element_stride)
330 ElementType* previous_row = first_order_integral_image_.data();
331 ElementType* current_row = previous_row + (width_ + 1);
332 std::fill_n(previous_row, width_ + 1, 0);
334 unsigned* count_previous_row = finite_values_integral_image_.data();
335 unsigned* count_current_row = count_previous_row + (width_ + 1);
336 std::fill_n(count_previous_row, width_ + 1, 0);
338 if (!compute_second_order_integral_images_)
340 for (
unsigned rowIdx = 0; rowIdx < height_; ++rowIdx, data += row_stride,
341 previous_row = current_row, current_row += (width_ + 1),
342 count_previous_row = count_current_row, count_current_row += (width_ + 1))
344 current_row [0] = 0.0;
345 count_current_row [0] = 0;
346 for (
unsigned colIdx = 0, valIdx = 0; colIdx < width_; ++colIdx, valIdx += element_stride)
348 current_row [colIdx + 1] = previous_row [colIdx + 1] + current_row [colIdx] - previous_row [colIdx];
349 count_current_row [colIdx + 1] = count_previous_row [colIdx + 1] + count_current_row [colIdx] - count_previous_row [colIdx];
350 if (std::isfinite (data [valIdx]))
352 current_row [colIdx + 1] += data [valIdx];
353 ++(count_current_row [colIdx + 1]);
360 SecondOrderType* so_previous_row = second_order_integral_image_.data();
362 std::fill_n(so_previous_row, width_ + 1, 0);
364 for (
unsigned rowIdx = 0; rowIdx < height_; ++rowIdx, data += row_stride,
365 previous_row = current_row, current_row += (width_ + 1),
366 count_previous_row = count_current_row, count_current_row += (width_ + 1),
367 so_previous_row = so_current_row, so_current_row += (width_ + 1))
369 current_row [0] = 0.0;
370 so_current_row [0] = 0.0;
371 count_current_row [0] = 0;
372 for (
unsigned colIdx = 0, valIdx = 0; colIdx < width_; ++colIdx, valIdx += element_stride)
374 current_row [colIdx + 1] = previous_row [colIdx + 1] + current_row [colIdx] - previous_row [colIdx];
375 so_current_row [colIdx + 1] = so_previous_row [colIdx + 1] + so_current_row [colIdx] - so_previous_row [colIdx];
376 count_current_row [colIdx + 1] = count_previous_row [colIdx + 1] + count_current_row [colIdx] - count_previous_row [colIdx];
377 if (std::isfinite (data[valIdx]))
379 current_row [colIdx + 1] += data[valIdx];
380 so_current_row [colIdx + 1] += data[valIdx] * data[valIdx];
381 ++(count_current_row [colIdx + 1]);
Determines an integral image representation for a given organized data array.
void setSecondOrderComputation(bool compute_second_order_integral_images)
sets the computation for second order integral images on or off.
Eigen::Matrix< typename IntegralImageTypeTraits< DataType >::IntegralType, Dimension, 1 > ElementType
ElementType getFirstOrderSumSE(unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const
Compute the first order sum within a given rectangle.
ElementType getFirstOrderSum(unsigned start_x, unsigned start_y, unsigned width, unsigned height) const
Compute the first order sum within a given rectangle.
unsigned getFiniteElementsCount(unsigned start_x, unsigned start_y, unsigned width, unsigned height) const
Compute the number of finite elements within a given rectangle.
Eigen::Matrix< typename IntegralImageTypeTraits< DataType >::IntegralType, second_order_size, 1 > SecondOrderType
void setInput(const DataType *data, unsigned width, unsigned height, unsigned element_stride, unsigned row_stride)
Set the input data to compute the integral image for.
SecondOrderType getSecondOrderSumSE(unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const
Compute the second order sum within a given rectangle.
SecondOrderType getSecondOrderSum(unsigned start_x, unsigned start_y, unsigned width, unsigned height) const
Compute the second order sum within a given rectangle.
unsigned getFiniteElementsCountSE(unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const
Compute the number of finite elements within a given rectangle.