Point Cloud Library (PCL)  1.14.0-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 #include <pcl/search/organized.h> // for OrganizedNeighbor
45 #include <pcl/search/kdtree.h> // for KdTree
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointT> double
50  const Indices &indices,
51  const std::vector<float> &distances)
52 {
53  double BF = 0, W = 0;
54 
55  // For each neighbor
56  for (std::size_t n_id = 0; n_id < indices.size (); ++n_id)
57  {
58  int id = indices[n_id];
59  // Compute the difference in intensity
60  double intensity_dist = std::abs ((*input_)[pid].intensity - (*input_)[id].intensity);
61 
62  // Compute the Gaussian intensity weights both in Euclidean and in intensity space
63  double dist = std::sqrt (distances[n_id]);
64  double weight = kernel (dist, sigma_s_) * kernel (intensity_dist, sigma_r_);
65 
66  // Calculate the bilateral filter response
67  BF += weight * (*input_)[id].intensity;
68  W += weight;
69  }
70  return (BF / W);
71 }
72 
73 //////////////////////////////////////////////////////////////////////////////////////////////
74 template <typename PointT> void
76 {
77  // Check if sigma_s has been given by the user
78  if (sigma_s_ == 0)
79  {
80  PCL_ERROR ("[pcl::BilateralFilter::applyFilter] Need a sigma_s value given before continuing.\n");
81  return;
82  }
83  // In case a search method has not been given, initialize it using some defaults
84  if (!tree_)
85  {
86  // For organized datasets, use an OrganizedNeighbor
87  if (input_->isOrganized ())
88  tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
89  // For unorganized data, use a FLANN kdtree
90  else
91  tree_.reset (new pcl::search::KdTree<PointT> (false));
92  }
93  tree_->setInputCloud (input_);
94 
95  Indices k_indices;
96  std::vector<float> k_distances;
97 
98  // Copy the input data into the output
99  output = *input_;
100 
101  // For all the indices given (equal to the entire cloud if none given)
102  for (const auto& idx : (*indices_))
103  {
104  // Perform a radius search to find the nearest neighbors
105  tree_->radiusSearch (idx, sigma_s_ * 2, k_indices, k_distances);
106 
107  // Overwrite the intensity value with the computed average
108  output[idx].intensity = static_cast<float> (computePointWeight (idx, k_indices, k_distances));
109  }
110 }
111 
112 #define PCL_INSTANTIATE_BilateralFilter(T) template class PCL_EXPORTS pcl::BilateralFilter<T>;
113 
114 #endif // PCL_FILTERS_BILATERAL_IMPL_H_
115 
void applyFilter(PointCloud &output) override
Filter the input data and store the results into output.
Definition: bilateral.hpp:75
double computePointWeight(const int pid, const Indices &indices, const std::vector< float > &distances)
Compute the intensity average for a single point.
Definition: bilateral.hpp:49
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:62
OrganizedNeighbor is a class for optimized nearest neighbor search in organized projectable point clo...
Definition: organized.h:65
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133