gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:22:40 25 4
gpt4 key购买 nike

我正在尝试使用 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 .这将返回构成三角剖分点凸包的小平面的顶点。在您的 2D 情况下,这些是线,输出是一组包含每条线的相邻点。请注意,这种方法被认为是低效的,因为除了凸包之外,它还需要形成三角剖分。

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

enter image description here

关于python - 使用凸包获取矩形的边界(在 python 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com