pcl小知识(七)——增添点云、点云空间变换、点云链接、删除空值

转载自:https://segmentfault.com/a/1190000007125502

一、如何从点云里删除和添加点?

#include <pcl/io/pcd_io.h>
#include <pcl/common/impl/io.hpp>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
 
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>("C:\office3-after21111.pcd", *cloud);
pcl::PointCloud<pcl::PointXYZ>::iterator index = cloud->begin();
cloud->erase(index);//删除第一个
index = cloud->begin() + 5;
cloud->erase(cloud->begin());//删除第5个
pcl::PointXYZ point = { 1, 1, 1 };
//在索引号为5的位置1上插入一点,原来的点后移一位
cloud->insert(cloud->begin() + 5, point);
cloud->push_back(point);//从点云最后面插入一点
std::cout << cloud->points[5].x;//输出1

 

如果删除的点太多建议用上面的方法拷贝到新点云,再赋值给原点云,如果要添加很多点,建议先resize,然后用循环向点云里的添加。

二、如何对点云进行全局或局部的空间变换

 

#include <pcl/io/pcd_io.h>
#include <pcl/common/impl/io.hpp>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/common/transforms.h>
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
        pcl::io::loadPCDFile("path/.pcd",*cloud);
//全局变化
 //构造变化矩阵
        Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity();
        float theta = M_PI/4;   //旋转的度数,这里是45度
        transform_1 (0,0) = cos (theta);  //这里是绕的Z轴旋转
        transform_1 (0,1) = -sin(theta);
        transform_1 (1,0) = sin (theta);
        transform_1 (1,1) = cos (theta);
        //   transform_1 (0,2) = 0.3;   //这样会产生缩放效果
        //   transform_1 (1,2) = 0.6;
        //    transform_1 (2,2) = 1;
        transform_1 (0,3) = 25; //这里沿X轴平移
        transform_1 (1,3) = 30;
        transform_1 (2,3) = 380;
        pcl::PointCloud<pcl::PointXYZ>::Ptr transform_cloud1 (new pcl::PointCloud<pcl::PointXYZ>);
        pcl::transformPointCloud(*cloud,*transform_cloud1,transform_1);  //不言而喻
        
        //局部
        pcl::transformPointCloud(*cloud,pcl::PointIndices indices,*transform_cloud1,matrix); //第一个参数为输入,第二个参数为输入点云中部分点集索引,第三个为存储对象,第四个是变换矩阵。

三、链接两个点云字段(两点云大小必须相同)

 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
         pcl::io::loadPCDFile("/home/yxg/pcl/pcd/mid.pcd",*cloud);
         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.setKSearch(8);
        //ne.setRadisuSearch(0.3);
        ne.compute(*cloud_normals);    
        pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_nomal (new pcl::PointCloud<pcl::PointNormal>);
        pcl::concatenateFields(*cloud,*cloud_normals,*cloud_with_nomal);

 

四、如何从点云中删除无效点:坐标空值

pcl中的无效点是指:点的某一坐标值为nan.

 

 #include <pcl/point_cloud.h>
    #include <pcl/point_types.h>
    #include <pcl/filters/filter.h>
    #include <pcl/io/pcd_io.h>
    
    using namespace std;
    typedef pcl::PointXYZRGBA point;
    typedef pcl::PointCloud<point> CloudType;
    
    int main (int argc,char **argv)
    {
            CloudType::Ptr cloud (new CloudType);
            CloudType::Ptr output (new CloudType);
    
            
            pcl::io::loadPCDFile(argv[1],*cloud);
            cout<<"size is:"<<cloud->size()<<endl;
            
            
            vector<int> indices;
            pcl::removeNaNFromPointCloud(*cloud,*output,indices);
            cout<<"output size:"<<output->size()<<endl;
            
    
            pcl::io::savePCDFile("out.pcd",*output);
    
            return 0;
    }
  • 8
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值