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

参考源:http://www.cnblogs.com/bozhicheng/p/5842428.html
利用的是最小二乘估计的方法计算了点云的法向量,并且提供了两种法线的显示方法,还设置了多个ViewPort,练习了点云的显示:

// NormalEstimation.cpp : Defines the entry point for the console application.
 

#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 


int main()
{
    //加载点云模型
    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;
}

 

你可能感兴趣的:(PCL中计算点云的法向量并显示)