Loading [Contrib]/a11y/accessibility-menu.js

alex_bn_lee

导航

< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8

统计

【713】骨架图矢量化实现

参考:骨架矢量化sknw源码研读

参考:https://github.com/Image-Py/sknw

一、从栅格数据中提取结点和线段信息

代码:

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()

 

 

 

 

 


 

 

posted on   McDelfino  阅读(367)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
历史上的今天:
2020-05-29 【468】numpy 数组获取对角线数值
2020-05-29 【468】存储数据变量 — pickle 模块
点击右上角即可分享
微信分享提示