如何在shortest_path中找到特定的点/坐标?

时间:2018-09-01 08:15:30

标签: python-2.7 numpy matplotlib networkx

我正在使用NetworkX,numpy和sknw module查找迷宫的shortest_path。最短路径算法产生了我想要的结果,并且可以绘制带有节点的路径。但是,我想在此路径上找到其他要点,但它们不是最短路径中的节点。这是刚刚找到的节点指定的最短路径:

shortest path visualized with red nodes

这就是我需要的:

marked with blue circles

这是原始图片:

cropped_image

找到这些点并将其绘制为图像中的红色节点的方法是什么?这是代码(已编辑):

#Skeletonize the Thresholded Image
skel = skeletonize(threshold2)
#Build Graph from skeleton
graph = sknw.build_sknw(skel, multi=False)
G = nx.Graph(graph)
#Find the shortest path
path = nx.shortest_path(G,source=0,target=len(G)-1)
path_edges = zip(path,path[1:])
plt.imshow(img, cmap='gray') 

def nodes_edges(G,n):

    for (s,e) in path_edges:
        ps = graph[s][e]['pts']
        plt.plot(ps[:,1], ps[:,0], 'green')
        # Find the "corner points" and plot:
        tolerance = 30
        simple_polyline = approximate_polygon(ps, tolerance)
        plt.plot(simple_polyline[1:-1, 1], simple_polyline[1:-1, 0], '.m')
    node = G.node
    ps = np.array([node[i]['o'] for i in path])
    plt.plot(ps[:,1], ps[:,0], 'r.')
    plt.axis('equal')
    plt.show()

    print(ps)
    print('Number of Element = ',len(ps))
    print('Number of Step = ', 
    nx.shortest_path_length(G,source=0,target=len(G)-1))
    print('Path Edges = ', path_edges)
    print('Shortest Path = ', path)
    return(n)

nodes_edges(graph,skel)

编辑:这是分别提供转折点和节点的输出 output

1 个答案:

答案 0 :(得分:2)

要查找的“角”点未定义为用于构建图形的“相交”。因此,无法使用相同的方法找到它们。

取决于您对这些点的实际定义,一种方法可能是使用Douglas-Peucker algorithmskimage中的approximate_polygon来简化路径(请参见demo here )。为此,必须选择一个公差参数。

根据sknw自述文件中的example,我尝试重新创建您的文件:

import numpy as np
import matplotlib.pylab as plt

from skimage.morphology import skeletonize
from skimage import data
import sknw
import networkx as nx
from skimage.measure import approximate_polygon

# open and skeletonize
img = data.horse()
ske = skeletonize(~img).astype(np.uint16)

# build graph from skeleton
graph = sknw.build_sknw(ske)

# draw image
plt.imshow(img, cmap='gray')

# draw edges by pts
for (s,e) in graph.edges():
    polyline = graph[s][e]['pts']
    plt.plot(polyline[:,1], polyline[:,0], 'green', alpha=.6)

    # Find the "corner points" and plot:
    tolerance = 5
    simple_polyline = approximate_polygon(polyline, tolerance)
    plt.plot(simple_polyline[1:-1, 1], simple_polyline[1:-1, 0], '.m')


# 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], 'r.')

# title and show
plt.title('Build Graph')
plt.show()

给出:(洋红色点为“角”点)

point detection example

我认为在迷宫图像上效果会更好。

编辑,遍历路径的示例代码:

one_path = nx.shortest_path(graph, source=0, target=8)

full_line = []
for source, target in zip(one_path, one_path[1:]):
    polyline = graph[source][target]['pts']

    # Find the "corner point":
    tolerance = 5
    simple_polyline = approximate_polygon(polyline, tolerance)
    full_line.extend(simple_polyline[:-1])

full_line.append(simple_polyline[-1]) # add the last point
full_line = np.array(full_line)  # convert to an array