Point cloud library PCL learning: normal estimation and display

Introduction:

Surface normal is an important attribute of the geometry surface, and plays an important role in model point cloud segmentation and calculation of feature description. The following is the extraction and display of normals. When using it, select the appropriate neighborhood scale.

Code:

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/features/normal_3d.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>

int main(int argc, char **argv)
{
    //读取点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    if
(pcl::io::loadPCDFile<pcl::PointXYZ>("steelCoil.pcd", *cloud) == -1)
    {
        PCL_ERROR("Cloudn't read file!");
        system("pause");
        return -1;
    }
    
    //估计法线
    pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
    ne.setInputCloud(cloud);
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
    ne.setSearchMethod(tree);
    pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>);
    ne.setRadiusSearch(0.6);  //使用半径0.6范围内的所有邻元素估计法线
    ne.compute(*cloud_normals);   

    //可视化
    pcl::visualization::PCLVisualizer viewer("PCL Viewer");
    viewer.setBackgroundColor(0.0,
0.0, 0.0);
    //显示点云与法线,1和0.5可以调整法线的疏密与长短
    viewer.addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud,
cloud_normals, 1, 0.5, "normals");

    while (!viewer.wasStopped())
    {
        viewer.spinOnce(100);
    }
    return 0;
} 

effect:

Insert picture description here

Published 22 original articles · Likes2 · Visits 1157

Guess you like

Origin blog.csdn.net/qinqinxiansheng/article/details/105498140