FPS算法原理:
1.输入点云有N个点,从N个点中任意选取一个点P0作为初始点。
2.计算所有点到P0的距离,构成N维数组,从中选择距离最大值点P1,更新采样点集合B={P0,P1}。
3.计算所有点到集合B中每个点的距离,将最小距离作为该点到集合的距离,选取最大距离作为采样点,更新采样点集合B。
4.重复2-3步,直至采样点数量满足要求。
如图所示:
import numpy
def farthest_point_sample(point,npoint):
N,D = point.shape
xyz = point[:,0:3]
farthest = np.random.randint(0,N)
centroids = np.zeros((npoint,))
dist = np.ones((N,)) * 1e10
for i in range(npoint):
centroids[i] = farthest
centroid = xyz[farthest,:]
dist = np.sum((xyz-centroid)**2,-1)
mask = dist < distance
distance[mask] = dist[mask]
farthest = np.argmax(distance,-1)
point = point[centroids.astype(np.int32)]
return point