得到点的法向量存储输出

#include <iostream>  
#include <pcl/point_types.h>  
#include <pcl/io/pcd_io.h>  
#include <pcl/features/normal_3d.h>  
  
int main()  
{  
    // 加载点云数据  
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>);  
    if (pcl::io::loadPCDFile<pcl::PointXYZ>("input.pcd", *cloud_in) == -1)  
    {  
        PCL_ERROR("Couldn't read file input.pcd \n");  
        return (-1);  
    }  
  
    // 创建法线存储对象  
    pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>);  
  
    // 创建KdTree对象,用于在点云中搜索邻近点  
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());  
    tree->setInputCloud(cloud_in);  
  
    // 创建法线估计对象  
    pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;  
    ne.setInputCloud(cloud_in);  
    ne.setSearchMethod(tree);  
    // 设置搜索半径  
    ne.setRadiusSearch(0.03);  
  
    // 计算法线  
    ne.compute(*cloud_normals);  
  
    // 输出法线  
    std::cout << "Point Cloud normals with " << cloud_normals->points.size() << " data points." << std::endl;  
    for (size_t i = 0; i < cloud_normals->points.size(); ++i)  
    {  
        std::cout << "    point " << cloud_in->points[i].x << " "   
                  << cloud_in->points[i].y << " "   
                  << cloud_in->points[i].z << " has normal "   
                  << cloud_normals->points[i].normal_x << " "   
                  << cloud_normals->points[i].normal_y << " "   
                  << cloud_normals->points[i].normal_z << std::endl;  
    }  
  
    // 如果你想把带有法线的点云保存为PCD文件  
    pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);  
    pcl::concatenateFields(*cloud_in, *cloud_normals, *cloud_with_normals);  
    pcl::io::savePCDFileASCII("output_with_normals.pcd", *cloud_with_normals);  
  
    return 0;  
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值