標籤:

新的方式畫出個部分血管網路並保存

import numpy as npimport operatorimport osimport copyfrom matplotlib.font_manager import FontPropertiesfrom scipy.interpolate import lagrangeimport randomimport matplotlib.pyplot as plt# 把opt文件內的逗號變為空格np.set_printoptions(threshold=np.inf) #輸出全部矩陣不帶省略號# with open(opt\SubCAM.opt,r) as f:# file = f.read()# file = file.strip().split(
)
# for data in file:# # print(data)# data = data.replace(,, )#replace不能用於列表# data = data.replace(-1,0)# with open(txt\SubCAM1.txt,a) as f:# f.write(data+
)
########################################### data = np.loadtxt(txt\SubCAM.txt)path = os.listdir(txt文件)for file in path: data1 = os.path.join(txt文件,file) data = np.loadtxt(data1) # data = np.loadtxt(txt文件\CAM_7128.txt) data = data.astype(np.int64) x1 = data[:,5]#x起點坐標 x2 = data[:,9]#x終點坐標 y1 = data[:,6]#y起 y2 = data[:,10]#y起 z1 = data[:,4]#IDpart z2 = data[:,8]#IDpart diam = data[:,12] # print(y1.tolist()) # print(y2.tolist()) # print(z1.tolist()) # print(z2.tolist()) s1 = [a1 for a1 in range(1,len(x1)-1) if z1[a1]==z2[a1-1]!=-1 or z1[a1]!= z2[a1-1]]#id相同不等於0,或id不同 # print(s1) lx = []#x1,x2相同的部分組成的列表 lxqi = [] lxzg = [] for i1 in range(len(s1)-1): b1 = x1[s1[i1]:s1[i1+1]] b1 = b1.tolist() b2 = x2[s1[i1+1]-1]#s1[i1]相當於a1 # b1 = b1 + [b2]#把與x2最後相連的一個數和x1拼接起來 b5 = z1[s1[i1]]#x,y起點id b1qi_id = [b5]+b1 +[b2] b6 = z2[s1[i1+1]-1]#x,y終點id b1zg_id = [b6] + b1+[b2] lx.append(b1) lxqi.append(b1qi_id) lxzg.append(b1zg_id) ################################################### ly = []#y坐標以及管徑大小 for i3 in range(len(s1)-1): b3 = y1[s1[i3]:s1[i3+1]] b3 = b3.tolist() b4 = y2[s1[i3+1]-1]#y最後一個不相等的數 b3 = b3 + [b4] dm = diam[s1[i3+1]-1] b3 = b3 + [dm]#加上管徑 ly.append(b3) ##################################################### # lxwei =copy.deepcopy(lx)#深拷貝,不改變原來的值 # lywei = copy.deepcopy(ly) # print(lx) # print(ly) #帶有起點id的x坐標與y坐標合併 for q1 in range(len(lxqi)): for q2 in range(len(ly[q1])): lxqi[q1].append(ly[q1][q2]) #帶有終點id的x坐標與y坐標合併 for p1 in range(len(lxzg)): for p2 in range(len(ly[p1])): lxzg[p1].append(ly[p1][p2]) lxqi.sort(key=operator.itemgetter(0))#排序,只按照第一個索引大小排序 tou = lxqi lxzg.sort(key=operator.itemgetter(0)) wei = lxzg # ######################################### # final = [] # for i in range(len(tou)-1): # for dwei in wei: # if dwei[0]==tou[i][0] and dwei[0]==tou[i+1][0]: # zuhe = [dwei,tou[i],tou[i+1]] # final.append(zuhe) # print(final) toudeng = [] weideng = [] for dwei in wei: for i in range(len(tou)-1): if dwei[0] ==tou[i][0] and dwei[0]==tou[i+1][0]: toud = [dwei,tou[i],tou[i+1]] toudeng.append(toud) for dtou in tou: for i in range(len(wei)-1): if dtou[0] == wei[i][0] and dtou[0]==wei[i+1][0]: weid = [wei[i],wei[i+1],dtou] weideng.append(weid) # ################################################### datatoudeng = [] dataweideng = [] #去掉起點id for i in range(len(toudeng)): a = toudeng[i][0][1::] b = toudeng[i][1][1::] c = toudeng[i][2][1::] d = [a]+[b]+[c] datatoudeng.append(d) for i in range(len(weideng)): a1 = weideng[i][0][1::] b1 = weideng[i][1][1::] c1 = weideng[i][2][1::] d1 = [a1]+[b1]+[c1] dataweideng.append(d1) # print(dataweideng) #判斷管徑信息是否加進列表,若未加進則只為x,y坐標,為偶數 for i in range(len(dataweideng)): a = dataweideng[i] assert len(a[0])%2==1 assert len(a[1])%2==1 assert len(a[2])%2==1 for i in range(len(datatoudeng)): a = datatoudeng[i] assert len(a[0])%2==1 assert len(a[1])%2==1 assert len(a[2])%2==1 finaldata = datatoudeng +dataweideng # print(finaldata) # print(len(finaldata)) ########################################### #可視化,未插值,一個一個畫出血管分叉 # for i in range(len(finaldata)): # w1 = finaldata[i][0] # w2 = finaldata[i][1] # w3 = finaldata[i][2] # x1 = w1[0:(len(w1)-1)//2] # y1 = w1[((len(w1)-1)//2):len(w1)-1] # x2 = w2[0:(len(w2)-1)//2] # y2 = w2[((len(w2)-1)//2):(len(w2)-1)] # x3 = w3[0:((len(w3)-1)//2)] # y3 = w3[((len(w3)-1)//2):(len(w3)-1)] # plt.plot(x1,y1) # plt.plot(x2,y2) # plt.plot(x3,y3) # plt.show() # ##################################################### #畫出所有血管分叉 lx = [] ly = [] for i in range(len(finaldata)): a = finaldata[i][0] b = finaldata[i][1] c = finaldata[i][2] lx.append(a[0:len(a)//2]) ly.append(a[len(a)//2:-1]) lx.append(b[0:len(b)//2]) ly.append(b[len(b)//2:-1]) lx.append(c[0:len(c)//2]) ly.append(c[len(c)//2:-1]) for i in range(len(lx)): plt.plot(lx[i],ly[i]) plt.xlabel(x axis,size=16) plt.ylabel(y axis,size=16) font = FontProperties(fname=rc:windowsfontssimsun.ttc,size=16) plt.title(u血管分叉,fontproperties=font) plt.savefig(C:\Users\Administrator\Desktop\各部分血管圖片\) plt.show()

推薦閱讀:

Python 中如何刪除一個列表 List 中多個符合條件的元素?
五位專家跟你講講為啥Python更適合做AI/機器學習
動腦Python班|所有人都說Python 簡單易學,為何我覺得難?
用 Python 實現一個大數據搜索引擎
【視頻】有了selenium,小白也可以自豪的說:「抓包,cookie,去一邊吧!」

TAG:Python |