Nalyd1002

segmentation2

May 7th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import pybullet as p
  2. import pybullet_data
  3. import open3d as o3d
  4. import numpy as np
  5.  
  6. # Connect to PyBullet and set up the simulation environment
  7. p.connect(p.GUI)
  8. p.setAdditionalSearchPath(pybullet_data.getDataPath())
  9.  
  10. # Load a plane and two cubes onto it
  11. plane = p.loadURDF("plane.urdf")
  12. cube1 = p.loadURDF("cube.urdf", [0, 0, 0.5])
  13. cube2 = p.loadURDF("cube.urdf", [1.5, 0, 0.5])
  14. #cube3 = p.loadURDF("cube.urdf", [3, 0, 0.5])
  15.  
  16. # Step the simulation to update the positions of the objects
  17. p.stepSimulation()
  18.  
  19. # Get the positions of the cubes
  20. cube1_pos, _ = p.getBasePositionAndOrientation(cube1)
  21. cube2_pos, _ = p.getBasePositionAndOrientation(cube2)
  22. #cube3_pos, _ = p.getBasePositionAndOrientation(cube3)
  23.  
  24.  
  25. # Create a point cloud of the plane and the two cubes
  26. points = []
  27. for x in np.linspace(-5, 5, 100):
  28. for y in np.linspace(-5, 5, 100):
  29. z = 0
  30. points.append([x, y, z])
  31.  
  32. for cube_pos in [cube1_pos, cube2_pos]:
  33. for x in np.linspace(-0.5, 0.5, 10):
  34. for y in np.linspace(-0.5, 0.5, 10):
  35. for z in np.linspace(-0.5, 0.5, 10):
  36.  
  37. points.append([x + cube_pos[0], y + cube_pos[1], z + 1 + cube_pos[2]])
  38.  
  39. # Create Open3D point cloud object
  40. pcd = o3d.geometry.PointCloud()
  41. pcd.points = o3d.utility.Vector3dVector(points)
  42.  
  43. # Preprocess the data
  44. pcd_filtered = pcd.voxel_down_sample(voxel_size=0.1)
  45.  
  46. # Apply segmentation algorithm
  47. labels = pcd_filtered.cluster_dbscan(eps=0.5, min_points=10)
  48.  
  49.  
  50. """"# Segment the point cloud into different regions using ECE algorithm
  51. ece = pcd.cluster_dbscan(eps=1, min_points=5)
  52. max_label = np.max(ece)
  53. print('max label')
  54. print(max_label)
  55. for i in range(max_label + 1):
  56. indices = np.where(ece == i)[0]
  57. segment = pcd.select_by_index(indices)
  58. labels = np.array(segment.cluster_dbscan(eps=1, min_points=5))
  59. print("ok")
  60. print(f"Number of clusters in segment {i}: {len(set(labels))}")
  61. """
  62. # Visualize the segmented point cloud
  63. #colors = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1]]
  64. #color_array = [colors[label % len(colors)] for label in labels]
  65.  
  66. # Assign random colors to each segment
  67. num_segments = len(set(labels))
  68. print("Number of segments")
  69. print(num_segments)
  70. colors = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1]]
  71. color_array = [colors[label % num_segments] for label in np.asarray(labels).flatten()]
  72. pcd_filtered.colors = o3d.utility.Vector3dVector(color_array)
  73. o3d.visualization.draw_geometries([pcd_filtered])
  74. # Assign random colors to each segment
  75. """num_segments = len(set(labels))
  76. print("num seg")
  77. print(num_segments)
  78. colors = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1]]
  79. color_array = [colors[label % num_segments] for label in np.asarray(labels).flatten()]
  80.  
  81. # Assign a random color to each point
  82. pcd_filtered.colors = o3d.utility.Vector3dVector(np.random.rand(len(points), 3))
  83. o3d.visualization.draw_geometries([pcd_filtered])
  84.  
  85. # Extract object positions and orientations
  86. for label in set(labels):
  87. if label == -1:
  88. continue
  89. points = pcd_filtered.select_down_sample(np.where(labels == label)[0])
  90. centroid = np.mean(points.points, axis=0)
  91. print(f"Object {label}: position={centroid}, orientation={points.get_oriented_bounding_box().get_rotation_matrix()}")"""
  92. # Extract object positions and orientations
  93. for label in set(labels):
  94. print("enter1")
  95. if label == -1:
  96. print("enter2")
  97. continue
  98. print("enter3")
  99. indices = np.where(labels == label)[0]
  100. print("indices")
  101. print(indices)
  102. points = pcd_filtered.select_by_index(indices)
  103. print('points')
  104. print(points)
  105. centroid = np.mean(points.points, axis=0)
  106. print(
  107. f"Object {label}: position={centroid}")
  108. #orientation={points.get_oriented_bounding_box().get_rotation_matrix()}"
  109.  
Advertisement
Add Comment
Please, Sign In to add comment