crop-demo-2.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import open3d as o3d
  2. import numpy as np
  3. pcd = o3d.io.read_point_cloud("../laser1.ply")
  4. # ply_point_cloud = o3d.data.PLYPointCloud()
  5. # pcd = o3d.io.read_point_cloud(ply_point_cloud.path)
  6. corners = np.array([
  7. [ 10, -3.21384387, 0.30217625,],
  8. [ 10, -1.13804348, 0.29917539,],
  9. [ 7.69983939, -1.16651864, 0.30329364,],
  10. [ 7.67473496, -3.24231903, 0.3062945, ],
  11. [ 10, -3.21276837, 1.03551451,],
  12. [ 10, -1.13696798, 1.03251366,],
  13. [ 7.69856999, -1.16544314, 1.03663191,],
  14. [ 7.67346556, -3.24124353, 1.03963277,],
  15. ])
  16. # Convert the corners array to have type float64
  17. bounding_polygon = corners.astype("float64")
  18. # Create a SelectionPolygonVolume
  19. vol = o3d.visualization.SelectionPolygonVolume()
  20. # You need to specify what axis to orient the polygon to.
  21. # I choose the "Y" axis. I made the max value the maximum Y of
  22. # the polygon vertices and the min value the minimum Y of the
  23. # polygon vertices.
  24. vol.orthogonal_axis = "Y"
  25. vol.axis_max = np.max(bounding_polygon[:, 1])
  26. vol.axis_min = np.min(bounding_polygon[:, 1])
  27. # Set all the Y values to 0 (they aren't needed since we specified what they
  28. # should be using just vol.axis_max and vol.axis_min).
  29. bounding_polygon[:, 1] = 0
  30. # Convert the np.array to a Vector3dVector
  31. vol.bounding_polygon = o3d.utility.Vector3dVector(bounding_polygon)
  32. # Crop the point cloud using the Vector3dVector
  33. cropped_pcd = vol.crop_point_cloud(pcd)
  34. # Get a nice looking bounding box to display around the newly cropped point cloud
  35. # (This part is optional and just for display purposes)
  36. bounding_box = cropped_pcd.get_axis_aligned_bounding_box()
  37. bounding_box.color = (1, 0, 0)
  38. # Draw the newly cropped PCD and bounding box
  39. # o3d.visualization.draw_geometries([cropped_pcd, bounding_box],
  40. # zoom=2,
  41. # front=[5, -2, 0.5],
  42. # lookat=[7.67473496, -3.24231903, 0.3062945],
  43. # up=[1.0, 0.0, 0.0])
  44. # o3d.visualization.draw_geometries([pcd, bounding_box])
  45. o3d.visualization.draw([pcd, bounding_box],)