一、从栅格数据中提取结点和线段信息
代码:
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')
二、将栅格数据转换为对应的矢量数据
# 左上角经纬度信息 min_lon = 118.2808087994331 max_lat = 25.008157558205507 # 每个像素网格对应的经纬度值 delta_lon = 5.368543361789728e-06 delta_lat = 4.85525333299384e-06 # 结点转换为矢量信息 pts_lonlat = [] for pt in ps: x = pt[0] y = pt[1] lon = min_lon + delta_lon * x lat = max_lat - delta_lat * y pts_lonlat.append([lon, lat]) pts_lonlat = np.array(pts_lonlat) # 线段转换为矢量信息 line_lonlat = [] for (s, e) in graph.edges(): line = graph[s][e]['pts'] line_sh = [] for pts in line: x = pts[0] y = pts[1] lon = min_lon + delta_lon * x lat = max_lat - delta_lat * y line_sh.append([lon, lat]) line_lonlat.append(line_sh) # 矢量数据显示 for i in range(len(line_lonlat)): line_shapely = LineString(line_lonlat[i]) x1,y1=line_shapely.xy plt.plot(y1,x1) plt.plot(pts_lonlat[:, 1], pts_lonlat[:, 0], 'g.') plt.show()
本文摘自 :https://www.cnblogs.com/