41 #ifndef PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_
42 #define PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_
44 #include <pcl/common/io.h>
50 template <
typename Po
intT>
void
53 if (!input_->isOrganized ())
55 PCL_ERROR (
"[pcl::FastBilateralFilter] Input cloud needs to be organized.\n");
60 float base_max = -std::numeric_limits<float>::max (),
61 base_min = std::numeric_limits<float>::max ();
62 bool found_finite =
false;
63 for (
const auto& pt: output)
65 if (std::isfinite(pt.z))
67 base_max = std::max<float>(pt.z, base_max);
68 base_min = std::min<float>(pt.z, base_min);
74 PCL_WARN (
"[pcl::FastBilateralFilter] Given an empty cloud. Doing nothing.\n");
78 for (
auto& pt: output)
80 if (!std::isfinite(pt.z))
86 const float base_delta = base_max - base_min;
88 const std::size_t padding_xy = 2;
89 const std::size_t padding_z = 2;
91 const std::size_t small_width =
static_cast<std::size_t
> (
static_cast<float> (input_->width - 1) / sigma_s_) + 1 + 2 * padding_xy;
92 const std::size_t small_height =
static_cast<std::size_t
> (
static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;
93 const std::size_t small_depth =
static_cast<std::size_t
> (base_delta / sigma_r_) + 1 + 2 * padding_z;
96 Array3D data (small_width, small_height, small_depth);
97 for (std::size_t x = 0; x < input_->width; ++x)
99 const std::size_t small_x =
static_cast<std::size_t
> (
static_cast<float> (x) / sigma_s_ + 0.5f) + padding_xy;
100 for (std::size_t y = 0; y < input_->height; ++y)
102 const float z = output (x,y).z - base_min;
104 const std::size_t small_y =
static_cast<std::size_t
> (
static_cast<float> (y) / sigma_s_ + 0.5f) + padding_xy;
105 const std::size_t small_z =
static_cast<std::size_t
> (
static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;
107 Eigen::Vector2f& d = data (small_x, small_y, small_z);
108 d[0] += output (x,y).z;
114 std::vector<long int> offset (3);
115 offset[0] = &(data (1,0,0)) - &(data (0,0,0));
116 offset[1] = &(data (0,1,0)) - &(data (0,0,0));
117 offset[2] = &(data (0,0,1)) - &(data (0,0,0));
119 Array3D buffer (small_width, small_height, small_depth);
121 for (std::size_t dim = 0; dim < 3; ++dim)
123 const long int off = offset[dim];
124 for (std::size_t n_iter = 0; n_iter < 2; ++n_iter)
126 std::swap (buffer, data);
127 for(std::size_t x = 1; x < small_width - 1; ++x)
128 for(std::size_t y = 1; y < small_height - 1; ++y)
130 Eigen::Vector2f* d_ptr = &(data (x,y,1));
131 Eigen::Vector2f* b_ptr = &(buffer (x,y,1));
133 for(std::size_t z = 1; z < small_depth - 1; ++z, ++d_ptr, ++b_ptr)
134 *d_ptr = (*(b_ptr - off) + *(b_ptr + off) + 2.0 * (*b_ptr)) / 4.0;
141 for (
auto d = data.
begin (); d != data.
end (); ++d)
142 *d /= ((*d)[0] != 0) ? (*d)[1] : 1;
144 for (std::size_t x = 0; x < input_->width; x++)
145 for (std::size_t y = 0; y < input_->height; y++)
147 const float z = output (x,y).z - base_min;
149 static_cast<float> (y) / sigma_s_ + padding_xy,
150 z / sigma_r_ + padding_z);
151 output(x,y).z = D[0];
156 for (std::size_t x = 0; x < input_->width; ++x)
157 for (std::size_t y = 0; y < input_->height; ++y)
159 const float z = output (x,y).z - base_min;
161 static_cast<float> (y) / sigma_s_ + padding_xy,
162 z / sigma_r_ + padding_z);
163 output (x,y).z = D[0] / D[1];
169 template <
typename Po
intT> std::size_t
171 const std::size_t max_value,
174 if (x >= min_value && x <= max_value)
186 template <
typename Po
intT> Eigen::Vector2f
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);
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);
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);
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);
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);
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)
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.
PointCloud represents the base class in PCL for storing collections of 3D points.
void copyPointCloud(const pcl::PointCloud< PointInT > &cloud_in, pcl::PointCloud< PointOutT > &cloud_out)
Copy all the fields from a given point cloud into a new point cloud.