【发布时间】:2019-05-10 20:03:18
【问题描述】:
我想根据中心列表和图像大小生成 Voronoi 区域。
我尝试了下一个代码,基于https://rosettacode.org/wiki/Voronoi_diagram
def generate_voronoi_diagram(width, height, centers_x, centers_y):
image = Image.new("RGB", (width, height))
putpixel = image.putpixel
imgx, imgy = image.size
num_cells=len(centers_x)
nx = centers_x
ny = centers_y
nr,ng,nb=[],[],[]
for i in range (num_cells):
nr.append(randint(0, 255));ng.append(randint(0, 255));nb.append(randint(0, 255));
for y in range(imgy):
for x in range(imgx):
dmin = math.hypot(imgx-1, imgy-1)
j = -1
for i in range(num_cells):
d = math.hypot(nx[i]-x, ny[i]-y)
if d < dmin:
dmin = d
j = i
putpixel((x, y), (nr[j], ng[j], nb[j]))
image.save("VoronoiDiagram.png", "PNG")
image.show()
我有想要的输出:
但是生成输出需要太多。
我也试过https://stackoverflow.com/a/20678647 它很快,但我没有找到将其转换为 img_width X img_height 的 numpy 数组的方法。主要是因为我不知道如何将图像大小参数提供给 scipy Voronoi class。
有没有更快的方法来获得这个输出?不需要中心或多边形边缘
提前致谢
2018 年 12 月 11 日编辑: 使用@tel“快速解决方案”
代码执行速度变快了,好像中心变了。可能这个方法是给图片加边距
【问题讨论】:
-
可以编译吗?
-
这是一种极其缓慢的算法,具有极其缓慢的语言。请参阅 docs.scipy.org/doc/scipy-0.18.1/reference/generated/… 来计算 Voronoi 和 matplotlib.org/api/_as_gen/matplotlib.pyplot.fill.html 来制作图像。
标签: python numpy scipy voronoi