Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find shortest path in skeletonized maze image?

I am working on maze solving using Image Processing and NetworkX search algroithm and need to find the shortest connecting path between two points on those lines.

#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')

When I run the code above, I get the following outputs.

Original Input Maze Image :

Original Input Maze Image --

After Processing Image:

After Processing Image --

Node points on X-Y coordinates:

Node points on X-Y coordinates --

Path Info:

Path Info

I can successfully perform image processing operations, but the search algorithm finds the shortest bird flight distance between two nodes. I want to find the shortest path along the Skeleton.

When I was working on this github repo showed me solve this problem using the NetworkX library but I can not solve it because it does not give any detail.

How to find the shortest path along skeleton of maze image using image processing and any search algorithm ?

Thanks in advance.

like image 617
Yoko Bolt Avatar asked Jun 26 '18 13:06

Yoko Bolt


People also ask

How do you find the shortest path in a binary maze?

Shortest path in a Binary Maze. Given a MxN matrix where each element can either be 0 or 1. We need to find the shortest path between a given source cell to a destination cell. The path can only be created out of a cell if its value is 1. Expected time complexity is O(MN).

How does the mazesolver class work?

The MazeSolver class stores the Maze as a 2D integer array with value '0' for open (available) nodes and non-zero for closed nodes (walls). If a path is to be found, a new 2D integer array is created with the path traced by PathCharacter whose default value is '100'. The class can also trace diagonal paths if it is allowed to do so.

How does the shortest path algorithm work in Excel?

It considers all the paths starting from the source and moves ahead one unit in all those paths at the same time which makes sure that the first time when the destination is visited, it is the shortest path. // a given source cell to a destination cell.

Can the class trace diagonal paths in a matrix?

The class can also trace diagonal paths if it is allowed to do so. Throughout this article, we will use "node" to refer elements of the matrix (2D integer array representing a maze).


1 Answers

It's because you are reassigning you reference to the skeletonized graph here

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

enter image description here

enter image description here

like image 74
Gambit1614 Avatar answered Sep 22 '22 07:09

Gambit1614