Point Cloud Library (PCL)  1.10.0-dev
integral_image2D.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: feature.h 2784 2011-10-15 22:05:38Z aichim $
38  */
39 
40 #pragma once
41 
42 #include <vector>
43 
44 #include <boost/shared_ptr.hpp>
45 
46 namespace pcl
47 {
48  template <typename DataType>
50  {
51  using Type = DataType;
52  using IntegralType = DataType;
53  };
54 
55  template <>
57  {
58  using Type = float;
59  using IntegralType = double;
60  };
61 
62  template <>
64  {
65  using Type = char;
66  using IntegralType = int;
67  };
68 
69  template <>
71  {
72  using Type = short;
73  using IntegralType = long;
74  };
75 
76  template <>
77  struct IntegralImageTypeTraits<unsigned short>
78  {
79  using Type = unsigned short;
80  using IntegralType = unsigned long;
81  };
82 
83  template <>
84  struct IntegralImageTypeTraits<unsigned char>
85  {
86  using Type = unsigned char;
87  using IntegralType = unsigned int;
88  };
89 
90  template <>
92  {
93  using Type = int;
94  using IntegralType = long;
95  };
96 
97  template <>
98  struct IntegralImageTypeTraits<unsigned int>
99  {
100  using Type = unsigned int;
101  using IntegralType = unsigned long;
102  };
103 
104  /** \brief Determines an integral image representation for a given organized data array
105  * \author Suat Gedikli
106  */
107  template <class DataType, unsigned Dimension>
109  {
110  public:
113  static const unsigned second_order_size = (Dimension * (Dimension + 1)) >> 1;
114  using ElementType = Eigen::Matrix<typename IntegralImageTypeTraits<DataType>::IntegralType, Dimension, 1>;
115  using SecondOrderType = Eigen::Matrix<typename IntegralImageTypeTraits<DataType>::IntegralType, second_order_size, 1>;
116 
117  /** \brief Constructor for an Integral Image
118  * \param[in] compute_second_order_integral_images set to true if we want to compute a second order image
119  */
120  IntegralImage2D (bool compute_second_order_integral_images) :
121  first_order_integral_image_ (),
122  second_order_integral_image_ (),
123  width_ (1),
124  height_ (1),
125  compute_second_order_integral_images_ (compute_second_order_integral_images)
126  {
127  }
128 
129  /** \brief Destructor */
130  virtual
132 
133  /** \brief sets the computation for second order integral images on or off.
134  * \param compute_second_order_integral_images
135  */
136  void
137  setSecondOrderComputation (bool compute_second_order_integral_images);
138 
139  /** \brief Set the input data to compute the integral image for
140  * \param[in] data the input data
141  * \param[in] width the width of the data
142  * \param[in] height the height of the data
143  * \param[in] element_stride the element stride of the data
144  * \param[in] row_stride the row stride of the data
145  */
146  void
147  setInput (const DataType * data,
148  unsigned width, unsigned height, unsigned element_stride, unsigned row_stride);
149 
150  /** \brief Compute the first order sum within a given rectangle
151  * \param[in] start_x x position of rectangle
152  * \param[in] start_y y position of rectangle
153  * \param[in] width width of rectangle
154  * \param[in] height height of rectangle
155  */
156  inline ElementType
157  getFirstOrderSum (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
158 
159  /** \brief Compute the first order sum within a given rectangle
160  * \param[in] start_x x position of the start of the rectangle
161  * \param[in] start_y x position of the start of the rectangle
162  * \param[in] end_x x position of the end of the rectangle
163  * \param[in] end_y x position of the end of the rectangle
164  */
165  inline ElementType
166  getFirstOrderSumSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
167 
168  /** \brief Compute the second order sum within a given rectangle
169  * \param[in] start_x x position of rectangle
170  * \param[in] start_y y position of rectangle
171  * \param[in] width width of rectangle
172  * \param[in] height height of rectangle
173  */
174  inline SecondOrderType
175  getSecondOrderSum (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
176 
177  /** \brief Compute the second order sum within a given rectangle
178  * \param[in] start_x x position of the start of the rectangle
179  * \param[in] start_y x position of the start of the rectangle
180  * \param[in] end_x x position of the end of the rectangle
181  * \param[in] end_y x position of the end of the rectangle
182  */
183  inline SecondOrderType
184  getSecondOrderSumSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
185 
186  /** \brief Compute the number of finite elements within a given rectangle
187  * \param[in] start_x x position of rectangle
188  * \param[in] start_y y position of rectangle
189  * \param[in] width width of rectangle
190  * \param[in] height height of rectangle
191  */
192  inline unsigned
193  getFiniteElementsCount (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
194 
195  /** \brief Compute the number of finite elements within a given rectangle
196  * \param[in] start_x x position of the start of the rectangle
197  * \param[in] start_y x position of the start of the rectangle
198  * \param[in] end_x x position of the end of the rectangle
199  * \param[in] end_y x position of the end of the rectangle
200  */
201  inline unsigned
202  getFiniteElementsCountSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
203 
204  private:
205  using InputType = Eigen::Matrix<typename IntegralImageTypeTraits<DataType>::Type, Dimension, 1>;
206 
207  /** \brief Compute the actual integral image data
208  * \param[in] data the input data
209  * \param[in] element_stride the element stride of the data
210  * \param[in] row_stride the row stride of the data
211  */
212  void
213  computeIntegralImages (const DataType * data, unsigned row_stride, unsigned element_stride);
214 
215  std::vector<ElementType, Eigen::aligned_allocator<ElementType> > first_order_integral_image_;
216  std::vector<SecondOrderType, Eigen::aligned_allocator<SecondOrderType> > second_order_integral_image_;
217  std::vector<unsigned> finite_values_integral_image_;
218 
219  /** \brief The width of the 2d input data array */
220  unsigned width_;
221  /** \brief The height of the 2d input data array */
222  unsigned height_;
223 
224  /** \brief Indicates whether second order integral images are available **/
225  bool compute_second_order_integral_images_;
226  };
227 
228  /**
229  * \brief partial template specialization for integral images with just one channel.
230  */
231  template <class DataType>
232  class IntegralImage2D <DataType, 1>
233  {
234  public:
237 
238  static const unsigned second_order_size = 1;
241 
242  /** \brief Constructor for an Integral Image
243  * \param[in] compute_second_order_integral_images set to true if we want to compute a second order image
244  */
245  IntegralImage2D (bool compute_second_order_integral_images) :
246  first_order_integral_image_ (),
247  second_order_integral_image_ (),
248 
249  width_ (1), height_ (1),
250  compute_second_order_integral_images_ (compute_second_order_integral_images)
251  {
252  }
253 
254  /** \brief Destructor */
255  virtual
257 
258  /** \brief Set the input data to compute the integral image for
259  * \param[in] data the input data
260  * \param[in] width the width of the data
261  * \param[in] height the height of the data
262  * \param[in] element_stride the element stride of the data
263  * \param[in] row_stride the row stride of the data
264  */
265  void
266  setInput (const DataType * data,
267  unsigned width, unsigned height, unsigned element_stride, unsigned row_stride);
268 
269  /** \brief Compute the first order sum within a given rectangle
270  * \param[in] start_x x position of rectangle
271  * \param[in] start_y y position of rectangle
272  * \param[in] width width of rectangle
273  * \param[in] height height of rectangle
274  */
275  inline ElementType
276  getFirstOrderSum (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
277 
278  /** \brief Compute the first order sum within a given rectangle
279  * \param[in] start_x x position of the start of the rectangle
280  * \param[in] start_y x position of the start of the rectangle
281  * \param[in] end_x x position of the end of the rectangle
282  * \param[in] end_y x position of the end of the rectangle
283  */
284  inline ElementType
285  getFirstOrderSumSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
286 
287  /** \brief Compute the second order sum within a given rectangle
288  * \param[in] start_x x position of rectangle
289  * \param[in] start_y y position of rectangle
290  * \param[in] width width of rectangle
291  * \param[in] height height of rectangle
292  */
293  inline SecondOrderType
294  getSecondOrderSum (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
295 
296  /** \brief Compute the second order sum within a given rectangle
297  * \param[in] start_x x position of the start of the rectangle
298  * \param[in] start_y x position of the start of the rectangle
299  * \param[in] end_x x position of the end of the rectangle
300  * \param[in] end_y x position of the end of the rectangle
301  */
302  inline SecondOrderType
303  getSecondOrderSumSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
304 
305  /** \brief Compute the number of finite elements within a given rectangle
306  * \param[in] start_x x position of rectangle
307  * \param[in] start_y y position of rectangle
308  * \param[in] width width of rectangle
309  * \param[in] height height of rectangle
310  */
311  inline unsigned
312  getFiniteElementsCount (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
313 
314  /** \brief Compute the number of finite elements within a given rectangle
315  * \param[in] start_x x position of the start of the rectangle
316  * \param[in] start_y x position of the start of the rectangle
317  * \param[in] end_x x position of the end of the rectangle
318  * \param[in] end_y x position of the end of the rectangle
319  */
320  inline unsigned
321  getFiniteElementsCountSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
322 
323  private:
324  // using InputType = typename IntegralImageTypeTraits<DataType>::Type;
325 
326  /** \brief Compute the actual integral image data
327  * \param[in] data the input data
328  * \param[in] element_stride the element stride of the data
329  * \param[in] row_stride the row stride of the data
330  */
331  void
332  computeIntegralImages (const DataType * data, unsigned row_stride, unsigned element_stride);
333 
334  std::vector<ElementType, Eigen::aligned_allocator<ElementType> > first_order_integral_image_;
335  std::vector<SecondOrderType, Eigen::aligned_allocator<SecondOrderType> > second_order_integral_image_;
336  std::vector<unsigned> finite_values_integral_image_;
337 
338  /** \brief The width of the 2d input data array */
339  unsigned width_;
340  /** \brief The height of the 2d input data array */
341  unsigned height_;
342 
343  /** \brief Indicates whether second order integral images are available **/
344  bool compute_second_order_integral_images_;
345  };
346  }
347 
348 #include <pcl/features/impl/integral_image2D.hpp>
typename IntegralImageTypeTraits< DataType >::IntegralType ElementType
virtual ~IntegralImage2D()
Destructor.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
virtual ~IntegralImage2D()
Destructor.
IntegralImage2D(bool compute_second_order_integral_images)
Constructor for an Integral Image.
IntegralImage2D(bool compute_second_order_integral_images)
Constructor for an Integral Image.
typename IntegralImageTypeTraits< DataType >::IntegralType SecondOrderType
Determines an integral image representation for a given organized data array.
shared_ptr< IntegralImage2D< float, Dimension > > Ptr
shared_ptr< IntegralImage2D< DataType, 1 > > Ptr
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
shared_ptr< const IntegralImage2D< float, Dimension > > ConstPtr
shared_ptr< const IntegralImage2D< DataType, 1 > > ConstPtr
Eigen::Matrix< typename IntegralImageTypeTraits< float >::IntegralType, second_order_size, 1 > SecondOrderType
Eigen::Matrix< typename IntegralImageTypeTraits< float >::IntegralType, Dimension, 1 > ElementType