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