Python 中的 Voronoi 曲面细分

发布于 2024-12-18 18:05:15 字数 447 浏览 2 评论 0 原文

节点分配问题

在此处输入图像描述

我要解决的问题是用蓝色节点(源节点)给定的地图进行细分)作为给定的输入点,一旦我能够做到这一点,我想查看每个单元格内有多少黑色节点(需求节点)并将其分配给与该单元格关联的蓝色节点。

我想知道是否有一种更简单的方法可以在不使用财富算法的情况下完成此操作。我在 Mahotas 下遇到了这个名为 Mahotas.segmentation.gvoronoi(image) 的函数来源。但我不确定这是否能解决我的问题。

另外请建议我是否有更好的方法来进行此分割(除了 Voronoi 曲面细分)。我不确定聚类算法是否是一个不错的选择。我是一个编程新手。

Node Assignment Problem

enter image description here

The problem I want to solve is to tessellate the map given with the Blue Nodes(Source Nodes) as given input points, Once I am able to do this I would like to see how many Black Nodes(Demand Nodes) fall within each cell and assign it to the Blue Node associated with that cell.

I would like to know if there is a easier way of doing this without using Fortune's Algorithm.I came across this function under Mahotas called Mahotas.segmentation.gvoronoi(image)source. But I am not sure if this will solve my problem.

Also please suggest me if there is a better way of doing this segmentation(other than Voronoi tessellation). I am not sure if clustering algorithms would be a good choice. I am a programming newbie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

洋洋洒洒 2024-12-25 18:05:16

你没有说为什么你想避开《财富》的算法。我假设您的意思是您只是不想自己实现它,但是 Bill Simons 和 Carston Farmer 已经在脚本中实现了它,因此计算 voronoi 图应该不难。

在他们的脚本的基础上,我让它更容易使用,并将其上传到 PyPi,名称为 Pytess.因此,您可以使用基于蓝点的 pytess.voronoi() 函数作为输入,返回原始点及其计算的 voronoi 多边形。然后,您必须通过多边形内的点测试来分配每个黑点,您可以基于 http://geospatialpython.com/2011/08/point-in-polygon-2-on-line.html

在此处输入图像描述

You did not say why you wanted to avoid Fortune's algorithm. I assume you meant that you just didn't want to implement it yourself, but it has already been implemented in a script by Bill Simons and Carston Farmer so computing the voronoi diagram shouldn't be difficult.

Building on their script I made it even easier to use and uploaded it to PyPi under the name Pytess. So you could use the pytess.voronoi() function based on the blue points as input, returning the original points with their computed voronoi polygons. Then you would have to assign each black point through point-in-polygon testing, which you could base on http://geospatialpython.com/2011/08/point-in-polygon-2-on-line.html.

enter image description here

不甘平庸 2024-12-25 18:05:16

在 Mathematica 中运行此代码。太壮观了! (是的,我知道它不是 Python,但是...)

pts3 = RandomReal[1, {50, 3}];
ListDensityPlot[pts3, 
    InterpolationOrder -> 0, ColorFunction -> "SouthwestColors", Mesh -> All]

       Voronoi 图

Run this code in Mathematica. It's spectacular! (Yes, I know it is not Python, but ...)

pts3 = RandomReal[1, {50, 3}];
ListDensityPlot[pts3, 
    InterpolationOrder -> 0, ColorFunction -> "SouthwestColors", Mesh -> All]

          Voronoi Diagram

温柔女人霸气范 2024-12-25 18:05:15

这是使用 Voronoi 曲面细分的另一种方法:

在源节点上构建 kd 树。然后,对于每个需求节点,使用 kd 树查找最近的源节点并递增与该附近源节点关联的计数器。

kd 树的实现位于 http://code.google.com/p/python- kdtree/ 应该有用。

Here is an alternative approach to using Voronoi tessellation:

Build a k-d tree over the source nodes. Then for every demand node, use the k-d tree to find the nearest source node and increment a counter associated with that nearby source node.

The implementation of a k-d tree found at http://code.google.com/p/python-kdtree/ should be useful.

世界和平 2024-12-25 18:05:15

我一直在寻找同样的东西,发现了这个:

https://github.com/Softbass/py_geo_voronoi

I've just been looking for the same thing and found this:

https://github.com/Softbass/py_geo_voronoi

猫烠⑼条掵仅有一顆心 2024-12-25 18:05:15

你的图中没有太多点。这表明,对于每个需求节点,您可以迭代所有源节点并找到最近的一个。

也许是这样的:

def distance(a, b):
    return sum((xa - xb) ** 2 for (xa, xb) in zip(a, b))

def clusters(sources, demands):
    result = dict((source, []) for source in sources)
    for demand in demands:
        nearest = min(sources, key=lambda s: distance(s, demand))
        result[nearest].append(demand)
    return result

这段代码将为您提供一个字典,将源节点映射到所有需求节点的列表,这些节点比任何其他节点更接近该源节点。

这不是特别有效,但非常简单!

There's not many points in your diagram. That suggests you can, for each demand node, just iterate through all the source nodes and find the nearest one.

Perhaps this:

def distance(a, b):
    return sum((xa - xb) ** 2 for (xa, xb) in zip(a, b))

def clusters(sources, demands):
    result = dict((source, []) for source in sources)
    for demand in demands:
        nearest = min(sources, key=lambda s: distance(s, demand))
        result[nearest].append(demand)
    return result

This code will give you a dictionary, mapping source nodes to a list of all demand nodes which are closer to that source node than any other.

This isn't particularly efficient, but it's very simple!

喜爱纠缠 2024-12-25 18:05:15

我认为 https://stackoverflow.com/users/1062447/wye-bee 的空间索引答案(A例如 kd-tree)是解决您的问题的最简单的解决方案。

此外,您还问是否有比《财富》算法更简单的替代方案,对于这个特定问题,我建议您参考:最容易实现的 Voronoi 图算法?

I think the spatial index answer by https://stackoverflow.com/users/1062447/wye-bee (A kd-tree for example) is the easiest solution to your problem.

Additionally, you did also ask is there an easier alternative to Fortune's algorithm and for that particular question I refer you to: Easiest algorithm of Voronoi diagram to implement?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文