import open3d as o3d # 读取点云数据 point_cloud = o3d.io.read_point_cloud("01.pcd") # 创建Voxel Grid下采样器 voxel_size = 0.1 # 设置立方体格子的大小 downpcd = point_cloud.voxel_down_sample(voxel_size) # 保持下采样后的点云数量为2048 if len(downpcd.points) > 2048: downpcd.points = downpcd.points[:2048] # 可视化结果 o3d.io.write_point_cloud("downsampled_point_cloud.pcd", downpcd) o3d.visualization.draw_geometries([downpcd])请修改以下这段代码,使得体素下采样在体素方块中随机采样一个点
时间: 2024-04-18 16:24:11 浏览: 176
import open3d as o3d
import numpy as np
# 读取点云数据
point_cloud = o3d.io.read_point_cloud("01.pcd")
# 创建Voxel Grid下采样器
voxel_size = 0.1 # 设置立方体格子的大小
downpcd = point_cloud.voxel_down_sample(voxel_size)
# 从每个体素中随机选择一个点
sampled_points = []
for voxel in downpcd.get_voxel_centers():
voxel_points = np.asarray(point_cloud.points)[np.where((point_cloud.points >= voxel) & (point_cloud.points < voxel + voxel_size))]
if len(voxel_points) > 0:
random_index = np.random.randint(len(voxel_points))
sampled_points.append(voxel_points[random_index])
# 创建新的点云对象
new_point_cloud = o3d.geometry.PointCloud()
new_point_cloud.points = o3d.utility.Vector3dVector(sampled_points)
# 可视化结果
o3d.io.write_point_cloud("random_sampled_point_cloud.pcd", new_point_cloud)
o3d.visualization.draw_geometries([new_point_cloud])
阅读全文