我正在生成一个简单的 2D Voronoi 曲面分割,使用 scipy.spatial.Voronoi功能。我使用点的随机二维分布(参见下面的 MCVE)。
我需要一种方法来遍历每个定义的区域(由 scipy.spatial.Voronoi
定义)并获取与其关联的点的坐标(即:所述区域包含的点)。
问题是为 N
点定义了 N+1
区域(多边形),我不确定这意味着什么。
这是一个 MCVE,当它到达最后一个区域时会失败:
from scipy.spatial import Voronoi
import numpy as np
# Generate random data.
N = 10
x = [np.random.random() for i in xrange(N)]
y = [np.random.random() for i in xrange(N)]
points = zip(x, y)
# Obtain Voronoi regions.
vor = Voronoi(points)
# Loop through each defined region/polygon
for i, reg in enumerate(vor.regions):
print 'Region:', i
print 'Indices of vertices of Voronoi region:', reg
print 'Associated point:', points[i], '\n'
我不明白的另一件事是为什么存储了空的vor.regions
?根据文档:
regions: Indices of the Voronoi vertices forming each Voronoi region. -1 indicates vertex outside the Voronoi diagram.
空白区域是什么意思?
添加
我尝试了 point_region
属性,但显然我不明白它是如何工作的。它返回 points
列表范围之外的索引。例如:在上面的 MCVE 中,对于 10 个点的列表,它将始终显示索引 10
,这显然超出了范围。
最佳答案
第一个问题:
The issue is that there are N+1 regions (polygons) defined for the N points, and I'm not sure what this means.
这是因为你的 vor.regions 总是有一个空数组。 有点像
[[],[0, 0],[0, 1],[1, 1]]
这与你的第二个问题有关:
Another thing I don't understand is why are there empty vor.regions stored? According to the docs: regions: Indices of the Voronoi vertices forming each Voronoi region. -1 indicates vertex outside the Voronoi diagram. What does an empty region mean?
默认情况下,Voronoi() 使用启用了选项“Qbb Qc Qz Qx”的 QHull (qhull.org/html/qvoronoi.htm)。这会插入一个“无限远点”,用于提高循环输入的精度。因此,作为一个“假”点,它没有区域。如果你想摆脱这个,尝试删除 Qz 选项:
vor = Voronoi(points, qhull_options='Qbb Qc Qx')
关于python - 获取与 Voronoi 区域关联的点 (scipy.spatial.Voronoi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32019800/