pclpy 体素滤波

该博客介绍了如何使用PCL库对点云数据进行体素滤波,并通过PCLVisualizer进行可视化展示。首先,读取PCD文件中的点云数据,然后应用VoxelGrid滤波器降低数据密度。接着,使用两种不同颜色在可视化窗口中展示原始点云和过滤后的点云。最后,将过滤后的点云保存为新的PCD文件。
摘要由CSDN通过智能技术生成

1、算法原理

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、完整代码

from pclpy import pcl


def point_cloud_viewer(cloud, cloud_filtered):
    # Open 3D viewer and add point cloud and normals
    viewer = pcl.visualization.PCLVisualizer("cloud_viewer")
    v0 = 1
    viewer.createViewPort(0.0, 0.0, 0.5, 1.0, v0)
    viewer.setBackgroundColor(0.0, 0.0, 0.0, v0)
    single_color = pcl.visualization.PointCloudColorHandlerCustom.PointXYZ(cloud, 0.0, 255.0, 0.0)
    viewer.addPointCloud(cloud, single_color, "sample cloud1", v0)

    v1 = 2
    viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v1)
    viewer.setBackgroundColor(0.0, 0.0, 0.0, v1)
    single_color = pcl.visualization.PointCloudColorHandlerCustom.PointXYZ(cloud_filtered, 255.0, 0.0, 0.0)
    viewer.addPointCloud(cloud_filtered, single_color, "sample cloud2", v1)

    viewer.setPointCloudRenderingProperties(0, 1, "sample cloud1", v0)
    viewer.setPointCloudRenderingProperties(0, 1, "sample cloud2", v1)
    # viewer.addCoordinateSystem(1.0)
    while not viewer.wasStopped():
        viewer.spinOnce(10)


if __name__ == '__main__':
    # 读取点云数据
    cloud = pcl.PointCloud.PointXYZ()
    cloud_filtered = pcl.PointCloud.PointXYZ()
    reader = pcl.io.PCDReader()
    reader.read('bunny.pcd', cloud)
    print("PointCloud before filtering: ",
          cloud.width * cloud.height, " data points ")

    # 创建体素滤波器
    vox = pcl.filters.VoxelGrid.PointXYZ()
    vox.setInputCloud(cloud)  # 输入点云
    vox.setLeafSize(0.01, 0.01, 0.01)  # 设置体素的大小
    vox.filter(cloud_filtered)
    print("PointCloud before filtering: ",
          cloud_filtered.width * cloud_filtered.height, " data points ")

    writer = pcl.io.PCDWriter()
    writer.write("table_scene_lms400_downsampled.pcd", cloud_filtered)
    # 可视化结果
    point_cloud_viewer(cloud, cloud_filtered)

3、运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值