python - 如何将源和目标参数定义为shortest_path的数组?

标签 python numpy opencv networkx shortest-path

我正在使用NetworkX,opencv,numpy和python在图中找到shortest_path。它并不总是能提供我所需要的。 shortest_path函数查找从图像顶部到底部的路径。我的路总是在变化。每个图像始终知道起点和目标点。因此,我想找到这些点(起点和目标)之间的最短路径。但是,当源点和目标点不是节点且不在G中时,它并不能满足我的需求。

shortest_path(G, source=None, target=None, weight=None)

如何找到图像中两个特定坐标点之间的shortest_path?此外,如何分配像素坐标作为源和目标?例如,源是[45 66],目标是[250 350]

最佳答案

对于第二个问题,节点可以具有所需的尺寸。
例如,考虑以下网格。

import networkx as nx

g = nx.grid_2d_graph(2,2)

print(g.nodes()) #[(0, 0), (0, 1), (1, 0), (1, 1)]
print(g.edges()) #[((0, 0), (1, 0)), ((0, 0), (0, 1)), ((0, 1), (1, 1)), ((1, 0), (1, 1))]
print(nx.shortest_path(g, source=(0, 0), target=(1,0))) #[(0, 0), (1, 0)]

甚至更大的尺寸:
g = nx.grid_graph(dim=[2,2,2,2])
g.nodes()
#[(0, 0, 0, 0), (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1), (1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 0, 1, 0), (0, 1, 1, 0), (1, 0, 1, 1), (0, 1, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)]

对于其他问题:
  • 我的路径总是变化的:它取决于边缘的权重,如果最短路径不是唯一的,则更改
  • 是正常的
    当源点和目标点不是节点且不在G中时
  • :networkx在G的2个节点之间找到最短路径,如果节点不在G中,它将不起作用
  • 关于python - 如何将源和目标参数定义为shortest_path的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174391/

    相关文章:

    python - 使用 Python 删除字母图像中的残留物

    python - 为什么在 Python 中对一些大整数进行除法和乘法会返回奇怪的结果?

    python - 如何从列表中随机选择连续样本?

    python - 在centos 6上安装Opencv 3.0.0

    python - 获取多索引中级别的最后一个元素

    python - 在 numpy 中使用多个排序键进行排序

    python - 图像噪声处理和边缘方向确定

    Python Pandas 科学记数法不一致

    Python Boto3 'StreamingBody' 对象没有属性 'iter_lines'

    python - 在opencv python中进行模板匹配时出现TypeError