点云到平面的映射

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace pcl;
using namespace std;
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloud;

int main(int argc, char** argv)
{

    //*******点云往平面投影的方法**********
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_projected(new pcl::PointCloud<pcl::PointXYZ>);

    if (pcl::io::loadPCDFile("C:/Users/18148/Desktop/qq3.pcd", *cloud) == -1){
        PCL_ERROR("Could not read pcd file!\n");
        return -1;
    }

    std::cerr << "Cloud before projection: " << std::endl;
    /*for (size_t i = 0; i < cloud->points.size(); ++i)
        std::cerr << "    " << cloud->points[i].x << " "
        << cloud->points[i].y << " "
        << cloud->points[i].z << std::endl;
*/
    // 定义模型系数对象,并填充对应的数据Create a set of planar coefficients with X=Y=0,Z=1
    pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
    coefficients->values.resize(4);
    coefficients->values[0] = 0.5;
    coefficients->values[1] = 1;
    coefficients->values[2] = 1;
    coefficients->values[3] = 0;

    // Create the filtering object
    pcl::ProjectInliers<pcl::PointXYZ> proj;//创建投影滤波对象
    proj.setModelType(pcl::SACMODEL_PLANE);//设置对象对应的投影模型
    proj.setInputCloud(cloud);//设置输入点云
    proj.setModelCoefficients(coefficients);//设置模型对应的系数
    proj.filter(*cloud_projected);//执行投影滤波存储结果cloud_projected

    std::cerr << "Cloud after projection: " << std::endl;
    //for (size_t i = 0; i < cloud_projected->points.size(); ++i)
    //    std::cerr << "    " << cloud_projected->points[i].x << " "
    //    << cloud_projected->points[i].y << " "
    //    << cloud_projected->points[i].z << std::endl;

    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    viewer->setBackgroundColor(0, 0, 0);
    //pcl::visualization::PointCloudColorHandlerRGBField<PointT> rgb(cloud_pointsPtr);
    pcl::visualization::PointCloudColorHandlerCustom<PointT> blue(cloud, 0, 0, 255);
    viewer->addPointCloud<PointT>(cloud, blue, "sample cloud");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");

    pcl::visualization::PointCloudColorHandlerCustom<PointT> red(cloud_projected, 255, 0, 0);
    viewer->addPointCloud<PointT>(cloud_projected, red, "sample cloud2");
    viewer->addCoordinateSystem(1.0);
    viewer->initCameraParameters();

    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }
    system("pause");
    return 0;
}

转载自:https://www.cnblogs.com/lovebay/p/9337364.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值