目录
1. crop_point_cloud
2. crop
3. crop_mesh
1. crop_point_cloud
关键函数
chair = vol.crop_point_cloud(pcd) # vol: SelectionPolygonVolume
import open3d as o3d
if __name__ == "__main__":
# 1. read pcd
print("Load a ply point cloud, crop it, and render it")
sample_ply_data = o3d.data.DemoCropPointCloud()
pcd = o3d.io.read_point_cloud(sample_ply_data.point_cloud_path)
# 2. crop
# vol: SelectionPolygonVolume, 定义裁剪区域
vol = o3d.visualization.read_selection_polygon_volume(sample_ply_data.cropped_json_path)
chair = vol.crop_point_cloud(pcd)
# 2. view
# Flip the pointclouds, otherwise they will be upside down.
pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
chair.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
print("Displaying original pointcloud ...")
o3d.visualization.draw([pcd])
print("Displaying cropped pointcloud")
o3d.visualization.draw([chair])
2. crop
pcd_cropped = pcd.crop(bbox) # pcd: PointCloud. bbox: AxisAlignedBoundingBox
AxisAlignedBoundingBox https://blog.csdn.net/jizhidexiaoming/article/details/130561514?spm=1001.2014.3001.5501
# 1. read pcd
print("Load a ply point cloud, crop it, and render it")
sample_ply_data = o3d.data.DemoCropPointCloud()
pcd = o3d.io.read_point_cloud(sample_ply_data.point_cloud_path)
# 2. 定义裁剪区域。沿着y轴过滤: 只返回y坐标在[0.1,2]之间的点
bounds_list = [[-math.inf, math.inf], [0.8, 2], [-math.inf, math.inf]] # xyz边界范围
# list2tuple, limit points边界点。itertools.product排列笛卡尔乘积是x,y,z各取一个值,组成xyz点坐标
bbox_pt_list = list(itertools.product(*bounds_list))
bbox_pt_vec = o3d.utility.Vector3dVector(bbox_pt_list) # list
bbox = o3d.geometry.AxisAlignedBoundingBox.create_from_points(bbox_pt_vec)
# 3. crop
pcd_cropped = pcd.crop(bbox)
# 4. display
# Flip the pointclouds, otherwise they will be upside down.
pcd_cropped.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
o3d.visualization.draw(pcd_cropped)
3. crop_mesh
上面是裁剪PointCloud,这里裁剪TriangleMesh.
转成numpy,修改mesh的traingles和triangle_normals成员。
import open3d as o3d
import numpy as np
import copy
if __name__ == "__main__":
# 1. read mesh
knot_mesh = o3d.data.KnotMesh()
mesh = o3d.io.read_triangle_mesh(knot_mesh.path)
mesh.compute_vertex_normals()
print("Displaying original mesh ...")
o3d.visualization.draw([mesh])
# 2. crop: triangles and triangle_normals
print("Displaying mesh of only the first half triangles ...")
mesh_cropped = copy.deepcopy(mesh) # mesh_cropped.triangles. (num, 3)
mesh_cropped.triangles = o3d.utility.Vector3iVector( # triangles
np.asarray(mesh_cropped.triangles)[:len(mesh_cropped.triangles) // 2, :])
mesh_cropped.triangle_normals = o3d.utility.Vector3dVector( # triangle_normals
np.asarray(mesh_cropped.triangle_normals)
[:len(mesh_cropped.triangle_normals) // 2, :])
print(mesh_cropped.triangles)
o3d.visualization.draw([mesh_cropped])