PCL ——CropBox 过滤掉用户给定立方体内的点云数据

最近开始学习PCL,博客写作存档学习。

//PCL CropBox   过滤掉用户给定立方体内的点云数据
#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/time.h>
#include <pcl/filters/crop_box.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>


typedef pcl::PointXYZ PointT;
using namespace std;

int main()
{
    pcl::PointCloud<PointT>::Ptr cloud(new pcl::PointCloud<PointT>);
    pcl::console::TicToc tt;

    std::cerr<<"ReadImage...\n",tt.tic();
    pcl::PCDReader reader;
    reader.read("2.pcd",*cloud);
    std::cerr<<"Done  "<<tt.toc()<<"  ms\n"<<std::endl;
    std::cerr<<"The points data:  "<<cloud->points.size()<<std::endl;

    pcl::PointCloud<PointT>::Ptr cloud_filtered(new pcl::PointCloud<PointT>);
    pcl::CropBox<PointT> crop;

    crop.setMin(Eigen::Vector4f(-0.2,-0.2,0.0,1.0)); //给定立体空间
    crop.setMax(Eigen::Vector4f(-2.0,1.0,2.0,1.0));  //数据随意给的,具体情况分析
    crop.setInputCloud(cloud);
    crop.setKeepOrganized(true);
    crop.setUserFilterValue(0.1f);
    crop.filter(*cloud_filtered);
    std::cerr<<"The points data:  "<<cloud_filtered->points.size()<<std::endl;

    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    int v1(0);
    viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
    viewer->setBackgroundColor(0, 0, 0, v1);
    viewer->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud1", v1);

    int v2(0);
    viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
    viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered, "sample cloud2", v2);
    viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
    viewer->addCoordinateSystem(1.0);

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

    return 0;
}

注:F2可查看类明细。
参考链接:(https://blog.csdn.net/ethan_guo/article/details/80359313)

你可能感兴趣的:(PCL)