Point Cloud Library (PCL)  1.14.0-dev
fast_bilateral.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  * Copyright (c) 2004, Sylvain Paris and Francois Sillion
7 
8  * All rights reserved.
9 
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #pragma once
42 
43 #include <pcl/filters/filter.h>
44 
45 namespace pcl
46 {
47  /** \brief Implementation of a fast bilateral filter for smoothing depth information in organized point clouds
48  * Based on the following paper:
49  * * Sylvain Paris and Fredo Durand
50  * "A Fast Approximation of the Bilateral Filter using a Signal Processing Approach"
51  * European Conference on Computer Vision (ECCV'06)
52  *
53  * More details on the webpage: http://people.csail.mit.edu/sparis/bf/
54  */
55  template<typename PointT>
56  class FastBilateralFilter : public Filter<PointT>
57  {
58  protected:
61 
62  public:
63 
64  using Ptr = shared_ptr<FastBilateralFilter<PointT> >;
65  using ConstPtr = shared_ptr<const FastBilateralFilter<PointT> >;
66 
67  /** \brief Empty constructor. */
68  FastBilateralFilter () = default;
69 
70  /** \brief Empty destructor */
71  ~FastBilateralFilter () override = default;
72 
73  /** \brief Set the standard deviation of the Gaussian used by the bilateral filter for
74  * the spatial neighborhood/window.
75  * \param[in] sigma_s the size of the Gaussian bilateral filter window to use
76  */
77  inline void
78  setSigmaS (float sigma_s)
79  { sigma_s_ = sigma_s; }
80 
81  /** \brief Get the size of the Gaussian bilateral filter window as set by the user. */
82  inline float
83  getSigmaS () const
84  { return sigma_s_; }
85 
86 
87  /** \brief Set the standard deviation of the Gaussian used to control how much an adjacent
88  * pixel is downweighted because of the intensity difference (depth in our case).
89  * \param[in] sigma_r the standard deviation of the Gaussian for the intensity difference
90  */
91  inline void
92  setSigmaR (float sigma_r)
93  { sigma_r_ = sigma_r; }
94 
95  /** \brief Get the standard deviation of the Gaussian for the intensity difference */
96  inline float
97  getSigmaR () const
98  { return sigma_r_; }
99 
100  /** \brief Filter the input data and store the results into output.
101  * \param[out] output the resultant point cloud
102  */
103  void
104  applyFilter (PointCloud &output) override;
105 
106  protected:
107  float sigma_s_{15.0f};
108  float sigma_r_{0.05f};
109  bool early_division_{false};
110 
111  class Array3D
112  {
113  public:
114  Array3D (const std::size_t width, const std::size_t height, const std::size_t depth)
115  : v_({(width*height*depth), Eigen::Vector2f (0.0f, 0.0f), Eigen::aligned_allocator<Eigen::Vector2f>()})
116  {
117  x_dim_ = width;
118  y_dim_ = height;
119  z_dim_ = depth;
120  }
121 
122  inline Eigen::Vector2f&
123  operator () (const std::size_t x, const std::size_t y, const std::size_t z)
124  { return v_[(x * y_dim_ + y) * z_dim_ + z]; }
125 
126  inline const Eigen::Vector2f&
127  operator () (const std::size_t x, const std::size_t y, const std::size_t z) const
128  { return v_[(x * y_dim_ + y) * z_dim_ + z]; }
129 
130  inline void
131  resize (const std::size_t width, const std::size_t height, const std::size_t depth)
132  {
133  x_dim_ = width;
134  y_dim_ = height;
135  z_dim_ = depth;
136  v_.resize (x_dim_ * y_dim_ * z_dim_);
137  }
138 
139  Eigen::Vector2f
140  trilinear_interpolation (const float x,
141  const float y,
142  const float z);
143 
144  static inline std::size_t
145  clamp (const std::size_t min_value,
146  const std::size_t max_value,
147  const std::size_t x);
148 
149  inline std::size_t
150  x_size () const
151  { return x_dim_; }
152 
153  inline std::size_t
154  y_size () const
155  { return y_dim_; }
156 
157  inline std::size_t
158  z_size () const
159  { return z_dim_; }
160 
161  inline std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> >::iterator
162  begin ()
163  { return v_.begin (); }
164 
165  inline std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> >::iterator
166  end ()
167  { return v_.end (); }
168 
169  inline std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> >::const_iterator
170  begin () const
171  { return v_.begin (); }
172 
173  inline std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> >::const_iterator
174  end () const
175  { return v_.end (); }
176 
177  private:
178  std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > v_;
179  std::size_t x_dim_, y_dim_, z_dim_;
180  };
181 
182 
183  };
184 }
185 
186 #ifdef PCL_NO_PRECOMPILE
187 #include <pcl/filters/impl/fast_bilateral.hpp>
188 #else
189 #define PCL_INSTANTIATE_FastBilateralFilter(T) template class PCL_EXPORTS pcl::FastBilateralFilter<T>;
190 #endif
std::vector< Eigen::Vector2f, Eigen::aligned_allocator< Eigen::Vector2f > >::const_iterator end() const
std::vector< Eigen::Vector2f, Eigen::aligned_allocator< Eigen::Vector2f > >::const_iterator begin() const
Array3D(const std::size_t width, const std::size_t height, const std::size_t depth)
static std::size_t clamp(const std::size_t min_value, const std::size_t max_value, const std::size_t x)
std::vector< Eigen::Vector2f, Eigen::aligned_allocator< Eigen::Vector2f > >::iterator end()
Eigen::Vector2f trilinear_interpolation(const float x, const float y, const float z)
Eigen::Vector2f & operator()(const std::size_t x, const std::size_t y, const std::size_t z)
std::vector< Eigen::Vector2f, Eigen::aligned_allocator< Eigen::Vector2f > >::iterator begin()
void resize(const std::size_t width, const std::size_t height, const std::size_t depth)
Implementation of a fast bilateral filter for smoothing depth information in organized point clouds B...
void applyFilter(PointCloud &output) override
Filter the input data and store the results into output.
shared_ptr< FastBilateralFilter< PointT > > Ptr
void setSigmaS(float sigma_s)
Set the standard deviation of the Gaussian used by the bilateral filter for the spatial neighborhood/...
~FastBilateralFilter() override=default
Empty destructor.
float getSigmaR() const
Get the standard deviation of the Gaussian for the intensity difference.
void setSigmaR(float sigma_r)
Set the standard deviation of the Gaussian used to control how much an adjacent pixel is downweighted...
float getSigmaS() const
Get the size of the Gaussian bilateral filter window as set by the user.
shared_ptr< const FastBilateralFilter< PointT > > ConstPtr
FastBilateralFilter()=default
Empty constructor.
Filter represents the base filter class.
Definition: filter.h:81
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173