40 #ifndef PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_
41 #define PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_
43 #include <pcl/common/io.h>
46 template <
typename Po
intT>
void
49 if (!input_->isOrganized ())
51 PCL_ERROR (
"[pcl::FastBilateralFilter] Input cloud needs to be organized.\n");
56 float base_max = -std::numeric_limits<float>::max (),
57 base_min = std::numeric_limits<float>::max ();
58 bool found_finite =
false;
59 for (
size_t x = 0; x < output.
width; ++x)
61 for (
size_t y = 0; y < output.
height; ++y)
63 if (pcl_isfinite (output (x, y).z))
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;
75 PCL_WARN (
"[pcl::FastBilateralFilter] Given an empty cloud. Doing nothing.\n");
79 for (
size_t x = 0; x < output.
width; ++x)
80 for (
size_t y = 0; y < output.
height; ++y)
81 if (!pcl_isfinite (output (x, y).z))
82 output (x, y).z = base_max;
84 const float base_delta = base_max - base_min;
86 const size_t padding_xy = 2;
87 const size_t padding_z = 2;
89 const size_t small_width =
static_cast<size_t> (
static_cast<float> (input_->width - 1) / sigma_s_) + 1 + 2 * padding_xy;
90 const size_t small_height =
static_cast<size_t> (
static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;
91 const size_t small_depth =
static_cast<size_t> (base_delta / sigma_r_) + 1 + 2 * padding_z;
94 Array3D data (small_width, small_height, small_depth);
95 for (
size_t x = 0; x < input_->width; ++x)
97 const size_t small_x =
static_cast<size_t> (
static_cast<float> (x) / sigma_s_ + 0.5f) + padding_xy;
98 for (
size_t y = 0; y < input_->height; ++y)
100 const float z = output (x,y).z - base_min;
102 const size_t small_y =
static_cast<size_t> (
static_cast<float> (y) / sigma_s_ + 0.5f) + padding_xy;
103 const size_t small_z =
static_cast<size_t> (
static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;
105 Eigen::Vector2f& d = data (small_x, small_y, small_z);
106 d[0] += output (x,y).z;
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));
117 Array3D buffer (small_width, small_height, small_depth);
119 for (
size_t dim = 0; dim < 3; ++dim)
121 const long int off = offset[dim];
122 for (
size_t n_iter = 0; n_iter < 2; ++n_iter)
124 std::swap (buffer, data);
125 for(
size_t x = 1; x < small_width - 1; ++x)
126 for(
size_t y = 1; y < small_height - 1; ++y)
128 Eigen::Vector2f* d_ptr = &(data (x,y,1));
129 Eigen::Vector2f* b_ptr = &(buffer (x,y,1));
131 for(
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;
139 for (std::vector<Eigen::Vector2f >::iterator d = data.
begin (); d != data.
end (); ++d)
140 *d /= ((*d)[0] != 0) ? (*d)[1] : 1;
142 for (
size_t x = 0; x < input_->width; x++)
143 for (
size_t y = 0; y < input_->height; y++)
145 const float z = output (x,y).z - base_min;
147 static_cast<float> (y) / sigma_s_ + padding_xy,
148 z / sigma_r_ + padding_z);
149 output(x,y).z = D[0];
154 for (
size_t x = 0; x < input_->width; ++x)
155 for (
size_t y = 0; y < input_->height; ++y)
157 const float z = output (x,y).z - base_min;
159 static_cast<float> (y) / sigma_s_ + padding_xy,
160 z / sigma_r_ + padding_z);
161 output (x,y).z = D[0] / D[1];
169 template <
typename Po
intT>
size_t
171 const size_t max_value,
174 if (x >= min_value && x <= max_value)
178 else if (x < min_value)
189 template <
typename Po
intT> Eigen::Vector2f
194 const size_t x_index = clamp (0, x_dim_ - 1, static_cast<size_t> (x));
195 const size_t xx_index = clamp (0, x_dim_ - 1, x_index + 1);
197 const size_t y_index = clamp (0, y_dim_ - 1, static_cast<size_t> (y));
198 const size_t yy_index = clamp (0, y_dim_ - 1, y_index + 1);
200 const size_t z_index = clamp (0, z_dim_ - 1, static_cast<size_t> (z));
201 const size_t zz_index = clamp (0, z_dim_ - 1, z_index + 1);
203 const float x_alpha = x -
static_cast<float> (x_index);
204 const float y_alpha = y -
static_cast<float> (y_index);
205 const float z_alpha = z -
static_cast<float> (z_index);
208 (1.0f-x_alpha) * (1.0f-y_alpha) * (1.0f-z_alpha) * (*this)(x_index, y_index, z_index) +
209 x_alpha * (1.0f-y_alpha) * (1.0f-z_alpha) * (*
this)(xx_index, y_index, z_index) +
210 (1.0f-x_alpha) * y_alpha * (1.0f-z_alpha) * (*
this)(x_index, yy_index, z_index) +
211 x_alpha * y_alpha * (1.0f-z_alpha) * (*this)(xx_index, yy_index, z_index) +
212 (1.0f-x_alpha) * (1.0f-y_alpha) * z_alpha * (*
this)(x_index, y_index, zz_index) +
213 x_alpha * (1.0f-y_alpha) * z_alpha * (*this)(xx_index, y_index, zz_index) +
214 (1.0f-x_alpha) * y_alpha * z_alpha * (*this)(x_index, yy_index, zz_index) +
215 x_alpha * y_alpha * z_alpha * (*
this)(xx_index, yy_index, zz_index);
Eigen::Vector2f trilinear_interpolation(const float x, const float y, const float z)
virtual void applyFilter(PointCloud &output)
Filter the input data and store the results into output.
std::vector< Eigen::Vector2f >::iterator begin()
std::vector< Eigen::Vector2f >::iterator end()
uint32_t width
The point cloud width (if organized as an image-structure).
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.
static size_t clamp(const size_t min_value, const size_t max_value, const size_t x)
uint32_t height
The point cloud height (if organized as an image-structure).