PCL surface point cloud resampling three methods: sampling, down-sampling, uniform sampling

(1) Sampling Downsampling

Downsampling is generally configured by a three-dimensional voxel grid, and then display the approximate center of gravity of all points in the voxel in the voxel all points within each of the other points of the voxels, voxels so that a center of gravity can be represented by , the sampling to achieve the effect of filtering, thus greatly reduces the amount of data, in particular prior to registration, surface reconstruction, etc. as a pretreatment, may well increase the running speed,

Copy the code

#include <PCL / IO / pcd_io.h> 
#include <PCL / Filters / voxel_grid.h> 

int 
main (int argc, char ** the argv) 
{ 
    // create a point cloud objects 
    pcl :: PointCloud <pcl :: PointXYZ> Cloud the Ptr :: (PCL new new PointCloud :: <PCL :: PointXYZ>); 
    PCL PointCloud :: <PCL PointXYZ ::> :: filteredCloud the Ptr (PCL new new PointCloud :: <PCL :: PointXYZ>); 

    // read PCD file 
    IF (PCL :: IO :: loadPCDFile <PCL :: PointXYZ> (the argv [. 1], Cloud *) = 0!) 
    { 
        return -1; 
    } 

    // Create filter objects 
    pcl :: VoxelGrid <pcl :: PointXYZ > filter; 
    filter.setInputCloud (Cloud); 
    // set the size of the voxel grid 1x1x1cm 
    filter.setLeafSize (0.01F, 0.01F, 0.01F); 
    filter.filter (* filteredCloud);
}

Copy the code

The experimental results (omitted)

(2)

Uniform sampling: This class is essentially the same, but its output point cloud index key is selected in the manner common computing descriptors.

Copy the code

#include <pcl/io/pcd_io.h>
#include <pcl/keypoints/uniform_sampling.h>

int
main(int argc, char** argv)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr filteredCloud(new pcl::PointCloud<pcl::PointXYZ>);
    if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) != 0)
    {
        return -1;
    }
    // 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);
    pcl::copyPointCloud(*cloud, keypointIndices.points, *filteredCloud);
}

Copy the code

(3) by sampling: by sampling a surface reconstruction method, when you have less point cloud data than you think, by sampling it can help you to restore the original surface (S), by interpolation you currently have the point cloud data, this is a complex process hypothetical conjecture. So the results will not build one hundred percent accurate, but sometimes it is an alternative solution. So, in your sampling point and so be sure to save a copy of the original data!

Copy the code

#include <pcl/io/pcd_io.h>
#include <pcl/surface/mls.h>

int main(int argc,char** argv)
{
// 新建点云存储对象
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr filteredCloud(new pcl::PointCloud<pcl::PointXYZ>);

    // 读取文件
    if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) != 0)
    {
        return -1;
    }
    // 滤波对象
    pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointXYZ> filter;
    filter.setInputCloud(cloud);
    //建立搜索对象
    pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree;
    filter.setSearchMethod(kdtree);
    // set the search radius 3cm neighborhood
    filter.setSearchRadius (0.03); 
    Method // Upsampling sampling has DISTINCT_CLOUD, RANDOM_UNIFORM_DENSITY 
    filter.setUpsamplingMethod (PCL MovingLeastSquares :: <:: PointXYZ PCL, PCL PointXYZ ::> :: SAMPLE_LOCAL_PLANE); 
    radius // sample is 
    filter.setUpsamplingRadius (0.03); 
    // number of sampling step size 
    filter.setUpsamplingStepSize (0.02); 

    filter.process (* filteredCloud); 
}

Copy the code

The results of the experiment

Visualization of the original image:

 

(4) surface reconstruction

Depth sensor surface measurements are not accurate, and the resulting measurement error is the presence of a point cloud, such as outliers, holes or the like, a surface reconstruction algorithm can be used, and through all the point cloud data interpolation, attempts to reconstruct the original s surface. For example by sampling, PCL using algorithms and MLS. This step is very important, because the resulting point cloud normal to be more accurate.

Copy the code

#include <pcl/io/pcd_io.h>
#include <pcl/surface/mls.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <boost/thread/thread.hpp>
int
main(int argc, char** argv)
{
    
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);    
    pcl::PointCloud<pcl::PointNormal>::Ptr smoothedCloud(new pcl::PointCloud<pcl::PointNormal>);

    if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) != 0)
    {
        return -1;
    }

    // Smoothing object (we choose what point types we want as input and output).
    pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointNormal> filter;
    filter.setInputCloud(cloud);
    // Use all neighbors in a radius of 3cm.
    filter.setSearchRadius(0.03);
    // If true, the surface and normal are approximated using a polynomial estimation
    // (if false, only a tangent one).
    filter.setPolynomialFit(true);
    // We can tell the algorithm to also compute smoothed normals (optional).
    filter.setComputeNormals(true);
    // kd-tree object for performing searches.
    pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree;
    filter.setSearchMethod(kdtree);

    filter.process(*smoothedCloud);


  boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("smooth"));
viewer->addPointCloud<pcl::PointNormal>(smoothedCloud,"smoothed");

while(!viewer->wasStopped())
  {
 viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(1000000));
 }
}

Copy the code

Run to view the results

                                                               Original image (color added)

                                             Sampling by smoothing (without color information)

 

Published 179 original articles · won praise 124 · views 700 000 +

Guess you like

Origin blog.csdn.net/shixin_0125/article/details/105153554