Nalyd1002

convergence

May 10th, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 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.01])
  22. sphereId2 = p.loadURDF("sphere_small.urdf", [0.5, 1, 0.01])
  23. #robotId = p.loadURDF("r2d2.urdf", [0.5,0.5,0.5],useFixedBase=True)
  24. cubeId = p.loadURDF("cube_small.urdf", [0, 0.5, 0.01])
  25. cubeId2 = p.loadURDF("cube_small.urdf", [0, 0.9, 0.01])
  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. new_points = copy.copy(points)
  40. while number_of_iteration != max_iteration and C != 1:
  41. # Create Open3D point cloud object
  42. pcd = o3d.geometry.PointCloud()
  43. pcd.points = o3d.utility.Vector3dVector(points)
  44. labels = pcd.cluster_dbscan(eps=eps, min_points=9)
  45. num_segments = len(set(labels))
  46. print("number of segments = ", num_segments)
  47. print("eps = ", eps)
  48.  
  49.  
  50.  
  51. # number of objects must include the ground
  52. if number_of_objects == num_segments and C == 1:
  53. print('number_of_iteration ', number_of_iteration)
  54. print('eps= ', eps)
  55. return eps
  56. break
  57. else:
  58. print(number_of_iteration)
  59. number_of_iteration += 1
  60. eps = eps / convergence_speed
  61. #C = C / convergence_speed
  62. if C<=1:
  63. C = 1
  64. break
  65. print('eps = ', eps)
  66. print("C = ", C)
  67.  
  68.  
  69. while True:
  70. # Get the image from the camera
  71. camera_pos = [0, 1, 2]
  72. viewMatrix = p.computeViewMatrix(
  73. cameraEyePosition=camera_pos,
  74. cameraTargetPosition=[0, 1, 0],
  75. cameraUpVector=[0, 1, 0])
  76.  
  77. # Set the intrinsic parameters of the camera
  78. fov = 60
  79. aspect = 320.0 / 240.0
  80. near = 0.01
  81. far = 100
  82.  
  83. # Get the image from the camera
  84. width, height, rgbImg, depthImg, segImg = p.getCameraImage(width=320, height=240, viewMatrix=viewMatrix,
  85. projectionMatrix=p.computeProjectionMatrixFOV(fov,
  86. aspect,
  87. near, far))
  88. # Convert the depth image to a point cloud
  89. points = np.zeros((height * width, 3))
  90.  
  91. for y in range(height):
  92. for x in range(width):
  93. depth = depthImg[y, x]
  94. if depth != 0:
  95. points[y * width + x, 0] = (x - 160) * depth / 160.0
  96. points[y * width + x, 1] = (y - 120) * depth / 120.0
  97. points[y * width + x, 2] = depth
  98.  
  99. convergence_eps(5, 300, 1000, 0.1, 1.001, points=points)
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment