m0_67182379 2023-03-11 15:23 采纳率: 54.5%
浏览 42
已结题

python pcd文件读取

别人给了一段代码,但是没有给传参设置,只有函数那部分,希望加一部分传参函数,进行批量读取点云数据(pcd格式)文件,将代码运行起来。

def SimplifyPointClouds(inputDir, file_name, outputDir):
    print("simplify point clouds", file_name)

    curFileName = inputDir + "/" + file_name
    print("processing"+curFileName)
    # readfile
    pcd = o3d.geometry.PointCloud()

    if file_name.endswith(".pcd"):
        pcd = o3d.io.read_point_cloud(curFileName)
    elif file_name.endswith(".txt"):
        pcdPts = np.loadtxt(curFileName)[:, 0:3]
        pcd.points = o3d.utility.Vector3dVector(pcdPts)
    print("The input point cloud has", pcd)

    '''# denoising
    print("denoising...")
    cl, ind = pcd.remove_statistical_outlier(nb_neighbors=30,
                                             std_ratio=2.0)
    pcd = pcd.select_by_index(ind)
    print("Denoised", pcd)'''

    # down sampling
    pcd = pcd.voxel_down_sample(voxel_size=2.0)
    print("Down_sampled", pcd)
    o3d.io.write_point_cloud(outputDir+"/_1Ds_"+file_name, pcd)

def RegulateData(file_name, outputDir):
    pcd = o3d.io.read_point_cloud(outputDir+"/_1Ds_"+file_name)
    normal = [0.0, -1.0, 0.0]
    R = pyrsc.get_rotationMatrix_from_vectors(normal, [0, 0, 1])
    T = np.eye(4)
    T[:3, :3] = R
    pcd_r = pcd.transform(T)

    # estimate bonding box
    aabb = pcd_r.get_axis_aligned_bounding_box()
    aabb_box_length = np.asarray(aabb.get_extent())
    print("bonding box length: ", aabb_box_length)
    # adjust x and y direction
    if aabb_box_length[1] > aabb_box_length[0]:
        print("Rotate around Z-axis")
        R = o3d.geometry.get_rotation_matrix_from_axis_angle((0, 0, np.pi / 2))
        pcd_r = pcd_r.rotate(R)
    o3d.io.write_point_cloud(outputDir+"/_2Ms_"+file_name, pcd_r) # maize stands with ground

    # detect ground again, remove ground and translate maize canopy to 0 level height
    xyz = np.asarray(pcd_r.points)
    csf = CSF.CSF()

    # CSF算法的必要参数设置
    csf.params.bSloopSmooth = False
    csf.params.cloth_resolution = 1.0  # 布料网格分辨率
    csf.params.rigidness = 3  # 布料刚性参数,只能是1、2、3
    csf.params.time_step = 0.65
    csf.params.class_threshold = 0.2  # 点云与布料模拟点的距离阈值
    csf.params.interations = 500  # 最大迭代次数
    # 更多参数详细信息见: http://ramm.bnu.edu.cn/projects/CSF/download/
    csf.setPointCloud(xyz)
    ground = CSF.VecInt()  # 存储地面点索引的list列表
    non_ground = CSF.VecInt()  # 存储非地面点的list列表
    csf.do_filtering(ground, non_ground)  # 执行CSF滤波
    ground_cloud = pcd.select_by_index(ground)  # 保存地面点
    non_ground_cloud = pcd.select_by_index(non_ground)  # 保存非地面点
    groundPts = np.asarray(ground_cloud.points)
    cur_Z = np.mean(groundPts[:, 2]) # 这个值作为平移的依据(但是这个检测到的地面是最下面的,所以要加上thickness/2.0?)。
    thickness = 15.0  # 地面厚度
    dists = non_ground_cloud.compute_point_cloud_distance(ground_cloud)
    dists = np.asarray(dists)
    ind = np.where(dists > thickness)[0]
    CanopyPcd = non_ground_cloud.select_by_index(ind)
    CanopyPts = np.asarray(CanopyPcd.points)
    ind = np.where(CanopyPts[:, 2] > cur_Z)[0]
    CanopyPcd = CanopyPcd.select_by_index(ind)
    o3d.io.write_point_cloud(outputDir + "/_3Te_" + file_name, CanopyPcd)

  • 写回答

2条回答 默认 最新

  • VenYy 2023-03-11 15:28
    关注
    
    import os
    
    def BatchProcessPointClouds(inputDir, outputDir):
        for file_name in os.listdir(inputDir):
            if not file_name.endswith(".pcd"):
                continue
            SimplifyPointClouds(inputDir, file_name, outputDir)
            RegulateData(file_name, outputDir)
    

    这个函数将遍历指定目录中的所有 .pcd 文件,并对每个文件执行 SimplifyPointClouds() 和 RegulateData() 函数。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月19日
  • 已采纳回答 3月11日
  • 创建了问题 3月11日

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题