使用凸包获取矩形的边界(python)

Getting the boundary of a rectangle using the convex hull (in 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" 稍微扰动点,如下所示:

 hull = ConvexHull(points2D, qhull_options="QJ Pp")

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

有人可以告诉我正确的解决方法吗?

凸包状态的数学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。这 returns 个面的顶点形成了三角剖分点的凸包。在您的 2D 情况下,这些是线,输出是一组包含每条线的相邻点。请注意,这种方法被认为是低效的,因为除了凸包之外,它还需要形成三角剖分。

程序的其余部分提取每个点的 x 值和相应的 y 值,并将它们与三角测量结果一起绘制出来