import numpy as np import pandas as pd from scipy.spatial import KDTree from PIL import Image # 读取CSV文件 data = np.genfromtxt('PHA SID230828-2003.csv', delimiter=',') # 创建矩阵A A = np.zeros((120, 120)) A[:data.shape[0], :data.shape[1]] = data # 创建矩阵B B = np.zeros((1280, 1024)) # 进行映射和填充已知点 for u in range(120): for v in range(120): x = 0.211 * u - 4.538 * v + 894.754 y = -4.594 * u - 0.1044 * v + 800.7384 # 判断坐标是否在矩阵B的范围内 if 0 <= int(x) < 1280 and 0 <= int(y) < 1024: B[int(x), int(y)] = A[u, v] # 寻找未知点的最近邻并进行填充 known_points = np.argwhere(B != 0) # 获取已知点的坐标 tree = KDTree(known_points) # 构建KD树 unknown_points = np.argwhere(B == 0) # 获取未知点的坐标 for u, v in unknown_points: x = 0.211 * u - 4.538 * v + 894.754 y = -4.594 * u - 0.1044 * v + 800.7384 # 寻找最近邻点的索引 _, idx = tree.query([u, v]) nearest_point = known_points[idx] nearest_u, nearest_v = nearest_point[0], nearest_point[1] # 用最近邻点的值填充未知点 B[u, v] = B[nearest_u, nearest_v] # 将矩阵B保存为CSV文件 df = pd.DataFrame(B) df.to_csv('output.csv', index=False, header=False) matrix_B = B.copy() # 将矩阵B中的非零值映射到灰度值范围 matrix_B = np.where(matrix_B != 0, matrix_B * 217, matrix_B) # 将矩阵B转换为8位灰度图像 image = Image.fromarray(matrix_B.astype(np.uint8)) # 保存为BMP图像 image.save('output2.bmp')