第一步对图像进行读取:
研究数据集:
在ARCGIS上观察倾斜程度:
PIL 对路径的支持更友好:PIL 在处理文件路径(尤其是包含中文字符的路径)时通常更加健壮。OpenCV 在某些版本或特定环境下可能会对非英文路径处理不当,导致读取或保存失败。
跨平台兼容性:PIL 在不同操作系统(如 Windows、Linux、macOS)上的路径处理更为一致,这在开发跨平台应用时是一个优势
import pandas as pd
from PIL import Image# 专业红外图像读取方式
def read_ir_image(file_path):try:# 先用PIL读取pil_img = Image.open(file_path)# 转换为numpy数组img_array = np.array(pil_img)# 检查是否为单通道红外数据if len(img_array.shape) == 2: # 单通道print("检测到单通道红外数据")# 归一化处理示例(根据实际传感器数据调整)img_array = (img_array - img_array.min()) / (img_array.max() - img_array.min()) * 255img_array = img_array.astype(np.uint8)# 转换为OpenCV格式return cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) if len(img_array.shape) == 3 else cv2.cvtColor(img_array,cv2.COLOR_GRAY2BGR)except Exception as e:print(f"红外图像处理错误: {e}")# 将 OpenCV 图像转换为 PIL 图像并保存(支持中文路径)
第二步对卫星图像进行分割:
分割可以提高处理效率:将大尺寸的卫星图像分割成多个小块(tiles),每个小块的尺寸与无人机图像相近,可以显著降低单次处理的数据量,加快处理速度。
并行处理:分割后的图像块可以进行并行处理,进一步提升整体处理效率。
局部匹配更精确:在大尺寸图像上直接进行匹配可能会因为场景复杂性高、特征点分布不均等原因导致匹配精度下降。而通过分割,可以在每个小块内进行更精细的特征提取和匹配,提高匹配精度。
读取匹配文件:
使用 pd.read_csv 读取 matches.csv 文件。
matches_df['coordinate'] 列存储了裁剪中心坐标,并通过 apply 解析为元组形式 (x_center, y_center)。
# 读取 matches.csv 文件
matches_df = pd.read_csv(r'E:\倾斜\倾斜\Match-Dataset-train\gs20251-ir\matches.csv')# 打印列名以确认
print("Columns:", matches_df.columns)# 解析 coordinate 列
matches_df['coordinate'] = matches_df['coordinate'].apply(lambda x: tuple(map(int, x.strip('()').split(', '))))
对裁剪图片进行填充并保存:
# 计算裁剪区域half_crop = crop_size // 2x1 = max(0, x_center - half_crop)y1 = max(0, y_center - half_crop)x2 = min(satellite_img.shape[1], x_center + half_crop)y2 = min(satellite_img.shape[0], y_center + half_crop)# 裁剪卫星图cropped_satellite = satellite_img[y1:y2, x1:x2]# 如果裁剪区域小于指定尺寸,进行填充if cropped_satellite.shape[0] < crop_size or cropped_satellite.shape[1] < crop_size:padded_satellite = np.zeros((crop_size, crop_size, 3), dtype=np.uint8)padx1 = (crop_size - cropped_satellite.shape[1]) // 2pady1 = (crop_size - cropped_satellite.shape[0]) // 2padx2 = crop_size - cropped_satellite.shape[1] - padx1pady2 = crop_size - cropped_satellite.shape[0] - pady1padded_satellite[pady1:pady1 + cropped_satellite.shape[0],padx1:padx1 + cropped_satellite.shape[1]] = cropped_satellitecropped_satellite = padded_satellite
第三步对图片进行倾斜校正:
要根据无人机的姿态角(yaw、pitch、roll)将图像修正为正射投影(orthorectification),需要进行一系列的几何变换。
Yaw 角影响图像的水平旋转,可以通过简单的旋转来修正。
Pitch 和 Roll 的修正较为复杂,需要进行三维旋转。可以使用 OpenCV 的 warpPerspective 函数来实现。
上述方法基于简单的几何变换,对于复杂的地形和高精度要求,可能需要更专业的正射纠正算法(如使用 DEM 数据)。
# 读取姿态角数据csv_path = 'drone.csv'df = pd.read_csv(csv_path)image_name = '000000.jpg'image_data = df[df['drone_name'] == image_name].iloc[0]yaw, pitch, roll = image_data['yaw'], image_data['pitch'], image_data['roll']# Yaw 修正corrected_yaw = rotate_image(ir_image, -yaw)# Pitch 和 Roll 修正corrected_pitch_roll = correct_pitch_roll(corrected_yaw, pitch, roll)
第四步进行训练学习:
可以适当调整训练轮次改到100,然后每一个批次可以改为8,以提高训练效率。
# Eval Configparser.add_argument('--batch_size_eval', default=128, type=int)parser.add_argument('--eval_every_n_epoch', default=1, type=int)parser.add_argument('--normalize_features', default=True, type=bool)parser.add_argument('--eval_gallery_n', default=-1, type=int)
第五步扩大数据集:
历史影像地图数据提供了更多的样本,可以帮助模型更好地学习和泛化。特别是在数据集较小或特定场景数据不足的情况下,历史影像可以作为补充数据。
寻找历史影像数据,将历史影像的数据格式、分辨率等与现有数据集一致,可以直接将其加入数据集中进行训练。