def dist_for_float(p1, p2): p1 = DTW.numpy_num_to_python_num(p1) p2 = DTW.numpy_num_to_python_num(p2) if (type(p1) == float or type(p1) == int) and \ (type(p2) == float or type(p2) == int): dist = float(abs(p1 - p2)) return dist else: sum_val = 0.0 for i in range(len(p1)): sum_val += pow(p1[i] - p2[i], 2) dist = pow(sum_val, 0.5) return dist请帮我把上面的欧式距离计算改为马氏距离计算
时间: 2023-03-29 09:02:14 浏览: 188
马氏距离的计算公式为:dist = sqrt((p1-p2) * S^(-1) * (p1-p2)^T),其中S为协方差矩阵,^T表示矩阵的转置。因此,可以将原来的代码修改为:
import numpy as np
def mahalanobis_dist(p1, p2, cov):
p1 = np.array(p1)
p2 = np.array(p2)
diff = p1 - p2
inv_cov = np.linalg.inv(cov)
dist = np.sqrt(np.dot(np.dot(diff, inv_cov), diff.T))
return dist
# 使用示例
p1 = [1, 2, 3]
p2 = [4, 5, 6]
cov = np.array([[1, , ], [, 1, ], [, , 1]]) # 假设协方差矩阵为单位矩阵
dist = mahalanobis_dist(p1, p2, cov)
print(dist) # 输出结果为 5.196152422706632
相关问题
def dist_for_float(p1, p2, cov): p1 = DTW.numpy_num_to_python_num(p1) p2 = DTW.numpy_num_to_python_num(p2) if (type(p1) == float or type(p1) == int) and \ (type(p2) == float or type(p2) == int): dist = float(abs(p1 - p2)) return dist else: sum_val = 0.0 for i in range(len(p1)): sum_val += pow(p1[i] - p2[i], 2) dist = pow(sum_val, 0.5) return dist
这是一个计算两个点之间距离的函数,输入参数包括两个点p1和p2,以及一个协方差矩阵cov。该函数首先将输入的numpy类型转换为python类型,然后判断p1和p2是否为float或int类型,如果是,则直接计算两点之间的距离,如果不是,则按照欧氏距离公式计算两个点之间的距离。最后返回计算出的距离值。
def DSM_grid_sorting_masking_check(DSM,grid_size,threshold_angle): ''' 进行基于DSM格网排序的遮蔽检测方法 :param DSM: 输入的数字高程模型 :param grid_size: 格网大小 :param threshold_angle: 实现遮蔽的最大角度 :return: 遮蔽检测结果。True表示不遮蔽,False表示遮蔽 ''' width = DSM.RasterXSize height = DSM.RasterYSize #计算网格数量 grid_num_y =int(np.ceil(height/grid_size)) grid_num_x =int(np.ceil(width/grid_size)) #初始化遮蔽检测结果矩阵 result = np.ones((grid_num_y,grid_num_x),dtype=bool) #计算每个格网进行遮蔽检测 for i in range(grid_num_y): for j in range(grid_num_x): #当前格网内的点坐标 y_min = i*grid_size y_max = min((i+1)*grid_size,height) x_min = j * grid_size x_max = min((j+1)*grid_size,width) coords = np.argwhere(DSM.ReadAsArray(x_min, y_min, x_max - x_min, y_max - y_min) > 0) coords[:, 0] += y_min coords[:, 1] += x_min # 构建KD树 tree = cKDTree(coords) # 查询每个点的最邻近点 k = 2 dist, ind = tree.query(coords, k=k) # 计算每个点的法向量 normals = np.zeros(coords.shape) for l in range(coords.shape[0]): if k == 2: p1 = coords[l, :] p2 = coords[ind[l, 1], :] else: p1 = coords[l, :] p2 = coords[ind[l, 1], :] normals[l, :] = np.cross(p1 - p2, p1 - DSM.ReadAsArray(p1[1], p1[0], 1, 1)) # 计算每个点的可见性 visibilities = np.zeros(coords.shape[0]) for l in range(coords.shape[0]): if k == 2: p1 = coords[l, :] p2 = coords[ind[l, 1], :] else: p1 = coords[l, :] p2 = coords[ind[l, 1], :] angle = np.cross(np.dot(normals[l, :], (p2 - p1) / dist[l, 1])) * 180 / np.pi if angle <= threshold_angle: visibilities[l] = 1 # 判断当前格网是否遮蔽 if np.sum(visibilities) == 0: result[i, j] = False else: result[i, j] = True return result dsm_path = 'C:/yingxiang/output.tif' DSM = gdal.Open(dsm_path) result = DSM_grid_sorting_masking_check(DSM,grid_size=10,threshold_angle=10) print(result.shape)这段代码怎么改可以输出每个点是否被遮蔽
可以在函数中添加一个返回值,记录每个点是否被遮蔽。可以按照以下方式修改函数:
```python
def DSM_grid_sorting_masking_check(DSM,grid_size,threshold_angle):
''' 进行基于DSM格网排序的遮蔽检测方法
:param DSM: 输入的数字高程模型
:param grid_size: 格网大小
:param threshold_angle: 实现遮蔽的最大角度
:return: 遮蔽检测结果。True表示不遮蔽,False表示遮蔽,以及每个点是否被遮蔽的矩阵,True表示不遮蔽,False表示遮蔽
'''
width = DSM.RasterXSize
height = DSM.RasterYSize
#计算网格数量
grid_num_y =int(np.ceil(height/grid_size))
grid_num_x =int(np.ceil(width/grid_size))
#初始化遮蔽检测结果矩阵
result = np.ones((grid_num_y,grid_num_x),dtype=bool)
mask = np.zeros((height, width), dtype=bool)
#计算每个格网进行遮蔽检测
for i in range(grid_num_y):
for j in range(grid_num_x):
#当前格网内的点坐标
y_min = i*grid_size
y_max = min((i+1)*grid_size,height)
x_min = j * grid_size
x_max = min((j+1)*grid_size,width)
coords = np.argwhere(DSM.ReadAsArray(x_min, y_min, x_max - x_min, y_max - y_min) > 0)
coords[:, 0] += y_min
coords[:, 1] += x_min
# 构建KD树
tree = cKDTree(coords)
# 查询每个点的最邻近点
k = 2
dist, ind = tree.query(coords, k=k)
# 计算每个点的法向量
normals = np.zeros(coords.shape)
for l in range(coords.shape[0]):
if k == 2:
p1 = coords[l, :]
p2 = coords[ind[l, 1], :]
else:
p1 = coords[l, :]
p2 = coords[ind[l, 1], :]
normals[l, :] = np.cross(p1 - p2, p1 - DSM.ReadAsArray(p1[1], p1[0], 1, 1))
# 计算每个点的可见性
visibilities = np.zeros(coords.shape[0])
for l in range(coords.shape[0]):
if k == 2:
p1 = coords[l, :]
p2 = coords[ind[l, 1], :]
else:
p1 = coords[l, :]
p2 = coords[ind[l, 1], :]
angle = np.cross(np.dot(normals[l, :], (p2 - p1) / dist[l, 1])) * 180 / np.pi
if angle <= threshold_angle:
visibilities[l] = 1
# 判断当前格网是否遮蔽
if np.sum(visibilities) == 0:
result[i, j] = False
mask[y_min:y_max, x_min:x_max] = True
else:
result[i, j] = True
return result, mask
dsm_path = 'C:/yingxiang/output.tif'
DSM = gdal.Open(dsm_path)
result, mask = DSM_grid_sorting_masking_check(DSM,grid_size=10,threshold_angle=10)
print(result.shape)
```
使用该函数后,`mask` 矩阵的值即表示每个点是否被遮蔽,True 表示被遮蔽,False 表示没有被遮蔽。
阅读全文