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

问题描述 投票:0回答:1

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

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

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

python numpy opencv networkx shortest-path
1个回答
0
投票

对于第二个问题,节点可以包含您想要的维数。 例如,考虑以下网格。

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中则不起作用
© www.soinside.com 2019 - 2024. All rights reserved.