上一篇博客讲了需求:需要在离散点中查找外部边框;
外部边框如图:
可以用Alpha shape 运算(https://blog.csdn.net/Janine_1991/article/details/121017287?spm=1001.2014.3001.5501);
但是当数据点过多时,Alpha shape 会被卡死,很久之后跑出空的结果
此时建议用concave Hull 进行运算;
安装地址:Concave
具体安装不再赘述,可能需要root 权限;
所以此时的代码是:
import numpy as np
import concave_hull
import matplotlib.pyplot as plt
points=np.load("drive/MyDrive/data/points.npy") #######我不用random 示意了,用一个图像数据集示意
## points.shape=(xx,2) ,二维x,y数据点
plt.scatter(*zip(*points))
plt.axis("off")
进行 concave hull 计算:
hull = concave_hull.compute(points, 15)
###15 是 knn的k_neighbors, k_neighbors 越大,得到的点数越少
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(*zip(*points))
ax.scatter(hull[:,0], hull[:,1], color='red')
plt.axis("off")
# plt.show()
当 k_neighbors 取 100时,结果为:
当k_neighbors为1000时:



