参考:骨架矢量化sknw源码研读
代码:
from skimage.morphology import skeletonize from skimage import data import sknw import numpy as np import matplotlib.pyplot as plt # 骨架提取 img = data.horse() ske = skeletonize(~img).astype(np.uint16) # 矢量化调用函数 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 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() # plt.savefig('pc.png')