Point Cloud Library (PCL)  1.9.1-dev
fast_bilateral.hpp
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 #ifndef PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_
41 #define PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_
42 
43 #include <pcl/common/io.h>
44 
45 //////////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointT> void
48 {
49  if (!input_->isOrganized ())
50  {
51  PCL_ERROR ("[pcl::FastBilateralFilter] Input cloud needs to be organized.\n");
52  return;
53  }
54 
55  copyPointCloud (*input_, output);
56  float base_max = -std::numeric_limits<float>::max (),
57  base_min = std::numeric_limits<float>::max ();
58  bool found_finite = false;
59  for (std::size_t x = 0; x < output.width; ++x)
60  {
61  for (std::size_t y = 0; y < output.height; ++y)
62  {
63  if (std::isfinite (output (x, y).z))
64  {
65  if (base_max < output (x, y).z)
66  base_max = output (x, y).z;
67  if (base_min > output (x, y).z)
68  base_min = output (x, y).z;
69  found_finite = true;
70  }
71  }
72  }
73  if (!found_finite)
74  {
75  PCL_WARN ("[pcl::FastBilateralFilter] Given an empty cloud. Doing nothing.\n");
76  return;
77  }
78 
79  for (std::size_t x = 0; x < output.width; ++x)
80  for (std::size_t y = 0; y < output.height; ++y)
81  if (!std::isfinite (output (x, y).z))
82  output (x, y).z = base_max;
83 
84  const float base_delta = base_max - base_min;
85 
86  const std::size_t padding_xy = 2;
87  const std::size_t padding_z = 2;
88 
89  const std::size_t small_width = static_cast<std::size_t> (static_cast<float> (input_->width - 1) / sigma_s_) + 1 + 2 * padding_xy;
90  const std::size_t small_height = static_cast<std::size_t> (static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;
91  const std::size_t small_depth = static_cast<std::size_t> (base_delta / sigma_r_) + 1 + 2 * padding_z;
92 
93 
94  Array3D data (small_width, small_height, small_depth);
95  for (std::size_t x = 0; x < input_->width; ++x)
96  {
97  const std::size_t small_x = static_cast<std::size_t> (static_cast<float> (x) / sigma_s_ + 0.5f) + padding_xy;
98  for (std::size_t y = 0; y < input_->height; ++y)
99  {
100  const float z = output (x,y).z - base_min;
101 
102  const std::size_t small_y = static_cast<std::size_t> (static_cast<float> (y) / sigma_s_ + 0.5f) + padding_xy;
103  const std::size_t small_z = static_cast<std::size_t> (static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;
104 
105  Eigen::Vector2f& d = data (small_x, small_y, small_z);
106  d[0] += output (x,y).z;
107  d[1] += 1.0f;
108  }
109  }
110 
111 
112  std::vector<long int> offset (3);
113  offset[0] = &(data (1,0,0)) - &(data (0,0,0));
114  offset[1] = &(data (0,1,0)) - &(data (0,0,0));
115  offset[2] = &(data (0,0,1)) - &(data (0,0,0));
116 
117  Array3D buffer (small_width, small_height, small_depth);
118 
119  for (std::size_t dim = 0; dim < 3; ++dim)
120  {
121  const long int off = offset[dim];
122  for (std::size_t n_iter = 0; n_iter < 2; ++n_iter)
123  {
124  std::swap (buffer, data);
125  for(std::size_t x = 1; x < small_width - 1; ++x)
126  for(std::size_t y = 1; y < small_height - 1; ++y)
127  {
128  Eigen::Vector2f* d_ptr = &(data (x,y,1));
129  Eigen::Vector2f* b_ptr = &(buffer (x,y,1));
130 
131  for(std::size_t z = 1; z < small_depth - 1; ++z, ++d_ptr, ++b_ptr)
132  *d_ptr = (*(b_ptr - off) + *(b_ptr + off) + 2.0 * (*b_ptr)) / 4.0;
133  }
134  }
135  }
136 
137  if (early_division_)
138  {
139  for (std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> >::iterator d = data.begin (); d != data.end (); ++d)
140  *d /= ((*d)[0] != 0) ? (*d)[1] : 1;
141 
142  for (std::size_t x = 0; x < input_->width; x++)
143  for (std::size_t y = 0; y < input_->height; y++)
144  {
145  const float z = output (x,y).z - base_min;
146  const Eigen::Vector2f D = data.trilinear_interpolation (static_cast<float> (x) / sigma_s_ + padding_xy,
147  static_cast<float> (y) / sigma_s_ + padding_xy,
148  z / sigma_r_ + padding_z);
149  output(x,y).z = D[0];
150  }
151  }
152  else
153  {
154  for (std::size_t x = 0; x < input_->width; ++x)
155  for (std::size_t y = 0; y < input_->height; ++y)
156  {
157  const float z = output (x,y).z - base_min;
158  const Eigen::Vector2f D = data.trilinear_interpolation (static_cast<float> (x) / sigma_s_ + padding_xy,
159  static_cast<float> (y) / sigma_s_ + padding_xy,
160  z / sigma_r_ + padding_z);
161  output (x,y).z = D[0] / D[1];
162  }
163  }
164 }
165 
166 
167 
168 //////////////////////////////////////////////////////////////////////////////////////////////
169 template <typename PointT> std::size_t
171  const std::size_t max_value,
172  const std::size_t x)
173 {
174  if (x >= min_value && x <= max_value)
175  {
176  return x;
177  }
178  if (x < min_value)
179  {
180  return (min_value);
181  }
182  return (max_value);
183 }
184 
185 //////////////////////////////////////////////////////////////////////////////////////////////
186 template <typename PointT> Eigen::Vector2f
188  const float y,
189  const float z)
190 {
191  const std::size_t x_index = clamp (0, x_dim_ - 1, static_cast<std::size_t> (x));
192  const std::size_t xx_index = clamp (0, x_dim_ - 1, x_index + 1);
193 
194  const std::size_t y_index = clamp (0, y_dim_ - 1, static_cast<std::size_t> (y));
195  const std::size_t yy_index = clamp (0, y_dim_ - 1, y_index + 1);
196 
197  const std::size_t z_index = clamp (0, z_dim_ - 1, static_cast<std::size_t> (z));
198  const std::size_t zz_index = clamp (0, z_dim_ - 1, z_index + 1);
199 
200  const float x_alpha = x - static_cast<float> (x_index);
201  const float y_alpha = y - static_cast<float> (y_index);
202  const float z_alpha = z - static_cast<float> (z_index);
203 
204  return
205  (1.0f-x_alpha) * (1.0f-y_alpha) * (1.0f-z_alpha) * (*this)(x_index, y_index, z_index) +
206  x_alpha * (1.0f-y_alpha) * (1.0f-z_alpha) * (*this)(xx_index, y_index, z_index) +
207  (1.0f-x_alpha) * y_alpha * (1.0f-z_alpha) * (*this)(x_index, yy_index, z_index) +
208  x_alpha * y_alpha * (1.0f-z_alpha) * (*this)(xx_index, yy_index, z_index) +
209  (1.0f-x_alpha) * (1.0f-y_alpha) * z_alpha * (*this)(x_index, y_index, zz_index) +
210  x_alpha * (1.0f-y_alpha) * z_alpha * (*this)(xx_index, y_index, zz_index) +
211  (1.0f-x_alpha) * y_alpha * z_alpha * (*this)(x_index, yy_index, zz_index) +
212  x_alpha * y_alpha * z_alpha * (*this)(xx_index, yy_index, zz_index);
213 }
214 
215 #endif /* PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_ */
Eigen::Vector2f trilinear_interpolation(const float x, const float y, const float z)
std::vector< Eigen::Vector2f, Eigen::aligned_allocator< Eigen::Vector2f > >::iterator end()
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:397
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:399
PointCloud represents the base class in PCL for storing collections of 3D points. ...
std::vector< Eigen::Vector2f, Eigen::aligned_allocator< Eigen::Vector2f > >::iterator begin()
void applyFilter(PointCloud &output) override
Filter the input data and store the results into output.
static std::size_t clamp(const std::size_t min_value, const std::size_t max_value, const std::size_t x)