當(dāng)前位置:首頁 > IT技術(shù) > 其他 > 正文

【713】骨架圖矢量化實(shí)現(xiàn)
2022-05-29 22:31:53

參考:骨架矢量化sknw源碼研讀

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

一、從柵格數(shù)據(jù)中提取結(jié)點(diǎn)和線段信息

代碼:

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)
 
# 矢量化調(diào)用函數(shù)
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')

?

?

二、將柵格數(shù)據(jù)轉(zhuǎn)換為對應(yīng)的矢量數(shù)據(jù)

# 左上角經(jīng)緯度信息
min_lon = 118.2808087994331
max_lat = 25.008157558205507
# 每個像素網(wǎng)格對應(yīng)的經(jīng)緯度值
delta_lon = 5.368543361789728e-06
delta_lat = 4.85525333299384e-06

# 結(jié)點(diǎn)轉(zhuǎn)換為矢量信息
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) 

# 線段轉(zhuǎn)換為矢量信息
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) 
    
# 矢量數(shù)據(jù)顯示
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/

開通會員,享受整站包年服務(wù)立即開通 >