简体   繁体   中英

networkx.exception.NodeNotFound even if the node exists

I'm using sknw for building graph from skeletonized image (The image is produced with scikit-image ). The problem is when I try to draw shortest path with any of the algorithm given in skimage networkx.exception.NodeNotFound . But the function graph.has_node() says that node exists.

Don't know what to do as stuck up for many days. Any help is appreciated.

Code

 from skimage.morphology import skeletonize from skimage import data, io from skimage.graph import shortest_path import matplotlib.pyplot as plt import sknw import.networkx as nx import numpy as np import numba img = io.imread('m5_skeleton.png') 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(): ps = graph[s][e]['pts'] plt.plot(ps[:, 1], ps[:, 0], 'green') # draw node by o nodes = graph.nodes() ps = np.array([nodes[i]['o'] for i in nodes]) plt.plot(ps[:, 1], ps[:, 0], 'r.') print('------------------------------------------------------------------------------') print('nodes') print('------------------------------------------------------------------------------') print(nodes) print('------------------------------------------------------------------------------') # title and show plt.title('Build Graph') plt.savefig('m5_graph.svg', dpi=1200) pos = nx.spring_layout(graph) print('------------------------------------------------------------------------------') print('pos') print('------------------------------------------------------------------------------') print(pos) print('------------------------------------------------------------------------------') # nx.draw(graph, pos, node_color='k') # draw path in red print('graph.has_node(0): ' + str(graph.has_node(0))) print('graph.has_node(10): ' + str(graph.has_node(10))) # source = graph.nodes[0] # target = graph.nodes[2] path = nx.astar_path(graph, graph.nodes.get(0), graph.nodes.get(10)) h = graph.subgraph(path) path_edges = zip(path, path[1:]) path_edges = set(path_edges) nx.draw.networkx_nodes(h, pos, nodelist=path, node_color='b', cmap='gray') nx.draw.networkx_edges(h, pos, edge_cmap='gray') plt.axis('equal') plt.show()

在此处输入图像描述 Above is m5_skeleton.png image I have used.

You need to replace:

path = nx.astar_path(graph, graph.nodes.get(0), graph.nodes.get(10))

with

path = nx.astar_path(graph, 0, 10)

That is, the function takes the ID of a node, not the node's contents, as input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM