Point Cloud Library (PCL)  1.7.0
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 #ifndef PCL_INTEGRAL_IMAGE2D_H_
41 #define PCL_INTEGRAL_IMAGE2D_H_
42 
43 #include <vector>
44 
45 namespace pcl
46 {
47  template <typename DataType>
49  {
50  typedef DataType Type;
51  typedef DataType IntegralType;
52  };
53 
54  template <>
56  {
57  typedef float Type;
58  typedef double IntegralType;
59  };
60 
61  template <>
63  {
64  typedef char Type;
65  typedef int IntegralType;
66  };
67 
68  template <>
70  {
71  typedef short Type;
72  typedef long IntegralType;
73  };
74 
75  template <>
76  struct IntegralImageTypeTraits<unsigned short>
77  {
78  typedef unsigned short Type;
79  typedef unsigned long IntegralType;
80  };
81 
82  template <>
83  struct IntegralImageTypeTraits<unsigned char>
84  {
85  typedef unsigned char Type;
86  typedef unsigned int IntegralType;
87  };
88 
89  template <>
91  {
92  typedef int Type;
93  typedef long IntegralType;
94  };
95 
96  template <>
97  struct IntegralImageTypeTraits<unsigned int>
98  {
99  typedef unsigned int Type;
100  typedef unsigned long IntegralType;
101  };
102 
103  /** \brief Determines an integral image representation for a given organized data array
104  * \author Suat Gedikli
105  */
106  template <class DataType, unsigned Dimension>
108  {
109  public:
110  static const unsigned second_order_size = (Dimension * (Dimension + 1)) >> 1;
111  typedef Eigen::Matrix<typename IntegralImageTypeTraits<DataType>::IntegralType, Dimension, 1> ElementType;
112  typedef Eigen::Matrix<typename IntegralImageTypeTraits<DataType>::IntegralType, second_order_size, 1> SecondOrderType;
113 
114  /** \brief Constructor for an Integral Image
115  * \param[in] compute_second_order_integral_images set to true if we want to compute a second order image
116  */
117  IntegralImage2D (bool compute_second_order_integral_images) :
118  first_order_integral_image_ (),
119  second_order_integral_image_ (),
120  finite_values_integral_image_ (),
121  width_ (1),
122  height_ (1),
123  compute_second_order_integral_images_ (compute_second_order_integral_images)
124  {
125  }
126 
127  /** \brief Destructor */
128  virtual
130 
131  /** \brief sets the computation for second order integral images on or off.
132  * \param compute_second_order_integral_images
133  */
134  void
135  setSecondOrderComputation (bool compute_second_order_integral_images);
136 
137  /** \brief Set the input data to compute the integral image for
138  * \param[in] data the input data
139  * \param[in] width the width of the data
140  * \param[in] height the height of the data
141  * \param[in] element_stride the element stride of the data
142  * \param[in] row_stride the row stride of the data
143  */
144  void
145  setInput (const DataType * data,
146  unsigned width, unsigned height, unsigned element_stride, unsigned row_stride);
147 
148  /** \brief Compute the first order sum within a given rectangle
149  * \param[in] start_x x position of rectangle
150  * \param[in] start_y y position of rectangle
151  * \param[in] width width of rectangle
152  * \param[in] height height of rectangle
153  */
154  inline ElementType
155  getFirstOrderSum (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
156 
157  /** \brief Compute the first order sum within a given rectangle
158  * \param[in] start_x x position of the start of the rectangle
159  * \param[in] start_y x position of the start of the rectangle
160  * \param[in] end_x x position of the end of the rectangle
161  * \param[in] end_y x position of the end of the rectangle
162  */
163  inline ElementType
164  getFirstOrderSumSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
165 
166  /** \brief Compute the second order sum within a given rectangle
167  * \param[in] start_x x position of rectangle
168  * \param[in] start_y y position of rectangle
169  * \param[in] width width of rectangle
170  * \param[in] height height of rectangle
171  */
172  inline SecondOrderType
173  getSecondOrderSum (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
174 
175  /** \brief Compute the second order sum within a given rectangle
176  * \param[in] start_x x position of the start of the rectangle
177  * \param[in] start_y x position of the start of the rectangle
178  * \param[in] end_x x position of the end of the rectangle
179  * \param[in] end_y x position of the end of the rectangle
180  */
181  inline SecondOrderType
182  getSecondOrderSumSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
183 
184  /** \brief Compute the number of finite elements within a given rectangle
185  * \param[in] start_x x position of rectangle
186  * \param[in] start_y y position of rectangle
187  * \param[in] width width of rectangle
188  * \param[in] height height of rectangle
189  */
190  inline unsigned
191  getFiniteElementsCount (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
192 
193  /** \brief Compute the number of finite elements within a given rectangle
194  * \param[in] start_x x position of the start of the rectangle
195  * \param[in] start_y x position of the start of the rectangle
196  * \param[in] end_x x position of the end of the rectangle
197  * \param[in] end_y x position of the end of the rectangle
198  */
199  inline unsigned
200  getFiniteElementsCountSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
201 
202  private:
203  typedef Eigen::Matrix<typename IntegralImageTypeTraits<DataType>::Type, Dimension, 1> InputType;
204 
205  /** \brief Compute the actual integral image data
206  * \param[in] data the input data
207  * \param[in] element_stride the element stride of the data
208  * \param[in] row_stride the row stride of the data
209  */
210  void
211  computeIntegralImages (const DataType * data, unsigned row_stride, unsigned element_stride);
212 
213  std::vector<ElementType, Eigen::aligned_allocator<ElementType> > first_order_integral_image_;
214  std::vector<SecondOrderType, Eigen::aligned_allocator<SecondOrderType> > second_order_integral_image_;
215  std::vector<unsigned> finite_values_integral_image_;
216 
217  /** \brief The width of the 2d input data array */
218  unsigned width_;
219  /** \brief The height of the 2d input data array */
220  unsigned height_;
221 
222  /** \brief Indicates whether second order integral images are available **/
223  bool compute_second_order_integral_images_;
224  };
225 
226  /**
227  * \brief partial template specialization for integral images with just one channel.
228  */
229  template <class DataType>
230  class IntegralImage2D <DataType, 1>
231  {
232  public:
233  static const unsigned second_order_size = 1;
236 
237  /** \brief Constructor for an Integral Image
238  * \param[in] compute_second_order_integral_images set to true if we want to compute a second order image
239  */
240  IntegralImage2D (bool compute_second_order_integral_images) :
241  first_order_integral_image_ (),
242  second_order_integral_image_ (),
243  finite_values_integral_image_ (),
244  width_ (1), height_ (1),
245  compute_second_order_integral_images_ (compute_second_order_integral_images)
246  {
247  }
248 
249  /** \brief Destructor */
250  virtual
252 
253  /** \brief Set the input data to compute the integral image for
254  * \param[in] data the input data
255  * \param[in] width the width of the data
256  * \param[in] height the height of the data
257  * \param[in] element_stride the element stride of the data
258  * \param[in] row_stride the row stride of the data
259  */
260  void
261  setInput (const DataType * data,
262  unsigned width, unsigned height, unsigned element_stride, unsigned row_stride);
263 
264  /** \brief Compute the first order sum within a given rectangle
265  * \param[in] start_x x position of rectangle
266  * \param[in] start_y y position of rectangle
267  * \param[in] width width of rectangle
268  * \param[in] height height of rectangle
269  */
270  inline ElementType
271  getFirstOrderSum (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
272 
273  /** \brief Compute the first order sum within a given rectangle
274  * \param[in] start_x x position of the start of the rectangle
275  * \param[in] start_y x position of the start of the rectangle
276  * \param[in] end_x x position of the end of the rectangle
277  * \param[in] end_y x position of the end of the rectangle
278  */
279  inline ElementType
280  getFirstOrderSumSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
281 
282  /** \brief Compute the second order sum within a given rectangle
283  * \param[in] start_x x position of rectangle
284  * \param[in] start_y y position of rectangle
285  * \param[in] width width of rectangle
286  * \param[in] height height of rectangle
287  */
288  inline SecondOrderType
289  getSecondOrderSum (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
290 
291  /** \brief Compute the second order sum within a given rectangle
292  * \param[in] start_x x position of the start of the rectangle
293  * \param[in] start_y x position of the start of the rectangle
294  * \param[in] end_x x position of the end of the rectangle
295  * \param[in] end_y x position of the end of the rectangle
296  */
297  inline SecondOrderType
298  getSecondOrderSumSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
299 
300  /** \brief Compute the number of finite elements within a given rectangle
301  * \param[in] start_x x position of rectangle
302  * \param[in] start_y y position of rectangle
303  * \param[in] width width of rectangle
304  * \param[in] height height of rectangle
305  */
306  inline unsigned
307  getFiniteElementsCount (unsigned start_x, unsigned start_y, unsigned width, unsigned height) const;
308 
309  /** \brief Compute the number of finite elements within a given rectangle
310  * \param[in] start_x x position of the start of the rectangle
311  * \param[in] start_y x position of the start of the rectangle
312  * \param[in] end_x x position of the end of the rectangle
313  * \param[in] end_y x position of the end of the rectangle
314  */
315  inline unsigned
316  getFiniteElementsCountSE (unsigned start_x, unsigned start_y, unsigned end_x, unsigned end_y) const;
317 
318  private:
319  // typedef typename IntegralImageTypeTraits<DataType>::Type InputType;
320 
321  /** \brief Compute the actual integral image data
322  * \param[in] data the input data
323  * \param[in] element_stride the element stride of the data
324  * \param[in] row_stride the row stride of the data
325  */
326  void
327  computeIntegralImages (const DataType * data, unsigned row_stride, unsigned element_stride);
328 
329  std::vector<ElementType, Eigen::aligned_allocator<ElementType> > first_order_integral_image_;
330  std::vector<SecondOrderType, Eigen::aligned_allocator<SecondOrderType> > second_order_integral_image_;
331  std::vector<unsigned> finite_values_integral_image_;
332 
333  /** \brief The width of the 2d input data array */
334  unsigned width_;
335  /** \brief The height of the 2d input data array */
336  unsigned height_;
337 
338  /** \brief Indicates whether second order integral images are available **/
339  bool compute_second_order_integral_images_;
340  };
341  }
342 
343 #include <pcl/features/impl/integral_image2D.hpp>
344 
345 #endif // PCL_INTEGRAL_IMAGE2D_H_
346