在更新中。。。
一、对点云数据的聚类处理
这里参考了微博用Python打造无人驾驶车-激光雷达数据(1) - 知乎 (zhihu.com),这篇博文的聚类算法的分类有一些缺陷,所以本人对其代码稍微的修改,具体代码如下:
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
%matplotlib
def cluster(points, radius=0.2):
"""
points: pointcloud
radius: max cluster range
"""
items = []
while len(points)>1:
item = np.array([points[0]])
base = points[0]
points = np.delete(points, 0, 0)
distance = (points[:,0]-base[0])**2+(points[:,1]-base[1])**2
infected_points = np.where(distance <= radius**2)
item = np.append(item, points[infected_points], axis=0)
border_points = points[infected_points]
points = np.delete(points, infected_points, 0)
while len(border_points) > 0:
border_base = border_points[0]
border_points = np.delete(border_points, 0, 0)
border_distance = (points[:,0]-border_base[0])**2+(points[:,1]-border_base[1])**2
border_infected_points = np.where(border_distance <= radius**2)
item = np.append(item, points[border_infected_points], axis=0)
#for循环替代了原博文下面注释的一行
for k in border_infected_points:
if points[k] not in border_points:
border_points=np.append(border_points,points[k], axis=0)
#border_points = points[border_infected_points]
points = np.delete(points, border_infected_points, 0)
items.append(item)
return items
if __name__ == '__main__':
points=[]#这里的points就是我们前一章获得的点云,返回的item
fig=cluster(points, radius=0.2)
fig = plt.figure()
ax = Axes3D(fig)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(item[:,0], item[:,1], item[:,2], s=1)
fig.show()