【713】骨架图矢量化实现
一、从栅格数据中提取结点和线段信息
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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') |

二、将栅格数据转换为对应的矢量数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # 左上角经纬度信息 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() |



【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2020-05-29 【468】numpy 数组获取对角线数值
2020-05-29 【468】存储数据变量 — pickle 模块