最远点采样(Farest Point Sampling)
import numpy as np
import matplotlib.pyplot as plt
class FarthestSample(object):
def __init__(self):
super(FarthestSample, self).__init__()
def __cal_distances(self, p_0, points):
return ((p_0 - points) ** 2).sum(axis=1)
def __call__(self, points, k):
farthest_pts = np.zeros((k, points.shape[1]), dtype=np.float32)
farthest_pts[0] = points[np.random.randint(len(points))]
distances = self.__cal_distances(farthest_pts[0], points)
for i in range(1, k):
farthest_pts[i] = points[np.argmax(distances)]
distances = np.minimum(distances, self.__cal_distances(farthest_pts[i], points))
# 此处更新得到的距离,已选点集中所有点到各个点的最小距离(所谓最远,是离现有所有的已选点最远->这个距离如何定义)
return farthest_pts
points = np.random.randn(100, 4) * 100
# plt.scatter(points[:,0], points[:, 1])
farthestsample = FarthestSample()
ds_points = farthestsample(points, 10)
print(ds_points.shape)