您当前的位置: 首页 > 

phymat.nico

暂无认证

  • 2浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

PCL中计算点云的法向量并显示

phymat.nico 发布时间:2020-03-26 15:15:39 ,浏览量:2

#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
int main(int argc, char *argv[])
{
    VTK_MODULE_INIT(vtkRenderingOpenGL);
    //加载点云模型
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
 
    if (pcl::io::loadPCDFile("D:\\rabbit.pcd", *cloud)==-1)
    {
        PCL_ERROR("Could not read file\n");
    }
 
    //计算法线
    pcl::NormalEstimation n;
    pcl::PointCloud::Ptr normals(new pcl::PointCloud);
    //建立kdtree来进行近邻点集搜索
    pcl::search::KdTree::Ptr tree(new pcl::search::KdTree);
    //为kdtree添加点运数据
    tree->setInputCloud(cloud);
    n.setInputCloud(cloud);
    n.setSearchMethod(tree);
    //点云法向计算时,需要所搜的近邻点大小
    n.setKSearch(20);
    //开始进行法向计算
    n.compute(*normals);
 
    /*图形显示模块*/
    boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("3D viewer"));
    //viewer->initCameraParameters();
 
    int v1(0), v2(1),v3(2),v4(3);
    viewer->createViewPort(0.0, 0.0, 0.5, 0.5, v1);
    viewer->createViewPort(0.5, 0.0, 1.0, 0.5, v2);
    viewer->createViewPort(0.0, 0.5, 0.5, 1.0, v3);
    viewer->createViewPort(0.5, 0.5, 1.0, 1.0, v4);
 
    //设置背景颜色
    viewer->setBackgroundColor(5,55, 10, v1);
    //设置点云颜色
    pcl::visualization::PointCloudColorHandlerCustom single_color(cloud, 0, 225, 0);
    //添加坐标系
    viewer->addCoordinateSystem(0.5, v1);
 
    viewer->addPointCloud(cloud, "sample cloud",v1);
    viewer->addPointCloud(cloud, "sample cloud0", v2);
    viewer->addPointCloudNormals(cloud, normals, 50, 0.01, "normals",v2);
    viewer->addPointCloud(cloud, single_color, "sample cloud1", v3);
    viewer->addPointCloud(cloud, "sample cloud3", v4);
 
    //设置点云大小
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "sample cloud3", 4);
 
 
    //添加点云法向量的另一种方式;
    //解决来源http://www.pcl-users.org/How-to-add-PointNormal-on-PCLVisualizer-td4037041.html
    //pcl::PointCloud::Ptr cloud_with_normals(new pcl::PointCloud);
    //pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);
    //viewer->addPointCloudNormals(cloud_with_normals, 50, 0.01, "normals");
    //
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }
 
    return 0;
}

关注
打赏
1659628745
查看更多评论
立即登录/注册

微信扫码登录

0.0529s