Go to the documentation of this file.00001
00059 #ifndef SPECKLE_FILTER_HPP_
00060 #define SPECKLE_FILTER_HPP_
00061
00062
00063
00064
00065
00066 #include "cob_3d_mapping_filters/speckle_filter.h"
00067
00068
00069
00070 #include "pcl/point_types.h"
00071
00072 #include <pcl/filters/extract_indices.h>
00073 #include <pcl/filters/impl/extract_indices.hpp>
00074
00075 template<typename PointT>
00076 void
00077 cob_3d_mapping_filters::SpeckleFilter<PointT>::applyFilter (PointCloud &pc_out)
00078 {
00079 pc_out.points.resize (input_->points.size ());
00080 pc_out.header = input_->header;
00081
00082 points_to_remove_->indices.clear ();
00083 float newVal = std::numeric_limits<float>::quiet_NaN ();
00084 int width = input_->width, height = input_->height, npixels = width * height;
00085 size_t bufSize = npixels * (int)(sizeof(Eigen::Vector2i) + sizeof(int) + sizeof(unsigned char));
00086
00087 unsigned char* buf = new unsigned char[bufSize];
00088 unsigned char* rem_buf = buf;
00089 int i, j, dstep = input_->width;
00090 int* labels = (int*)buf;
00091 buf += npixels * sizeof(labels[0]);
00092 Eigen::Vector2i* wbuf = (Eigen::Vector2i*)buf;
00093 buf += npixels * sizeof(wbuf[0]);
00094 unsigned char* rtype = (unsigned char*)buf;
00095 int curlabel = 0;
00096
00097
00098 memset (labels, 0, npixels * sizeof(labels[0]));
00099
00100 for (i = 0; i < height; i++)
00101 {
00102 const PointT * const ds = &input_->points[i * input_->width];
00103 int* ls = labels + width * i;
00104
00105 for (j = 0; j < width; j++)
00106 {
00107 if (!pcl_isnan (ds[j].y))
00108 {
00109 if (ls[j])
00110 {
00111 if (rtype[ls[j]])
00112 {
00113 points_to_remove_->indices.push_back (j + i * input_->width);
00114 }
00115 }
00116
00117 else
00118 {
00119 Eigen::Vector2i* ws = wbuf;
00120 Eigen::Vector2i p (j, i);
00121 curlabel++;
00122 int count = 0;
00123 ls[j] = curlabel;
00124
00125
00126 while (ws >= wbuf)
00127 {
00128 count++;
00129
00130 const PointT * const dpp = &input_->points[p(0) + p(1) * input_->width];
00131 const PointT dp = *dpp;
00132 int* lpp = labels + width * p(1) + p(0);
00133
00134 if (p(0) < width - 1 && !lpp[+1] && !pcl_isnan (dpp[+1].z)
00135 && std::abs (dp.z - dpp[+1].z) <= speckle_range_)
00136 {
00137 lpp[+1] = curlabel;
00138 *ws++ = Eigen::Vector2i (p(0) + 1, p(1));
00139
00140 }
00141
00142 if (p(0) > 0 && !lpp[-1] && !pcl_isnan (dpp[-1].z) && std::abs (dp.z - dpp[-1].z) <= speckle_range_)
00143 {
00144 lpp[-1] = curlabel;
00145 *ws++ = Eigen::Vector2i (p(0) - 1, p(1));
00146 }
00147
00148 if (p(1) < height - 1 && !lpp[+width] && !pcl_isnan (dpp[+dstep].z)
00149 && std::abs (dp.z - dpp[+dstep].z) <= speckle_range_)
00150 {
00151 lpp[+width] = curlabel;
00152 *ws++ = Eigen::Vector2i (p(0), p(1) + 1);
00153 }
00154
00155 if (p(1) > 0 && !lpp[-width] && !pcl_isnan (dpp[-dstep].z)
00156 && std::abs (dp.z - dpp[-dstep].z) <= speckle_range_)
00157 {
00158 lpp[-width] = curlabel;
00159 *ws++ = Eigen::Vector2i (p(0), p(1) - 1);
00160 }
00161
00162
00163
00164 p = *--ws;
00165
00166 }
00167
00168
00169 if (count <= speckle_size_)
00170 {
00171 points_to_remove_->indices.push_back (j + i * input_->width);
00172 rtype[ls[j]] = 1;
00173 }
00174 else
00175 rtype[ls[j]] = 0;
00176 }
00177 }
00178 else
00179 points_to_remove_->indices.push_back (j + i * input_->width);
00180 }
00181 }
00182
00183 delete[] rem_buf;
00184
00185
00186
00187 pcl::ExtractIndices<PointT> extractIndices;
00188 extractIndices.setInputCloud (input_);
00189 extractIndices.setIndices (points_to_remove_);
00190 extractIndices.setNegative (true);
00191 extractIndices.filter (pc_out);
00192
00193 }
00194
00195 #define PCL_INSTANTIATE_SpeckleFilter(T) template class cob_3d_mapping_filters::SpeckleFilter<T>;
00196 #endif