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 */ 00037 00038 #ifndef PCL_FILTERS_BILATERAL_IMPL_H_ 00039 #define PCL_FILTERS_BILATERAL_IMPL_H_ 00040 00041 #include <pcl/filters/bilateral.h> 00042 00044 template <typename PointT> double 00045 pcl::BilateralFilter<PointT>::computePointWeight (const int pid, 00046 const std::vector<int> &indices, 00047 const std::vector<float> &distances) 00048 { 00049 double BF = 0, W = 0; 00050 00051 // For each neighbor 00052 for (size_t n_id = 0; n_id < indices.size (); ++n_id) 00053 { 00054 int id = indices[n_id]; 00055 // Compute the difference in intensity 00056 double intensity_dist = fabs (input_->points[pid].intensity - input_->points[id].intensity); 00057 00058 // Compute the Gaussian intensity weights both in Euclidean and in intensity space 00059 double dist = std::sqrt (distances[n_id]); 00060 double weight = kernel (dist, sigma_s_) * kernel (intensity_dist, sigma_r_); 00061 00062 // Calculate the bilateral filter response 00063 BF += weight * input_->points[id].intensity; 00064 W += weight; 00065 } 00066 return (BF / W); 00067 } 00068 00070 template <typename PointT> void 00071 pcl::BilateralFilter<PointT>::applyFilter (PointCloud &output) 00072 { 00073 // Check if sigma_s has been given by the user 00074 if (sigma_s_ == 0) 00075 { 00076 PCL_ERROR ("[pcl::BilateralFilter::applyFilter] Need a sigma_s value given before continuing.\n"); 00077 return; 00078 } 00079 // In case a search method has not been given, initialize it using some defaults 00080 if (!tree_) 00081 { 00082 // For organized datasets, use an OrganizedDataIndex 00083 if (input_->isOrganized ()) 00084 tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ()); 00085 // For unorganized data, use a FLANN kdtree 00086 else 00087 tree_.reset (new pcl::search::KdTree<PointT> (false)); 00088 } 00089 tree_->setInputCloud (input_); 00090 00091 std::vector<int> k_indices; 00092 std::vector<float> k_distances; 00093 00094 // Copy the input data into the output 00095 output = *input_; 00096 00097 // For all the indices given (equal to the entire cloud if none given) 00098 for (size_t i = 0; i < indices_->size (); ++i) 00099 { 00100 // Perform a radius search to find the nearest neighbors 00101 tree_->radiusSearch ((*indices_)[i], sigma_s_ * 2, k_indices, k_distances); 00102 00103 // Overwrite the intensity value with the computed average 00104 output.points[(*indices_)[i]].intensity = computePointWeight ((*indices_)[i], k_indices, k_distances); 00105 } 00106 } 00107 00108 #define PCL_INSTANTIATE_BilateralFilter(T) template class PCL_EXPORTS pcl::BilateralFilter<T>; 00109 00110 #endif // PCL_FILTERS_BILATERAL_H_ 00111