点云最远点采样FPS(Farthest Point Sampling)方法的优势是可以尽可能多地覆盖到全部点云,但是需要多次计算全部距离,因而属于复杂度较高的、耗时较多的采样方法。
1 最远点采样(FPS)采样步骤
FPS采样步骤为:
(1)选择一个初始点:可以随机选择,也可以按照一定的规则来选取。如果随机选取那么每次得到的结果都是不一样的,反之每次得到的结果就是一致的。
(2)计算所有点与(1)中点的距离,选择距离最大的值作为新的初始点。
(3)重复前两步过程,知道选择的点数量满足要求。
由于(2)中每次选择的距离都是最大的,所以迭代的过程距离最大值会逐渐减少。这也就是下面代码中mask选取的依据。如果把加这一个限制,那么点会被来回重复选到。
2 距离最远点采样(D-FPS)
上述最远点采样通过计算点与点的之间距离来进行取样。距离最远点采样是指计算点与点之间的坐标距离,通常取xyz三个坐标。
# -*- coding: utf-8 -*-
"""
乐乐感知学堂公众号
@author: https://blog.csdn.net/suiyingy
参考:https://github.com/yanx27/Pointnet_Pointnet2_pytorch
"""
import numpy as np
def farthest_point_sample(point, npoint):
"""
Input:
xyz: pointcloud data, [N, D]
npoint: number of samples
Return:
centroids: sampled pointcloud index, [npoint, D]
"""
N, D = point.shape
xyz = point[:,:3]
centroids = np.zeros((npoint,))
distance = np.ones((N,)) * 1e10
farthest = np.random.randint(0, N)
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
3 特征最远点采样(F-FPS)
上述最远点采样通过计算点与点的之间距离来进行取样。特征最远点采样是指计算点的特征之间的距离。假设每个点的坐标为xyz 3个维度,输入特征为M个维度。特征最远点采样计算距离时经常会将xyz坐标与特征进行拼接作为新的特征,然后计算特征之间的距离,以进行最远点采样。
# -*- coding: utf-8 -*-
"""
乐乐感知学堂公众号
@author: https://blog.csdn.net/suiyingy
参考:https://github.com/yanx27/Pointnet_Pointnet2_pytorch
"""
import numpy as np
def farthest_point_sample(point, npoint):
"""
Input:
xyz: pointcloud data, [N, D]
npoint: number of samples
Return:
centroids: sampled pointcloud index, [npoint, D]
"""
N, D = point.shape
xyz = point
centroids = np.zeros((npoint,))
distance = np.ones((N,)) * 1e10
farthest = np.random.randint(0, N)
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