Nalyd1002

convergence function

May 11th, 2023 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. import copy
  2.  
  3. import open3d as o3d
  4. import open3d.core as o3c
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. import pybullet as p
  8. import pybullet_data
  9. import time
  10.  
  11. from sklearn.neighbors import NearestNeighbors
  12. import plotly.express as px
  13.  
  14. # Initialize the physics simulation
  15. physicsClient = p.connect(p.GUI)
  16. p.setAdditionalSearchPath(pybullet_data.getDataPath())
  17. p.setGravity(0, 0, -9.81)
  18.  
  19. # Load the objects into the simulation
  20. planeId = p.loadURDF("plane.urdf")
  21. sphereId = p.loadURDF("sphere_small.urdf", [0, 1, 0.1])
  22. sphereId2 = p.loadURDF("sphere_small.urdf", [0.5, 1, 0.1])
  23. #robotId = p.loadURDF("r2d2.urdf", [0.5,0.5,0.5],useFixedBase=True)
  24. cubeId = p.loadURDF("sphere_small.urdf", [0, 0.5, 0.1])
  25. cubeId2 = p.loadURDF("cube_small.urdf", [0, 0.9, 0.1])
  26.  
  27. # Define constant C for depth
  28. C=10000
  29.  
  30. number_of_iteration = 0
  31.  
  32. eps = 2
  33.  
  34.  
  35. # Convergence algorithm to find eps for DBSCAN
  36.  
  37. def convergence_eps(number_of_objects, max_iteration, C, eps, convergence_speed, points):
  38. number_of_iteration = 0
  39.  
  40. while number_of_iteration != max_iteration and C != 1:
  41. new_points = copy.copy(points)
  42. for y in range(height):
  43. for x in range(width):
  44. new_points[y * width + x, 2] = new_points[y * width + x, 2] * C
  45. # Create Open3D point cloud object
  46. pcd = o3d.geometry.PointCloud()
  47. pcd.points = o3d.utility.Vector3dVector(new_points)
  48. labels = pcd.cluster_dbscan(eps=eps, min_points=9)
  49. num_segments = len(set(labels))
  50. print("number of segments = ", num_segments)
  51. print("eps = ", eps)
  52. colors = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [2, 0, 0], [0, 3, 0]]
  53. color_array = [colors[label % num_segments] for label in np.asarray(labels).flatten()]
  54. pcd.colors = o3d.utility.Vector3dVector(color_array)
  55.  
  56.  
  57. # number of objects must include the ground
  58. if number_of_objects == num_segments and C == 1:
  59. print('number_of_iteration ', number_of_iteration)
  60. print('eps= ', eps)
  61. return eps
  62. break
  63.  
  64. elif num_segments < number_of_objects:
  65. number_of_iteration += 1
  66. print("convergence speed = ", convergence_speed)
  67. eps -= convergence_speed
  68. print("2")
  69.  
  70. elif num_segments >= number_of_objects:
  71. number_of_iteration += 1
  72. C -= 200
  73. if C <= 1:
  74. C = 1
  75. print("C = ", C)
  76. print('eps =', eps)
  77.  
  78. o3d.visualization.draw_geometries([pcd])
  79. print("1")
  80.  
  81.  
  82. print('eps = ', eps)
  83. print("C = ", C)
  84.  
  85.  
  86. while True:
  87. # Get the image from the camera
  88. camera_pos = [0, 1, 2]
  89. viewMatrix = p.computeViewMatrix(
  90. cameraEyePosition=camera_pos,
  91. cameraTargetPosition=[0, 1, 0],
  92. cameraUpVector=[0, 1, 0])
  93.  
  94. # Set the intrinsic parameters of the camera
  95. fov = 60
  96. aspect = 320.0 / 240.0
  97. near = 0.01
  98. far = 100
  99.  
  100. # Get the image from the camera
  101. width, height, rgbImg, depthImg, segImg = p.getCameraImage(width=320, height=240, viewMatrix=viewMatrix,
  102. projectionMatrix=p.computeProjectionMatrixFOV(fov,
  103. aspect,
  104. near, far))
  105. # Convert the depth image to a point cloud
  106. points = np.zeros((height * width, 3))
  107.  
  108. for y in range(height):
  109. for x in range(width):
  110. depth = depthImg[y, x]
  111. if depth != 0:
  112. points[y * width + x, 0] = (x - 160) * depth / 160.0
  113. points[y * width + x, 1] = (y - 120) * depth / 120.0
  114. points[y * width + x, 2] = depth
  115.  
  116. convergence_eps(5, 1000, 7000, 0.1, 0.01, points=points)
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment