别人给了一段代码,但是没有给传参设置,只有函数那部分,希望加一部分传参函数,进行批量读取点云数据(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)