pclpy 均匀采样

均匀采样

一、算法原理

  均匀采样通过构建指定半径的球体对点云进行下采样滤波,将每一个球内距离球体中心最近的点作为下采样之后的点输出。

体素滤波(下采样)是建立一个立方体,均匀采样是建立一个球。

二、代码实现

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("采样前点的个数为: ", cloud.size())

    # 创建均匀采样滤波器
    us = pcl.filters.UniformSampling.PointXYZ()
    us.setInputCloud(cloud)  # 输入点云
    us.setRadiusSearch(0.005)  # 设置滤波时创建的球体半径
    us.filter(cloud_filtered)
    print("均匀采样后点的个数为: ", cloud_filtered.size())

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

三、结果展示

pclpy 均匀采样

上一篇:Python敏感词汇检测


下一篇:pclpy 统计滤波