import os
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import math
import xlwt
from six.moves import xrange
np.set_printoptions(suppress=True)
np.set_printoptions(threshold=np.inf) #输出全部矩阵不带省略号
gen = True
# gen = False
save_dir = 'C:\\Users\\Administrator\\Desktop\\重新整理血管网络\\生成数据1.xls'
if gen:
data = np.load('data/good.npy')
else:
data = np.load('data/final37原始数据端点相连.npy')
data = data[500:1000,:,0:60] #选择保存多少数据
data = data.reshape(-1,3,60)
#距离计算公式
def get_len(x1,x2,y1,y2):
diff_x = (x1-x2)**2
diff_y = (y1-y2)**2
length = np.sqrt(diff_x+diff_y)
return length
#余弦定理计算角度公式
def cal_angle(a,b,c):
cos_angle = (a**2+b**2-c**2)/(2*a*b)
angle = np.arccos(cos_angle)
angle = angle*180/np.pi
return angle
#计算端点长度
def get_duandian_len(zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y):
#主分支头尾坐标
zhu_x_tou = zhu_x[0]
zhu_y_tou = zhu_y[0]
zhu_x_wei = zhu_x[-1]
zhu_y_wei = zhu_y[-1]
#左分支头尾坐标
zuo_x_tou = zuo_x[0]
zuo_y_tou = zuo_y[0]
zuo_x_wei = zuo_x[-1]
zuo_y_wei = zuo_y[-1]
#右分支头尾坐标
you_x_tou = you_x[0]
you_y_tou = you_y[0]
you_x_wei = you_x[-1]
you_y_wei = you_y[-1]
#主分支端点长
zhu_duan_len = get_len(zhu_x_tou,zhu_x_wei,zhu_y_tou,zhu_y_wei)
#左分支端点长
zuo_duan_len = get_len(zuo_x_tou,zuo_x_wei,zuo_y_tou,zuo_y_wei)
#右分支端点长
you_duan_len = get_len(you_x_tou,you_x_wei,you_y_tou,you_y_wei)
return zhu_duan_len,zuo_duan_len,you_duan_len
#计算分支总长度
def get_total_len(zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y):
zhu_lin_list = []
zuo_lin_list = []
you_lin_list = []
for i in range(1,len(zhu_x)):
#相邻点大小相差1e-5可以认为他们是插入点,距离为0
threshold = 1e-3
zhu_lin_len = get_len(zhu_x[i-1],zhu_x[i],zhu_y[i-1],zhu_y[i])
if zhu_lin_len< threshold:
zhu_lin_len = 0
zuo_lin_len = get_len(zuo_x[i-1],zuo_x[i],zuo_y[i-1],zuo_y[i])
if zuo_lin_len< threshold:
zuo_lin_len = 0
you_lin_len = get_len(you_x[i-1],you_x[i],you_y[i-1],you_y[i])
if you_lin_len< threshold:
you_lin_len = 0
zhu_lin_list.append(zhu_lin_len)
zuo_lin_list.append(zuo_lin_len)
you_lin_list.append(you_lin_len)
zhu_total_len = 0
for file in zhu_lin_list:
zhu_total_len += file
zuo_total_len = 0
for file in zuo_lin_list:
zuo_total_len += file
you_total_len = 0
for file in you_lin_list:
you_total_len += file
return zhu_total_len,zuo_total_len,you_total_len
#计算卷曲度
def get_juanqu(zhu_duan_len,zuo_duan_len,you_duan_len,zhu_total_len,zuo_total_len,you_total_len):
zhu_juanqu = zhu_total_len / zhu_duan_len
zuo_juanqu = zuo_total_len / zuo_duan_len
you_juanqu = you_total_len / you_duan_len
return zhu_juanqu,zuo_juanqu,you_juanqu
#计算角度
def get_angle(zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y):
#主分支上两个尾部点
zhu_x_a = zhu_x[-2]
zhu_y_a = zhu_y[-2]
zhu_x_b = zhu_x[-1]
zhu_y_b = zhu_y[-1]
#左分支上两个头部点
zuo_x_a = zuo_x[0]
zuo_y_a = zuo_y[0]
zuo_x_b = zuo_x[1]
zuo_y_b = zuo_y[1]
#右分支上两个头部点
you_x_a = you_x[0]
you_y_a = you_y[0]
you_x_b = you_x[1]
you_y_b = you_y[1]
#zhu_x_b,zuo_x_a,you_x_a应该相等
#每个端点两点长度
zhu_ab_len = get_len(zhu_x_a,zhu_x_b,zhu_y_a,zhu_y_b)
zuo_ab_len = get_len(zuo_x_a,zuo_x_b,zuo_y_a,zuo_y_b)
you_ab_len = get_len(you_x_a,you_x_b,you_y_a,you_y_b)
zhu_zuo_len = get_len(zhu_x_a,zuo_x_b,zhu_y_a,zuo_y_b)
zhu_you_len = get_len(zhu_x_a,you_x_b,zhu_y_a,you_y_b)
zuo_you_len = get_len(zuo_x_b,you_x_b,zuo_y_b,you_y_b)
zhu_zuo_angle = cal_angle(zhu_ab_len,zuo_ab_len,zhu_zuo_len)
zhu_you_angle = cal_angle(zhu_ab_len,you_ab_len,zhu_you_len)
zuo_you_angle = cal_angle(zuo_ab_len,you_ab_len,zuo_you_len)
zong_angle = zhu_zuo_angle + zhu_you_angle + zuo_you_angle
return zhu_zuo_angle,zhu_you_angle,zuo_you_angle,zong_angle
#计算主分支与左右分支的长度比
def get_ratio(zhu_total_len,zuo_total_len,you_total_len):
zhu_zuo_ratio = zhu_total_len / zuo_total_len
zhu_you_ratio = zhu_total_len / you_total_len
return zhu_zuo_ratio,zhu_you_ratio
#将指标写入excel文档
def calculate(data):
data = data.reshape(-1,3,60)
data = data[:,:,0:60]
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
sheet = book.add_sheet('mysheet',cell_overwrite_ok=True)
title = ["主分叉长度","左分叉长度","右分叉长度","主左长度比","主右长度比","主分叉弯曲度","左分叉卷曲度","右分叉卷曲度","主左角度","主右角度","左右角度","主端点长度","左端点长度","右端点长度","总角度"]
for j in range(len(title)):
sheet.write(0,j,title[j])
for i in range(1,len(data)):
zhu_x = data[i][0][0:30]
zhu_y = data[i][0][30:60]
zuo_x = data[i][1][0:30]
zuo_y = data[i][1][30:60]
you_x = data[i][2][0:30]
you_y = data[i][2][30:60]
#计算每个分叉分支端点长度
zhu_duan_len,zuo_duan_len,you_duan_len = get_duandian_len(zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y)
#计算每个分叉分支总长度
zhu_total_len,zuo_total_len,you_total_len = get_total_len(zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y)
#计算主分支与左右分支长度比
zhu_zuo_ratio,zhu_you_ratio = get_ratio(zhu_total_len,zuo_total_len,you_total_len)
#计算每个分叉分支卷曲度
zhu_juanqu,zuo_juanqu,you_juanqu = get_juanqu(zhu_duan_len,zuo_duan_len,you_duan_len,zhu_total_len,zuo_total_len,you_total_len)
#计算每个分叉分支角度
zhu_zuo_angle,zhu_you_angle,zuo_you_angle,zong_angle= get_angle(zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y)
#打印数组中曲线长度
indicators = [zhu_total_len,zuo_total_len,you_total_len,zhu_zuo_ratio,zhu_you_ratio,zhu_juanqu,zuo_juanqu,you_juanqu,zhu_zuo_angle,zhu_you_angle,zuo_you_angle,zhu_duan_len,zuo_duan_len,you_duan_len,zong_angle]
#写入excel文档
for k in range(len(indicators)):
sheet.write(i,k,indicators[k])
book.save(save_dir)
print("结束")
calculate(data)
可视化原始数据与生成数据
import numpy as np
import matplotlib.pyplot as plt
resorce_data = np.load('data/final37端点补充相等值.npy')
gen_data = np.load('data/good.npy')
np.random.shuffle(gen_data)
gen_data = gen_data[:,:,0:60]
np.random.shuffle(resorce_data)
np.set_printoptions(suppress=True)
np.set_printoptions(threshold=np.inf) #输出全部矩阵不带省略号
resorce_data = resorce_data[:,:,0:60]
def manage_gen(gen_imgs):
#gen_imgs一个维度为(-1,3,60)的数组,头部分支的尾部,与左右分支的头部分开了
#目的把头的尾部,加入左右分支头部,并保证,维度不变
gen_imgs = gen_imgs.reshape(-1,3,60)
finaldata = gen_imgs.tolist()
final = []
for i in range(len(finaldata)):
zhu = finaldata[i][0]
zuo = finaldata[i][1]
you = finaldata[i][2]
#单独分开x,y,列表
zhu_x = zhu[0:30]
zhu_y = zhu[30:60]
zuo_x = zuo[0:30]
zuo_y = zuo[30:60]
you_x = you[0:30]
you_y = you[30:60]
############################################
#真实数据主分支最后两个基本相等,所以生成数据也是,这样计算角度时,就应该计算最后一个和倒数第三个点
#为了让主分支最后一个加在左右分支的头部,此处先去掉左右分支的最后一个点,因为端点插入的值都是相等的,所以去掉影响不大
#然后,再将主分支的尾部,加入左右分支头部,这样,就保证了维度不变
#去除左右分支尾部一个数
del zuo_x[-1]
del zuo_y[-1]
del you_x[-1]
del you_y[-1]
#在左右分支的头部插入主分支的尾部
zuo_x.insert(0,zhu_x[-1])
zuo_y.insert(0,zhu_y[-1])
you_x.insert(0,zhu_x[-1])
you_y.insert(0,zhu_y[-1])
zhu_x.extend(zhu_y)
zuo_x.extend(zuo_y)
you_x.extend(you_y)
fencha = [zhu_x] +[zuo_x] + [you_x]
final.append(fencha)
final = np.array(final)#一个维度为(-1,3,60)的数组
return final
resorce_data = manage_gen(resorce_data)
print(resorce_data.shape)
# np.save('data/final37原始数据端点相连.npy',resorce_data)
def fenzhi(data):
data = data.reshape(-1,3,60)
data = data[0:1,:,0:60]
for i in range(len(data)):
zhu_x = data[i][0][0:30]
zhu_y = data[i][0][30:60]
zuo_x = data[i][1][0:30]
zuo_y = data[i][1][30:60]
you_x = data[i][2][0:30]
you_y = data[i][2][30:60]
print(zhu_x)
print(zuo_x)
print(you_x)
return zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y
def plt_imgs(zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y):
plt.plot(zhu_x,zhu_y,color='red')
plt.plot(zuo_x,zuo_y,color='blue')
plt.plot(you_x,you_y,color='green')
plt.show()
zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y = fenzhi(resorce_data)
# zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y = fenzhi(gen_data)
plt_imgs(zhu_x,zhu_y,zuo_x,zuo_y,you_x,you_y)