【问题标题】:Perimeter of a 2D convex hull in PythonPython中二维凸包的周长
【发布时间】:2019-02-23 12:09:12
【问题描述】:

如何在 Python 中计算凸包的周长?我知道SciPy 有用于凸包的area 参数;但是,我需要perimeter

【问题讨论】:

  • 使用SciPy依次找到凸包的顶点,然后遍历顶点,自己计算周长。

标签: python convex-hull


【解决方案1】:

您可以遍历凸包的点并计算连续点之间的距离:

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

进一步

  1. What is area in scipy convex hull

【讨论】:

  • 我更新了答案,基本上是二维数据hull.area返回周长。
  • 有趣的是Scipy中使用的概念不同。
猜你喜欢
  • 2019-02-23
  • 2011-03-06
  • 2013-10-09
  • 2021-11-26
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 2013-11-21
  • 2017-06-28
相关资源
最近更新 更多