python-2.7 - 如何在骨架化的迷宫图像中找到最短路径?

标签 python-2.7 opencv image-processing networkx image-morphology

我正在使用图像处理NetworkX 搜索算法解决迷宫问题,需要找到这些线上两点之间的最短连接路径。

#Solving Maze Using Image Processing and NetWorkx search



    #Open Maze image
    img = cv2.imread("C:/Users/Dell/HandMadeMaze1.jpg")
    kernel = np.ones((1,1),np.uint8)

    #Convert to GrayScaledImage
    grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    #BınaryThreshold + OtsuThreshold + BinaryThreshold
    retval, threshold = cv2.threshold(grayscaled, 10, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    retval, threshold2 = cv2.threshold(threshold, 10, 255, cv2.THRESH_BINARY_INV)
    threshold2[threshold2 == 255] = 1

    #Skeletonize the Thresholded Image
    skel = skeletonize(threshold2)

    #Build Graph from skeleton
    graph = sknw.build_sknw(skel, multi=False)
    G = nx.Graph(graph)
    plt.imshow(img, cmap='gray')

    #Draw Edges by 'pts'
    for (s,e) in graph.edges():
        ps = graph[s][e]['pts']
        plt.plot(ps[:,1], ps[:,0], 'red')

    #Draw Node by 'o'   
    node, nodes = graph.node, graph.nodes()
    ps = np.array([node[i]['o'] for i in nodes])
    plt.plot(ps[:,1], ps[:,0], 'g.')
    plt.title('Skeletonize')
    plt.savefig('Overlay_Maze.jpg')
    plt.show()

    G = nx.path_graph(len(ps))
    G = nx.karate_club_graph()
    pos = nx.spring_layout(G)
    nx.draw(G,pos,node_color='b')

当我运行上面的代码时,我得到以下输出。

原始输入迷宫图像:

Original Input Maze Image --

图像处理后:

After Processing Image --

X-Y坐标上的节点:

Node points on X-Y coordinates --

路径信息:

Path Info

我可以成功执行图像处理操作,但搜索算法找到了两个节点之间的最短鸟类飞行距离。 我想找到沿着骨架的最短路径。

当我在处理这个 github repo 时向我展示了使用 NetworkX 库解决这个问题,但我无法解决它,因为它没有提供任何细节。

如何使用图像处理和任何搜索算法找到沿迷宫图像骨架的最短路径?

提前致谢。

最佳答案

这是因为您要在此处重新分配对骨架图的引用

G = nx.path_graph(len(ps))
G = nx.karate_club_graph()

enter image description here

enter image description here

关于python-2.7 - 如何在骨架化的迷宫图像中找到最短路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51043824/

相关文章:

从 32 个字符中提取最后 16 个字符的 Python 代码

python - 如何使用Python一次删除多个文件夹?

python - python中的类继承

linux - OpenCV 1.0 : cvCreateFileCapture Always Returns Null under Fedora 11

python - 根据特定字段对字符串列表进行排序

linux - Beaglebone 黑色视频捕获 : Error "select timeout"

c++ - 为什么用 Eigen 和 OpenCV 计算的 SVD 左奇异 vector 有不同的符号

image-processing - 维度必须相等,但“SparseSoftmaxCrossEntropyWithLogits”为 1 和 128

python - 有没有人有任何使用 OpenCV 和 python 进行描述符提取的例子?

image-processing - 在序列matlab中提取字符