【问题标题】:what do I get from scipy.spatial.Delaunay.convex_hull我从 scipy.spatial.Delaunay.convex_hull 得到什么
【发布时间】:2013-12-18 17:35:36
【问题描述】:

我认为 scipy.spatial.Delaunay.convex_hull 正在返回一个数组,其中每个点/索引都被使用两次,因为一个点属于两个边。但就我而言,只有一次有几个索引:

hull = [[5053 6943]
        [6219 5797]
        [3441 5797]
        [7547 1405]
        [3441 6547]
        [8144 9215]
        [  47  444]
        [ 444 6219]
        [6547 5053]
        [9945 6943]
        [2695 1405]]

例如“47”只使用一次。这是什么意思(几何上)?凸包的一点怎么只能用于一条边?

【问题讨论】:

  • Delaunay.convex_hull 包含 Qhull 报告为没有邻居的三角形边。如果三角剖分包含退化三角形,则该集合可能包含伪像。如果要获得数值稳定的凸包,请使用scipy.spatial.ConvexHull

标签: python scipy spatial convex-hull delaunay


【解决方案1】:

正如上面评论中提到的,您应该使用较新的scipy.spatial.ConvexHull。如果您在下面的 IPython 示例中查看此方法返回的顶点索引,您会发现它们对于 2D 或 3D 数据集通常不是冗余的。

%pylab inline
import numpy
#use a simple trianglular data set:
triangle_points = numpy.array([[0,0],[-1,1],[1,1]])

#plot the triangle:
fig = plt.figure()
ax = fig.add_subplot(111,aspect='equal')
ax.scatter(triangle_points[...,0],triangle_points[...,1])

#now calculate the convex hull of the triangular point set:
import scipy, scipy.spatial
convex_hull_2D = scipy.spatial.ConvexHull(triangle_points)
#determine the indices of the vertices forming the convex hull (i.e., the output you get with the older scipy version):
convex_hull_vertex_indices = convex_hull_2D.vertices
#print the array of convex hull vertex indices:
convex_hull_vertex_indices
array([2, 1, 0], dtype=int32) #output

#For this simple 2D data set it is clear that scipy can define a convex hull using the index of each input point only once, so it is not doing AB, BC, CA as you might initially guess

#Let's see what happens if we add a point outside the plane of the triangle and make the data set 3D:
tetrahedral_points = numpy.array([[0,0,0],[-1,1,0],[1,1,0],[0,0.5,3]]) #the last element is the 'out of plane' new point
convex_hull = scipy.spatial.ConvexHull(tetrahedral_points)
convex_hull_vertex_indices = convex_hull.vertices
convex_hull_vertex_indices
array([0, 1, 2, 3], dtype=int32) #output  

#again, for a simple shape, even in 3D, scipy specifies the indices of the vertices of the convex hull in a non-redundant manner

#take a look at the simplices of the 2D ConvexHull object:
convex_hull_simplices = convex_hull_2D.simplices
convex_hull_simplices
array([[2, 1],
       [0, 1],
       [0, 2]], dtype=int32) #output

#so the simplices do contain duplicate indices to connect points to form edges/1-simplices
fig = plt.figure()
ax = fig.add_subplot(111,aspect='equal')
ax.set_ylim(0,1.2)
edge_colors = ['blue','red','green'] #color each 1-simplex differently for clarity
for simplex, edge_color in zip(convex_hull_simplices,edge_colors):
    plt.plot(triangle_points[simplex,0], triangle_points[simplex,1], c=edge_color)

【讨论】:

  • 听起来不错,但我得到一个错误,不知道为什么:from scipy.spatial import ConvexHull import numpy working_points = numpy.array([[0,0],[-1,1],[1,1]]) conv_hull = ConvexHull(working_points) conv_hull_vert = conv_hull.vertices 我得到:AttributeError: 'ConvexHull' object has no attribute 'vertices'
  • 它对我有用——你可能必须使用更新版本的 scipy,因为这个版本的 ConvexHull 是相当新的(当你评论中的代码有效时,我使用 0.13.0)。我发现使用 pip 是调整安装的 scipy 版本的最简单方法:pip install 'scipy==0.13.0' 然后import scipy scipy.__version__ 以确保 Python 正在访问正确的版本。
  • 谢谢。我知道我有 0.12.0 版本,但我无法在 atm 测试它,稍后会尝试。但我还有最后一个问题:ConvexHull().verticesConvexHull().simplices 在 2D 中有什么区别?
  • 我现在也包含了一个简单的例子。您可以看到这里有重复的索引,因为这些是正确的边缘。这在此处的标准文档中也有所描述:docs.scipy.org/doc/scipy-dev/reference/generated/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 2021-08-04
相关资源
最近更新 更多