我在3D中有大约50,000个数据点,我从新的scipy (我使用的是0.10)运行了scipy.spatial.Delaunay,这给了我一个非常有用的三角测量。
基于:http://en.wikipedia.org/wiki/Delaunay_triangulation (“与Voronoi图的关系”一节)
...I想知道是否有一种简单的方法可以到达这个三角剖分的“对偶图”,这就是Voronoi Tesselation。
有什么线索吗?我在这方面的搜索似乎没有显示任何预置的scipy函数,我发现这几乎是奇怪的!
谢谢,爱德华
发布于 2012-05-19 01:18:09
邻接信息可以在Delaunay对象的neighbors属性中找到。不幸的是,代码目前没有向用户公开圆心,因此您必须自己重新计算这些圆心。
而且,延伸到无穷远的Voronoi边不是以这种方式直接获得的。这仍然是可能的,但需要更多的思考。
import numpy as np
from scipy.spatial import Delaunay
points = np.random.rand(30, 2)
tri = Delaunay(points)
p = tri.points[tri.vertices]
# Triangle vertices
A = p[:,0,:].T
B = p[:,1,:].T
C = p[:,2,:].T
# See http://en.wikipedia.org/wiki/Circumscribed_circle#Circumscribed_circles_of_triangles
# The following is just a direct transcription of the formula there
a = A - C
b = B - C
def dot2(u, v):
return u[0]*v[0] + u[1]*v[1]
def cross2(u, v, w):
"""u x (v x w)"""
return dot2(u, w)*v - dot2(u, v)*w
def ncross2(u, v):
"""|| u x v ||^2"""
return sq2(u)*sq2(v) - dot2(u, v)**2
def sq2(u):
return dot2(u, u)
cc = cross2(sq2(a) * b - sq2(b) * a, a, b) / (2*ncross2(a, b)) + C
# Grab the Voronoi edges
vc = cc[:,tri.neighbors]
vc[:,tri.neighbors == -1] = np.nan # edges at infinity, plotting those would need more work...
lines = []
lines.extend(zip(cc.T, vc[:,:,0].T))
lines.extend(zip(cc.T, vc[:,:,1].T))
lines.extend(zip(cc.T, vc[:,:,2].T))
# Plot it
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
lines = LineCollection(lines, edgecolor='k')
plt.hold(1)
plt.plot(points[:,0], points[:,1], '.')
plt.plot(cc[0], cc[1], '*')
plt.gca().add_collection(lines)
plt.axis('equal')
plt.xlim(-0.1, 1.1)
plt.ylim(-0.1, 1.1)
plt.show()发布于 2014-02-04 22:17:20
由于我在这方面花了相当多的时间,我想分享我的解决方案,关于如何获得Voronoi多边形而不仅仅是边。
代码在https://gist.github.com/letmaik/8803860上,并在tauran解决方案上进行了扩展。
首先,我更改了代码,将顶点和(成对的)索引(=边)分开给出,因为当处理索引而不是点坐标时,许多计算都可以简化。
然后,在voronoi_cell_lines方法中,我确定哪些边属于哪些单元格。为此,我使用了一个相关问题中提出的Alink解决方案。也就是说,对于每条边,找到两个最近的输入点(=cell),并从中创建一个映射。
最后一步是创建实际的多边形(请参见voronoi_polygons方法)。首先,需要关闭具有悬垂边缘的外部单元格。这就像查看所有边并检查哪些边只有一个相邻边一样简单。可以有零个或两个这样的边。如果是两个,我会通过引入额外的边将它们连接起来。
最后,每个单元中的无序边需要按正确的顺序排列,以便从它们派生多边形。
其用法为:
P = np.random.random((100,2))
fig = plt.figure(figsize=(4.5,4.5))
axes = plt.subplot(1,1,1)
plt.axis([-0.05,1.05,-0.05,1.05])
vertices, lineIndices = voronoi(P)
cells = voronoi_cell_lines(P, vertices, lineIndices)
polys = voronoi_polygons(cells)
for pIdx, polyIndices in polys.items():
poly = vertices[np.asarray(polyIndices)]
p = matplotlib.patches.Polygon(poly, facecolor=np.random.rand(3,1))
axes.add_patch(p)
X,Y = P[:,0],P[:,1]
plt.scatter(X, Y, marker='.', zorder=2)
plt.axis([-0.05,1.05,-0.05,1.05])
plt.show()以下哪项输出:

该代码可能不适用于大量输入点,可以在某些方面进行改进。然而,它可能会对其他有类似问题的人有所帮助。
发布于 2013-04-03 17:27:58
我遇到了同样的问题,并用pv的答案和我在网上找到的其他代码片段构建了一个解决方案,该解决方案返回了一个完整的Voronoi图,其中包括没有三角形邻居的外边线。
#!/usr/bin/env python
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
def voronoi(P):
delauny = Delaunay(P)
triangles = delauny.points[delauny.vertices]
lines = []
# Triangle vertices
A = triangles[:, 0]
B = triangles[:, 1]
C = triangles[:, 2]
lines.extend(zip(A, B))
lines.extend(zip(B, C))
lines.extend(zip(C, A))
lines = matplotlib.collections.LineCollection(lines, color='r')
plt.gca().add_collection(lines)
circum_centers = np.array([triangle_csc(tri) for tri in triangles])
segments = []
for i, triangle in enumerate(triangles):
circum_center = circum_centers[i]
for j, neighbor in enumerate(delauny.neighbors[i]):
if neighbor != -1:
segments.append((circum_center, circum_centers[neighbor]))
else:
ps = triangle[(j+1)%3] - triangle[(j-1)%3]
ps = np.array((ps[1], -ps[0]))
middle = (triangle[(j+1)%3] + triangle[(j-1)%3]) * 0.5
di = middle - triangle[j]
ps /= np.linalg.norm(ps)
di /= np.linalg.norm(di)
if np.dot(di, ps) < 0.0:
ps *= -1000.0
else:
ps *= 1000.0
segments.append((circum_center, circum_center + ps))
return segments
def triangle_csc(pts):
rows, cols = pts.shape
A = np.bmat([[2 * np.dot(pts, pts.T), np.ones((rows, 1))],
[np.ones((1, rows)), np.zeros((1, 1))]])
b = np.hstack((np.sum(pts * pts, axis=1), np.ones((1))))
x = np.linalg.solve(A,b)
bary_coords = x[:-1]
return np.sum(pts * np.tile(bary_coords.reshape((pts.shape[0], 1)), (1, pts.shape[1])), axis=0)
if __name__ == '__main__':
P = np.random.random((300,2))
X,Y = P[:,0],P[:,1]
fig = plt.figure(figsize=(4.5,4.5))
axes = plt.subplot(1,1,1)
plt.scatter(X, Y, marker='.')
plt.axis([-0.05,1.05,-0.05,1.05])
segments = voronoi(P)
lines = matplotlib.collections.LineCollection(segments, color='k')
axes.add_collection(lines)
plt.axis([-0.05,1.05,-0.05,1.05])
plt.show()黑线= Voronoi图,红线= Delauny三角形

https://stackoverflow.com/questions/10650645
复制相似问题