Point Cloud Library (PCL)  1.9.1-dev
bilateral.hpp
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #ifndef PCL_FILTERS_BILATERAL_IMPL_H_
41 #define PCL_FILTERS_BILATERAL_IMPL_H_
42 
43 #include <pcl/filters/bilateral.h>
44 
45 //////////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointT> double
48  const std::vector<int> &indices,
49  const std::vector<float> &distances)
50 {
51  double BF = 0, W = 0;
52 
53  // For each neighbor
54  for (size_t n_id = 0; n_id < indices.size (); ++n_id)
55  {
56  int id = indices[n_id];
57  // Compute the difference in intensity
58  double intensity_dist = std::abs (input_->points[pid].intensity - input_->points[id].intensity);
59 
60  // Compute the Gaussian intensity weights both in Euclidean and in intensity space
61  double dist = std::sqrt (distances[n_id]);
62  double weight = kernel (dist, sigma_s_) * kernel (intensity_dist, sigma_r_);
63 
64  // Calculate the bilateral filter response
65  BF += weight * input_->points[id].intensity;
66  W += weight;
67  }
68  return (BF / W);
69 }
70 
71 //////////////////////////////////////////////////////////////////////////////////////////////
72 template <typename PointT> void
74 {
75  // Check if sigma_s has been given by the user
76  if (sigma_s_ == 0)
77  {
78  PCL_ERROR ("[pcl::BilateralFilter::applyFilter] Need a sigma_s value given before continuing.\n");
79  return;
80  }
81  // In case a search method has not been given, initialize it using some defaults
82  if (!tree_)
83  {
84  // For organized datasets, use an OrganizedNeighbor
85  if (input_->isOrganized ())
86  tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
87  // For unorganized data, use a FLANN kdtree
88  else
89  tree_.reset (new pcl::search::KdTree<PointT> (false));
90  }
91  tree_->setInputCloud (input_);
92 
93  std::vector<int> k_indices;
94  std::vector<float> k_distances;
95 
96  // Copy the input data into the output
97  output = *input_;
98 
99  // For all the indices given (equal to the entire cloud if none given)
100  for (size_t i = 0; i < indices_->size (); ++i)
101  {
102  // Perform a radius search to find the nearest neighbors
103  tree_->radiusSearch ((*indices_)[i], sigma_s_ * 2, k_indices, k_distances);
104 
105  // Overwrite the intensity value with the computed average
106  output.points[(*indices_)[i]].intensity = static_cast<float> (computePointWeight ((*indices_)[i], k_indices, k_distances));
107  }
108 }
109 
110 #define PCL_INSTANTIATE_BilateralFilter(T) template class PCL_EXPORTS pcl::BilateralFilter<T>;
111 
112 #endif // PCL_FILTERS_BILATERAL_IMPL_H_
113 
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:423
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void applyFilter(PointCloud &output) override
Filter the input data and store the results into output.
Definition: bilateral.hpp:73
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds...
Definition: organized.h:62
double computePointWeight(const int pid, const std::vector< int > &indices, const std::vector< float > &distances)
Compute the intensity average for a single point.
Definition: bilateral.hpp:47