PCL自定义点和点云使用

目录

自定义点云步骤

自定义点云示例

具体使用样例


定义点步骤

1、定义自己的点云结构

2、注册这个点云结构

3、在程序中合理使用(可能不能用viewer显示了,可以保存相应文件)

自定义点示例

//第一步:自定义点云结构
struct MyPointType
{
    float x;
    float y;
    float z;
    float i;
    float ring;
    float time;
    //其它的值都可以
} ;              
//第二步: 注册这个点结构
POINT_CLOUD_REGISTER_POINT_STRUCT(MyPointType,           
    (float, x, x)
    (float, y, y)
    (float, z, z)
    (float, i, i)
    (float, ring, ring)
    (float, time, time)
)
//接下来在程序中使用即可

具体使用样例

int
main(int argc, char** argv)
{
    pcl::PointCloud<MyPointType> cloud;
    cloud.points.resize(20000);
    cloud.width = 20000;
    cloud.height = 1;

    for (size_t i = 0; i < cloud.points.size(); ++i)
    {
        //std::cout << i << std::endl;
        cloud.points[i].x = 1024 * (rand() / (RAND_MAX + 1.0f));
        cloud.points[i].y = 1024 * (rand() / (RAND_MAX + 1.0f));
        cloud.points[i].z = 1024 * (rand() / (RAND_MAX + 1.0f));
        cloud.points[i].i = 1024 * (rand() / (RAND_MAX + 1.0f));
        cloud.points[i].ring = 1024 * (rand() / (RAND_MAX + 1.0f));
        cloud.points[i].time = 10240 * (rand() / (RAND_MAX + 1.0f));
    }

    pcl::io::savePCDFileASCII("test_que.pcd", cloud);

    // Create the filtering object
    pcl::PCLPointCloud2::Ptr cloud1(new pcl::PCLPointCloud2());
    pcl::PCLPointCloud2::Ptr cloud_filtered(new pcl::PCLPointCloud2());

    pcl::PCDReader reader;
    reader.read("test_que.pcd", *cloud1); // Remember to download the file first!

    pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
    sor.setInputCloud(cloud1);
    sor.setLeafSize(50.0f, 50.0f, 50.0f);
    sor.filter(*cloud_filtered);

    pcl::PointCloud<MyPointType>::Ptr cloud_show(new pcl::PointCloud<MyPointType>);
    pcl::fromPCLPointCloud2(*cloud_filtered, *cloud_show);
    pcl::io::savePCDFile("test_que_re.pcd", *cloud_show);

}

参考链接:https://pcl.readthedocs.io/projects/tutorials/en/latest/adding_custom_ptype.html#adding-custom-ptype

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自定义一个名为 `PointXYZRGBAI` 的点云类型,可以按以下步骤操作: 1. 创建一个名为 `PointXYZRGBAI` 的结构体,该结构体包含以下字段: ```cpp struct PointXYZRGBAI { PCL_ADD_POINT4D; // 添加 X、Y、Z 坐标 union { struct { uint8_t b; uint8_t g; uint8_t r; uint8_t a; uint8_t i; }; float rgbai; }; EIGEN_MAKE_ALIGNED_OPERATOR_NEW // 保证内存对齐 } EIGEN_ALIGN16; ``` 2. 使用 `PCL_ADD_POINT4D` 宏添加 X、Y、Z 坐标。这个宏会自动添加 `x`、`y`、`z` 字段和一些方法,用于操作这些字段。 3. 添加 `b`、`g`、`r`、`a` 和 `i` 字段,这些字段分别表示的颜色和透明度以及其它属性。这些字段也可以用 `rgbai` 字段来表示,它是一个 `float` 类型的联合体,可以直接访问这些字段。 4. 使用 `EIGEN_MAKE_ALIGNED_OPERATOR_NEW` 宏来保证内存对齐。 下面是完整的代码示例: ```cpp #include <pcl/point_types.h> struct PointXYZRGBAI { PCL_ADD_POINT4D; // 添加 X、Y、Z 坐标 union { struct { uint8_t b; uint8_t g; uint8_t r; uint8_t a; uint8_t i; }; float rgbai; }; EIGEN_MAKE_ALIGNED_OPERATOR_NEW // 保证内存对齐 } EIGEN_ALIGN16; POINT_CLOUD_REGISTER_POINT_STRUCT(PointXYZRGBAI, (float, x, x) (float, y, y) (float, z, z) (uint32_t, rgba, rgba) (uint8_t, i, i)) ``` 在注册点云类型时,可以使用 `POINT_CLOUD_REGISTER_POINT_STRUCT` 宏来注册 `PointXYZRGBAI` 类型。注意,`rgba` 字段必须使用 `uint32_t` 类型表示,因为 PCL 内部使用 32 位整数来表示颜色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值