Point Cloud Filtering in Matlab and PCL

·In PCL, several situations that need to be processed by point cloud filtering are summarized. These situations are:
(1) The irregular density of point cloud data needs to be smoothed.
(2) Outliers need to be removed due to problems such as occlusion.
(3) A large amount of data needs to be "down-sampled" (Downsample).
(4) Noise data needs to be removed.

·The corresponding solutions are:
(1) Restrict the filtering and removal points according to specific given rules.
(2) Modify some attributes of points through common filtering algorithms.
(3) Downsampling the data.

There are many aspects and many functions, such as removing noise points, outliers, point cloud smoothing and holes, data compression, etc.

The original point cloud data often contains a large number of hash points and isolated points. When obtaining the point cloud data, due to the accuracy of the equipment and the operator's experience

The influence of environmental factors, as well as the diffraction of electromagnetic waves, the change of the surface properties of the measured object and the influence of the operation process of data splicing and registration

Noise, some noise is inevitable in the point cloud data, and the sampling resolution is also different.

As the first step of preprocessing in the point cloud processing flow, filtering processing has a relatively large impact on the follow-up. Only when noise points, outliers, holes, and data compression are customized according to the subsequent processing in the filtering preprocessing, can it be more accurate. Good for follow-up application processing such as registration, feature extraction, surface reconstruction, and visualization. It is similar to filtering in signal processing.

Straight-through filter
The straight-through filter is to set the range on the attribute of the point according to the attribute of the point cloud, and filter the point, retaining the range or keeping outside the range.

Specify a dimension and the value range under this dimension
Traverse each point in the point cloud, judge whether the value of the point on the specified dimension is in the value range, delete the point whose value is not in the value range, the traversal ends
, and the remaining points are Constitute the filtered point cloud.

#include <pcl/filters/passthrough.h>
// 原点云获取后进行滤波
pcl::PassThrough<pcl::PointXYZ> pass;// 创建滤波器对象
pass.setInputCloud (cloud);//设置输入点云
pass.setFilterFieldName ("z");//滤波字段名被设置为Z轴方向
pass.setFilterLimits (0.0, 1.0);//可接受的范围为(0.0,1.0)
//pass.setFilterLimitsNegative (true);//设置保留范围内 还是 过滤掉范围内
pass.filter (*cloud_filtered); //执行滤波,保存过滤结果在cloud_filter

Voxel filter (downsampling)
The voxel filter can achieve the function of downsampling without destroying the geometric structure of the point cloud itself, but it will move the position of the point. In addition, the voxel filter can remove a certain degree of noise points and outliers. The main function is to perform downsampling.
Its principle is based on the input point cloud, first calculate a cube that can just wrap the point cloud, and then divide the large cube into different small cubes according to the set resolution. For each point in the small cube, calculate their centroid, and use the coordinates of the centroid to approximate several points in the cube.

The difference of ApproximateVoxelGrid is that this method uses the center of each small cube to approximate several points in the cube. Compared with VoxelGrid, the calculation speed is slightly faster, but it also loses the fineness of the local shape of the original point cloud

#include <pcl/filters/voxel_grid.h>
// VoxelGrid
pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
sor.setInputCloud (cloud);
sor.setLeafSize (0.01f, 0.01f, 0.01f);
sor.filter (*cloud_filtered);
// Approximate 体素格滤波器
pcl::ApproximateVoxelGrid<pcl::PointXYZ> approximate_voxel_filter;
approximate_voxel_filter.setLeafSize (0.2, 0.2, 0.2);
approximate_voxel_filter.setInputCloud (input_cloud);
approximate_voxel_filter.filter (*filtered_cloud)

Uniform Sampling Filter (Downsampling)
Uniform sampling filtering is basically equivalent to a voxel filter, but it does not change the position of the points. After downsampling, the point cloud distribution is basically uniform, but the accuracy of the point cloud is better than voxel filtering because there is no moving point position.
Uniform sampling algorithm:
uniform sampling constructs a sphere with a specified radius to downsample and filter the point cloud, and outputs the point closest to the center of the sphere in each sphere as the point after downsampling.
Voxel filtering is to create a cube, and uniform sampling is to create a sphere.

#include <pcl/keypoints/uniform_sampling.h>
// Uniform sampling object.
pcl::UniformSampling<pcl::PointXYZ> filter;
filter.setInputCloud(cloud);
filter.setRadiusSearch(0.01f);
// We need an additional object to store the indices of surviving points.
pcl::PointCloud<int> keypointIndices;
filter.compute(keypointIndices);

Statistical filter (denoising)
Statistical filter is mainly used to remove obvious outliers. Outlier features are sparsely distributed in space. If the point cloud is defined to be less than a certain density, the point cloud is invalid. Calculate the average distance from each point to its nearest k points. Then the distances of all points in the point cloud should constitute a Gaussian distribution. According to the given mean and variance, the points outside the variance can be eliminated.

#include <pcl/filters/statistical_outlier_removal.h>
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud (cloud);
sor.setMeanK (50); //设置考虑查询点临近点数
sor.setStddevMulThresh (1.0);//设置判断是否为离群点的阀值
sor.filter (*cloud_filtered);
// 然后,使用同样的参数再次调用该滤波器,但是利用函数setNegative设置使输出取外点,以获取离群点数据(也就是原本滤除掉的点)。
sor.setNegative (true);
sor.filter (*cloud_filtered);

Conditional filtering
The conditional filter performs filtering by setting filtering conditions, and deletes one or more conditions that do not meet the user-specified conditions. A pass-through filter is a simpler conditional filter.

#include <pcl/filters/conditional_removal.h>
pcl::ConditionAnd<pcl::PointXYZ>::Ptr range_cond (new pcl::ConditionAnd<pcl::PointXYZ>()); //创建条件定义对象
//添加在Z字段上大于0的比较算子
range_cond->addComparison (pcl::FieldComparison<pcl::PointXYZ>::ConstPtr (new pcl::FieldComparison<pcl::PointXYZ> ("z", pcl::ComparisonOps::GT, 0.0)));
//添加在Z字段上小于0.8的比较算子
range_cond->addComparison (pcl::FieldComparison<pcl::PointXYZ>::ConstPtr (new pcl::FieldComparison<pcl::PointXYZ> ("z", pcl::ComparisonOps::LT, 0.8)));
// 创建滤波器并用条件定义对象初始化
pcl::ConditionalRemoval<pcl::PointXYZ> condrem;
condrem.setCondition (range_cond);
condrem.setInputCloud (cloud);
condrem.setKeepOrganized(true); //设置保持点云的结构
condrem.filter (*cloud_filtered); // 执行滤波

Radius filter (denoising)
The radius filter draws a circle centered on a certain point and calculates the number of points falling in the middle of the circle. When the number is greater than a given value, the point is kept, and the number is smaller than the given value, and the point is eliminated.
It is mainly used to remove outliers, and to a certain extent can be used to filter edge points.

#include <pcl/filters/radius_outlier_removal.h>
pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
outrem.setInputCloud(cloud);
outrem.setRadiusSearch(0.8);//设置半径为0.8的范围内找临近点
outrem.setMinNeighborsInRadius (2);//设置查询点的邻域点集数小于2的删除
outrem.filter (*cloud_filtered); //

Projection filtering
Project points onto a parametric model, which can be a plane, sphere, cylinder, cone, etc. for projection filtering. Project the 3D point cloud onto the 2D image, and then process it with image processing methods.

// 填充ModelCoefficients的值,使用ax+by+cz+d=0平面模型,其中 a=b=d=0,c=1 也就是X——Y平面
//定义模型系数对象,并填充对应的数据
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
coefficients->values.resize(4);
coefficients->values[0] = coefficients->values[1] = 0;
coefficients->values[2] = 1.0;
coefficients->values[3] = 0;
 
// 创建ProjectInliers对象,使用ModelCoefficients作为投影对象的模型参数
pcl::ProjectInliers<pcl::PointXYZ> proj; //创建投影滤波对象
proj.setModelType(pcl::SACMODEL_PLANE); //设置对象对应的投影模型
proj.setInputCloud(cloud);//设置输入点云
proj.setModelCoefficients(coefficients);//设置模型对应的系数
proj.filter(*cloud_projected);//投影结果存储

Model filtering
According to the distance from the point to the model, set the distance threshold to filter the non-model points, and the point segmentation operation based on the model removes the points outside the model from the point cloud.

//x^2 + y^2 + z^2 = 1
pcl::ModelCoefficients sphere_coeff;
sphere_coeff.values.resize (4);
sphere_coeff.values[0] = 0;
sphere_coeff.values[1] = 0;
sphere_coeff.values[2] = 0;
sphere_coeff.values[3] = 1;
pcl::ModelOutlierRemoval<pcl::PointXYZ> sphere_filter;
sphere_filter.setModelCoefficients (sphere_coeff);
sphere_filter.setThreshold (0.05);
sphere_filter.setModelType (pcl::SACMODEL_SPHERE);
sphere_filter.setInputCloud (cloud);
sphere_filter.filter (*cloud_sphere_filtered);

Gaussian filtering (denoising, smoothing)
GaussianKernel is a convolution filtering implementation based on the Gaussian kernel. The Gaussian filter is equivalent to a low-pass filter with smoothing performance. The point cloud processed by this type is relatively smooth.

pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB> convolution;
Eigen::ArrayXf gaussian_kernel(5);
gaussian_kernel << 1.f/16, 1.f/4, 3.f/8, 1.f/4, 1.f/16;
 
convolution.setBordersPolicy(pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_IGNORE);
convolution.setDistanceThreshold (static_cast<float> (0.1));
convolution.setInputCloud (inputCloud);
convolution.setKernel (gaussian_kernel);
convolution.convolve(*cloud);

Bilateral filtering (smoothing)
Bilateral filtering is a nonlinear filter that can achieve the effect of maintaining edges and smoothing noise reduction. To a certain extent, it compensates for the shortcomings of Gaussian filtering. Bilateral filtering works better for Gaussian noise.
insert image description here
The filters module in PCL
pcl::PassThroughtpcl::PointXYZ pass; pass-through filter
pcl::VoxelGrid< pcl::PointXYZ > vox; voxel filter
pcl::StatisticalOutlierRemovalpcl::PointXYZ sor; statistical filter
pcl::RadiusOutlierRemovalpcl:: PointXYZ rador; Radius filter
pcl::UniformSamplinqpcl::PointXYz unisam; Uniform sampling
pcl::ConditionalRemovalpcl::PointXYZ condr; Conditional filter
pcl::ProjectInlierspcl::PointXYZ proj; Projection filter
pcl::ModelOutlierRemovalpcl::PointXYZ modr; model filter
pcl::BilateralFilter pcl::PointXYZ bf: bilateral filter
pcl::filters::GaussianKernal<PointInT, PointOutT> Gaussian filter
pcl::ExtractIndicespcl::PointxYz extr; index extraction
spatial clipping filter
pcl::Clipper3Dpcl::PointXYZ
pcl::BoxCliper3Dpcl::PointXYZ
pcl::CropBoxpcl::PointXYZ
pcl::CropHullpcl::PointXYZ

Guess you like

Origin blog.csdn.net/qq_27353621/article/details/128863190