用凸壳求矩形的边界(python语言)

2024-06-16 10:33:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用scipy.ConvexHull()得到一个矩形的边界,但没有成功。在

u=np.linspace(0, 4, 8)
v=np.linspace(5, 10, 8)
u,v=np.meshgrid(u,v)
u=u.flatten()
v=v.flatten()
points2D=np.vstack([u,v]).T


hull = ConvexHull(points2D)
convex_hull_plot_2d(hull)
boundaryList = hull.vertices
print boundaryList

只给出四个角:[ 0 7 63 56]

使用选项qhull_options="QJ Pp"稍微扰动点,如下所示:

^{pr2}$

给出更多的点:[62 56 40 8 0 2 6 7 15 23 47 55 63],但仍然不是完整的边界集。在

有人能告诉我一个合适的方法吗?在


Tags: plotnpscipy边界矩形flattenlinspaceconvex
1条回答
网友
1楼 · 发布于 2024-06-16 10:33:26

凸壳态的数学definition

In mathematics, the convex hull or convex envelope of a set X of points in the Euclidean plane or in a Euclidean space (or, more generally, in an affine space over the reals) is the smallest convex set that contains X.

包含一个矩形的最小凸集就是你得到的四个角。在

要获得边界上的所有点,可以使用Delaunay triangulation,然后根据生成的Delaunay网格计算凸包

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay

u=np.linspace(0, 4, 8)
v=np.linspace(5, 10, 8)
u,v=np.meshgrid(u,v)
u=u.flatten()
v=v.flatten()
points2D=np.vstack([u,v]).T

tri = Delaunay(points2D)
plt.triplot(points2D[:,0], points2D[:,1], tri.simplices.copy())

boundary = (points2D[tri.convex_hull]).flatten()
bx = boundary[0:-2:2]
by = boundary[1:-1:2]

plt.plot(points2D[:,0], points2D[:,1], 'o')
plt.plot(bx, by, 'rs')

plt.xlim(-1,5)
plt.ylim(4,11)
plt.show()

为了在形成三角剖分后创建外壳,程序使用tri.convex_hull。这将返回构成三角剖分点的凸壳的面顶点。在你的,二维的情况下,这些是线和输出出来作为一组相邻的点组成每一条线。注意,这种方法被认为是低效的,因为它需要形成除凸包外的三角剖分。在

程序的其余部分提取每个点的x和相应的y值,并将它们与生成的三角剖分一起绘制

enter image description here

相关问题 更多 >