欧式距离(L2范数)是最易于理解的一种距离计算方法,源自欧式空间中两点间的距离公式(如下图所示)。
#导入math包
import math
#定义点的函数
class Point:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def getx(self):
return self.x
def gety(self):
return self.y
#定义直线函数
class Getlen:
def __init__(self,p1,p2):
self.x=p1.getx()-p2.getx()
self.y=p1.gety()-p2.gety()
#用math.sqrt()求平方根
self.len= math.sqrt((self.x**2)+(self.y**2))
#定义得到直线长度的函数
def getlen(self):
return self.len
#设置点p1的坐标为(0,0)
p1=Point(0,0)
#设置点p2的坐标为(3,4)
p2=Point(3,4)
l=Getlen(p1,p2)
#获取两点之间直线的长度
l.getlen()
import matplotlib.pyplot as plt
import numpy as np
import math
data = np.load('final37.npy')
arr = data[0:10,:,0:60]
# arr = [[[1,2,3,3,3,3],[4,5,6,4,4,4],[7,8,9,7,7,7]]]
# arr = np.array(arr)
def getLen(x1,y1,x2,y2):
diff_x = (x1-x2)**2
diff_y = (y1-y2)**2
length = math.sqrt(diff_x+diff_y)
return length
len_arr = []
for i in range(len(arr)):
len1 = []
for j in range(len(arr[i])):
halfLen = len(arr[i][j])//2
arrLen = [] #数组中相邻坐标点的长度
for k in range(halfLen-1):
x1 = arr[i][j][k]
x2 = arr[i][j][k+1]
y1 = arr[i][j][k+halfLen]
y2 = arr[i][j][k+halfLen+1]
lineLen = getLen(x1,y1,x2,y2) #两点之间的长度
arrLen.append(lineLen)
lenSum = 0.
for n in range(len(arrLen)):
lenSum = arrLen[n] + lenSum
len1.append(lenSum)
len_arr.append(len1)
#打印数组中曲线长度
print(len_arr)
for i in range(len(len_arr)):
print("第%d个图" % i)
for j in range(len(len_arr[i])):
print("第%d个图,第%d个分支长度1: %f" % (i,j,len_arr[i][j]))
#可视化图像
data_images = arr
for i in range(10):
plt.figure(figsize=(100,100),dpi=1)
plt.plot(data_images[i][0][0:30],data_images[i][0][30:60],color='blue',linewidth=150)
plt.plot(data_images[i][1][0:30],data_images[i][1][30:60],color='red',linewidth=150)
plt.plot(data_images[i][2][0:30],data_images[i][2][30:60],color='green',linewidth=150)
plt.axis('off')
plt.show()
# plt.savefig('C:\\Users\\Administrator\\Desktop\\调整分辨率\\原始图\\resouce%d.jpg' %(i),dpi=1)
plt.close()
from datetime import datetime
import os
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import math
from six.moves import xrange
data = np.load('C:\\Users\\Administrator\\Desktop\\重新整理血管网络\\txt\\头尾不旋转.npy')
arr = data[0:10,:,0:60]
# arr = [[[1,2,3,3,3,3],[4,5,6,4,4,4],[7,8,9,7,7,7]]]
# arr = np.array(arr)
def getLen(x1,y1,x2,y2):
diff_x = (x1-x2)**2
diff_y = (y1-y2)**2
length = math.sqrt(diff_x+diff_y)
return length
len_arr = []
wanqu = []
for i in range(len(arr)):
len1 = []
wanqu1 = []
for j in range(len(arr[i])):
halfLen = len(arr[i][j])//2
arrLen = [] #数组中相邻坐标点的长度
#两端点的长度
duandian = getLen(arr[i][j][0],arr[i][j][30],arr[i][j][29],arr[i][j][59])
for k in range(halfLen-1):
x1 = arr[i][j][k]
x2 = arr[i][j][k+1]
y1 = arr[i][j][k+halfLen]
y2 = arr[i][j][k+halfLen+1]
lineLen = getLen(x1,y1,x2,y2) #两点之间的长度
arrLen.append(lineLen)
#曲线的长度
lenSum = 0.
for n in range(len(arrLen)):
lenSum = arrLen[n] + lenSum
wanqu2 = duandian / lenSum
wanqu1.append(wanqu2)
len1.append(lenSum)
# print("a:%f,b:%f"% (duandian,lenSum))
len_arr.append(len1)
wanqu.append(wanqu1)
print(wanqu)
print(len_arr)
#打印数组中曲线长度
# print(len_arr)
# for i in range(len(len_arr)):
# print("第%d个图" % i)
# for j in range(len(len_arr[i])):
# print("第%d个图,第%d个分支长度1: %f" % (i,j,len_arr[i][j]))
#可视化图像
# data_images = arr
# for i in range(10):
# plt.figure(figsize=(100,100),dpi=1)
# plt.plot(data_images[i][0][0:30],data_images[i][0][30:60],color='blue',linewidth=150)
# plt.plot(data_images[i][1][0:30],data_images[i][1][30:60],color='red',linewidth=150)
# plt.plot(data_images[i][2][0:30],data_images[i][2][30:60],color='green',linewidth=150)
# plt.axis('off')
# plt.show()
# # plt.savefig('C:\\Users\\Administrator\\Desktop\\调整分辨率\\原始图\\resouce%d.jpg' %(i),dpi=1)
# plt.close()