Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用“在图形中遍历以找到源和目标之间的最短路径;迪克斯特拉';s算法&x201D;用python?_Python_Image Processing_Dijkstra - Fatal编程技术网

如何使用“在图形中遍历以找到源和目标之间的最短路径;迪克斯特拉';s算法&x201D;用python?

如何使用“在图形中遍历以找到源和目标之间的最短路径;迪克斯特拉';s算法&x201D;用python?,python,image-processing,dijkstra,Python,Image Processing,Dijkstra,因此,我有实现Dijkstra算法的代码,该算法在Python3(Python3.8.1)中找到加权图中从源节点到目标节点的最短路径 这就是我想要解决的问题: 也可以通过计算从源到目标的白色像素数来找到最短路径吗 代码: import itertools import matplotlib.pyplot as plt from scipy import ndimage from scipy import misc from scipy.sparse.dok import dok_mat

因此,我有实现Dijkstra算法的代码,该算法在Python3(Python3.8.1)中找到加权图中从源节点到目标节点的最短路径

这就是我想要解决的问题:

也可以通过计算从源到目标的白色像素数来找到最短路径吗

代码:

import itertools
import matplotlib.pyplot as plt
from scipy import ndimage
from scipy import misc
from scipy.sparse.dok import dok_matrix
from scipy.sparse.csgraph import dijkstra

# Load the image from disk as a numpy ndarray
original_img = plt.imread('map.bmp')

# Create a flat color image for graph building:
img = original_img[:, :, 0] + original_img[:, :, 1] + original_img[:, :, 2]


# Defines a translation from 2 coordinates to a single number
def to_index(y, x):
    return y * img.shape[1] + x


# Defines a reversed translation from index to 2 coordinates
def to_coordinates(index):
    return index / img.shape[1], index % img.shape[1]


# A sparse adjacency matrix.
# Two pixels are adjacent in the graph if both are painted.
adjacency = dok_matrix((img.shape[0] * img.shape[1],
                        img.shape[0] * img.shape[1]), dtype=bool)

# The following lines fills the adjacency matrix by
directions = list(itertools.product([0, 1, -1], [0, 1, -1]))
for i in range(1, img.shape[0] - 1):
    for j in range(1, img.shape[1] - 1):
        if not img[i, j]:
            continue

        for y_diff, x_diff in directions:
            if img[i + y_diff, j + x_diff]:
                adjacency[to_index(i, j),
                          to_index(i + y_diff, j + x_diff)] = True

# We chose two arbitrary points, which we know are connected
source = to_index(14, 47)
target = to_index(151, 122)

# Compute the shortest path between the source and all other points in the image
_, predecessors = dijkstra(adjacency, directed=False, indices=[source],
                           unweighted=True, return_predecessors=True)

# Constructs the path between source and target
pixel_index = target
pixels_path = []
while pixel_index != source:
    pixels_path.append(pixel_index)
    pixel_index = predecessors[0, pixel_index]


# The following code is just for debugging and it visualizes the chosen path


plt.imshow(original_img)
plt.show()
输入

输出

我需要弄清楚,请帮帮我


谢谢

您可以通过检查每个像素将图像转换为图形,如果是白色,如果是,它的相邻像素是什么。您能详细说明一下吗,我是这个领域的新手。而且我正在使用枕头进行图像处理,mathplotlib进行打印。我无法在现有图形上绘制路径,我该怎么做?嗨!欢迎来到SO。请在代码示例中包含您自己的尝试。以及我们需要帮助您的任何输入和输出(以可编辑的形式)。