rospy.Subscriber("/velodyne_points", PointCloud2, self.lidar_callback)
def lidar_callback(self, msg):
pc_data = pc2.read_points(msg, field_names=("x", "y", "z"), skip_nans=True)
points = np.array(list(pc_data))
horizontal_angles = np.arctan2(points[:, 1], points[:, 0])
angle_filtered_points = points[(horizontal_angles >= self.min_horizontal_angle) & (horizontal_angles <= self.max_horizontal_angle)]
distances = np.sqrt(angle_filtered_points[:, 0]**2 + angle_filtered_points[:, 1]**2)
desired_distance_min, desired_distance_max = 2.0, 4.0
filtered_points = angle_filtered_points[(distances >= desired_distance_min) & (distances <= desired_distance_max), :]
clustered_points = self.cluster_points(filtered_points)
def cluster_points(self, points):
clusterer = hdbscan.HDBSCAN(min_cluster_size=25, min_samples=1, allow_single_cluster=True)
labels = clusterer.fit_predict(points)
unique_labels = np.unique(labels)
clustered_points = [points[labels == label] for label in unique_labels]
return clustered_points
HDBSCAN은 "Hierarchical Density-Based Spatial Clustering of Applications with Noise"의 약자로, 밀도 기반 공간 클러스터링 알고리즘의 한 종류입니다. HDBSCAN은 주어진 데이터에서 밀도가 높은 클러스터를 식별하는 데 사용되며, 클러스터의 모양과 크기에 덜 민감하게 작동합니다.
bounding_boxes, self.bounding_box_info = self.generate_bounding_boxes(clustered_points, header)
def generate_bounding_boxes(self, clusters, header):
marker = Marker()
# 중간 생략
object_positions = []
for i, cluster in enumerate(clusters):
# 중간 생략
object_positions.append({
'center': center,
'extent': extent
})
return marker, object_positions