【发布时间】:2019-02-23 12:09:12
【问题描述】:
如何在 Python 中计算凸包的周长?我知道SciPy 有用于凸包的area 参数;但是,我需要perimeter。
【问题讨论】:
-
使用SciPy依次找到凸包的顶点,然后遍历顶点,自己计算周长。
标签: python convex-hull
如何在 Python 中计算凸包的周长?我知道SciPy 有用于凸包的area 参数;但是,我需要perimeter。
【问题讨论】:
标签: python convex-hull
您可以遍历凸包的点并计算连续点之间的距离:
import numpy as np
from scipy.spatial.qhull import ConvexHull
from scipy.spatial.distance import euclidean
points = np.random.rand(30, 2)
hull = ConvexHull(points)
vertices = hull.vertices.tolist() + [hull.vertices[0]]
perimeter = np.sum([euclidean(x, y) for x, y in zip(points[vertices], points[vertices][1:])])
print(perimeter)
输出
3.11
注意:您还需要添加对(最后一个,第一个)
更新
作为替代方案,假设数据是二维的,您可以使用hull.area。即上述方法中返回的值等于 area 属性的值。如果要真正的区域,需要查询hull.volume。
进一步
【讨论】:
hull.area返回周长。
Scipy中使用的概念不同。