当前位置:   article > 正文

聚类+分类器python代码_聚类分类python代码

聚类分类python代码
  1. from sklearn.cluster import DBSCAN
  2. import matplotlib.pyplot as plt
  3. from matplotlib.patches import Rectangle
  4. import open3d as o3d
  5. import numpy as np
  6. # Reading data
  7. inputPath = r'E:\迅雷下载\kitti-velodyne-viewer-matlab-main\training\velodyne\000020.bin'
  8. num = np.fromfile(inputPath, dtype='float32', count=-1, sep='', offset=0)
  9. new = np.asarray(num).reshape(-1, 4)
  10. # Assigning data to different variables
  11. X = num[0::4]
  12. Y = num[1::4]
  13. Z = num[2::4]
  14. W = num[3::4]
  15. # Creating point cloud
  16. xyz = np.zeros((np.size(X), 3))
  17. xyz[:, 0] = X
  18. xyz[:, 1] = Y
  19. xyz[:, 2] = Z
  20. pc = o3d.geometry.PointCloud()
  21. pc.points = o3d.utility.Vector3dVector(xyz)
  22. # Downsampling
  23. downpcd = pc.voxel_down_sample(voxel_size=0.4) # Adjust voxel size
  24. # Segmentation
  25. plane_model, inliers = downpcd.segment_plane(distance_threshold=1, ransac_n=3, num_iterations=1000) # Adjust distance_threshold
  26. [a, b, c, d] = plane_model
  27. print(f"Plane equation: {a:.2f}x + {b:.2f}y + {c:.2f}z + {d:.2f} = 0")
  28. inlier_cloud = downpcd.select_by_index(inliers)
  29. outlier_cloud = downpcd.select_by_index(inliers, invert=True)
  30. # Clustering using DBSCAN
  31. clustering = DBSCAN(eps=2, min_samples=10).fit(np.asarray(outlier_cloud.points))
  32. labels = clustering.labels_
  33. max_label = labels.max()
  34. print(f"point cloud has {max_label + 1} clusters")
  35. # Visualization with bounding boxes
  36. fig = plt.figure()
  37. ax = fig.add_subplot(111)
  38. car_cluster_labels = []
  39. for cluster_label in range(max_label + 1):
  40. cluster_points = np.asarray(outlier_cloud.points)[labels == cluster_label]
  41. if len(cluster_points) > 0:
  42. min_x, min_y, min_z = np.min(cluster_points, axis=0)
  43. max_x, max_y, max_z = np.max(cluster_points, axis=0)
  44. width = max_x - min_x
  45. height = max_y - min_y
  46. depth = max_z - min_z
  47. volume = width * height * depth
  48. # Assume a car's volume is between 2 and 10 m^3 and z range between -1.5 and -0.5
  49. if 2 < volume < 10 and -1.5 < min_z < -0.5:
  50. car_cluster_labels.append(cluster_label)
  51. rect = Rectangle((min_x, min_y), width, height, linewidth=1, edgecolor='r', facecolor='none')
  52. ax.add_patch(rect)
  53. ax.text(min_x + width / 2, min_y + height / 2, 'car', color='blue', ha='center', va='center')
  54. # Plot points with different colors for each cluster
  55. ax.scatter(cluster_points[:, 0], cluster_points[:, 1], s=5, label=f"Cluster {cluster_label} - Car" if cluster_label in car_cluster_labels else f"Cluster {cluster_label}")
  56. print(f"Found {len(car_cluster_labels)} car clusters")
  57. # Show the legend to differentiate clusters
  58. ax.legend()
  59. # Show the 2D plot with bounding boxes
  60. ax.set_aspect('equal', adjustable='box')
  61. plt.show()
  62. # Show the 3D point cloud with bounding boxes
  63. o3d.visualization.draw_geometries([outlier_cloud])

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号