[英]Perimeter of a 2D convex hull in Python
How can I calculate the perimeter of a convex hull in Python? 如何在Python中计算凸包的周长? I know
SciPy
has the area
parameter for convex hulls; 我知道
SciPy
具有凸包的area
参数; however, I need the perimeter
. 但是,我需要
perimeter
。
You can iterate over the points of the convex hull and compute the distance between consecutive points: 您可以遍历凸包的点并计算连续点之间的距离:
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)
Output 产量
3.11
Note: You also need to add the pair (last, first) 注意:您还需要添加对(最后一个,第一个)
UPDATE UPDATE
As an alternative, given that the data is 2D, you can use hull.area
. 或者,假设数据为2D,则可以使用
hull.area
。 That is the value returned in the above method is equal to the value of the area property. 也就是说,上述方法返回的值等于area属性的值。 If you want the real area , you need to query
hull.volume
. 如果需要真实区域 ,则需要查询
hull.volume
。
Further 进一步
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.