Point Cloud Library (PCL)  1.3.1
integral_image2D.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: integral_image2D.hpp 3070 2011-11-01 21:12:47Z gedikli $
00037  */
00038 
00039 #ifndef PCL_INTEGRAL_IMAGE2D_IMPL_H_
00040 #define PCL_INTEGRAL_IMAGE2D_IMPL_H_
00041 
00042 #include <cstddef>
00043 
00045 template <typename DataType, unsigned Dimension> void
00046 pcl::IntegralImage2Dim<DataType, Dimension>::setInput (const DataType * data, unsigned width,unsigned height, unsigned element_stride, unsigned row_stride)
00047 {
00048   if ((width + 1) * (height + 1) > first_order_integral_image_.size () )
00049   {
00050     width_  = width;
00051     height_ = height;
00052     first_order_integral_image_.resize ( (width_ + 1) * (height_ + 1) );
00053     if (compute_second_order_integral_images_)
00054       second_order_integral_image_.resize ( (width_ + 1) * (height_ + 1) );
00055   }
00056   computeIntegralImages (data, row_stride, element_stride);
00057 }
00058 
00060 template <typename DataType, unsigned Dimension> typename pcl::IntegralImage2Dim<DataType, Dimension>::ElementType
00061 pcl::IntegralImage2Dim<DataType, Dimension>::getFirstOrderSum (
00062     unsigned start_x, unsigned start_y, unsigned width, unsigned height) const
00063 {
00064   const unsigned upper_left_idx      = start_y * (width_ + 1) + start_x;
00065   const unsigned upper_right_idx     = upper_left_idx + width;
00066   const unsigned lower_left_idx      = (start_y + height) * (width_ + 1) + start_x;
00067   const unsigned lower_right_idx     = lower_left_idx + width;
00068   
00069   return (first_order_integral_image_[lower_right_idx] + first_order_integral_image_[upper_left_idx]  -
00070           first_order_integral_image_[upper_right_idx] - first_order_integral_image_[lower_left_idx]  );
00071 }
00072 
00074 template <typename DataType, unsigned Dimension> typename pcl::IntegralImage2Dim<DataType, Dimension>::SecondOrderType
00075 pcl::IntegralImage2Dim<DataType, Dimension>::getSecondOrderSum (
00076     unsigned start_x, unsigned start_y, unsigned width, unsigned height) const
00077 {
00078   const unsigned upper_left_idx      = start_y * (width_ + 1) + start_x;
00079   const unsigned upper_right_idx     = upper_left_idx + width;
00080   const unsigned lower_left_idx      = (start_y + height) * (width_ + 1) + start_x;
00081   const unsigned lower_right_idx     = lower_left_idx + width;
00082   
00083   return (second_order_integral_image_[lower_right_idx] + second_order_integral_image_[upper_left_idx]  -
00084           second_order_integral_image_[upper_right_idx] - second_order_integral_image_[lower_left_idx]  );
00085 }
00086 
00088 template <typename DataType, unsigned Dimension> void
00089 pcl::IntegralImage2Dim<DataType, Dimension>::computeIntegralImages (
00090     const DataType *data, unsigned row_stride, unsigned element_stride)
00091 {
00092   ElementType* previous_row = &first_order_integral_image_[0];
00093   ElementType* current_row = previous_row + (width_ + 1);
00094   memset (previous_row, 0, sizeof (ElementType) * (width_ + 1));    
00095 
00096   if (!compute_second_order_integral_images_)
00097   {
00098     for (unsigned rowIdx = 0; rowIdx < height_; ++rowIdx, previous_row = current_row, current_row += (width_ + 1), data += row_stride)
00099     {
00100       current_row [0].setZero ();
00101       for (unsigned colIdx = 0, valIdx = 0; colIdx < width_; ++colIdx, valIdx += element_stride)
00102       {
00103         current_row [colIdx + 1] = previous_row [colIdx + 1] + current_row [colIdx] - previous_row [colIdx];
00104         const InputType* element = reinterpret_cast <const InputType*> (&data [valIdx]);
00105         if (pcl_isfinite (element->sum ()))
00106         {
00107           current_row [colIdx + 1] += element->template cast<typename IntegralImageTypeTraits<DataType>::IntegralType>();
00108         }
00109       }
00110     }
00111   }
00112   else
00113   {
00114     SecondOrderType* so_previous_row = &second_order_integral_image_[0];
00115     SecondOrderType* so_current_row  = so_previous_row + (width_ + 1);
00116     memset (so_previous_row, 0, sizeof (SecondOrderType) * (width_ + 1));    
00117     
00118     SecondOrderType so_element;
00119     for (unsigned rowIdx = 0; rowIdx < height_; ++rowIdx, 
00120          previous_row = current_row, current_row += (width_ + 1), 
00121          so_previous_row = so_current_row, so_current_row += (width_ + 1), data += row_stride)
00122     {
00123       current_row [0].setZero ();
00124       so_current_row [0].setZero ();
00125       for (unsigned colIdx = 0, valIdx = 0; colIdx < width_; ++colIdx, valIdx += element_stride)
00126       {
00127         current_row [colIdx + 1] = previous_row [colIdx + 1] + current_row [colIdx] - previous_row [colIdx];
00128         so_current_row [colIdx + 1] = so_previous_row [colIdx + 1] + so_current_row [colIdx] - so_previous_row [colIdx];
00129         
00130         const InputType* element = reinterpret_cast <const InputType*> (&data [valIdx]);
00131         if (pcl_isfinite (element->sum ()))
00132         {
00133           current_row [colIdx + 1] += element->template cast<typename IntegralImageTypeTraits<DataType>::IntegralType>();
00134           
00135           for (unsigned myIdx = 0, elIdx = 0; myIdx < Dimension; ++myIdx)
00136             for (unsigned mxIdx = myIdx; mxIdx < Dimension; ++mxIdx, ++elIdx)
00137               so_current_row [colIdx + 1][elIdx] += (*element)[myIdx] * (*element)[mxIdx];
00138         }
00139       }
00140     }
00141   }
00142 }
00143 
00144 #endif    // PCL_INTEGRAL_IMAGE2D_IMPL_H_
00145 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines