diff --git a/image_fusion/Image_Registration_test.py b/image_fusion/Image_Registration_test.py index f29125e..bf5731c 100644 --- a/image_fusion/Image_Registration_test.py +++ b/image_fusion/Image_Registration_test.py @@ -13,7 +13,7 @@ from ultralytics import YOLO # 添加YOLOv8模型初始化 yolo_model = YOLO("yolov8n.pt") # 可替换为yolov8s/m/l等 -yolo_model.to('cuda') # 启用GPU加速(可选) +yolo_model.to('cuda') # 启用GPU加速 def sift_registration(img1, img2): @@ -125,6 +125,7 @@ def Images_matching(img_base, img_target): src_pts = np.array([kp1[m.queryIdx].pt for m in good]) # 查询图像的特征描述子索引 # 134, 2 dst_pts = np.array([kp2[m.trainIdx].pt for m in good]) # 训练(模板)图像的特征描述子索引 if len(src_pts) <= 4: + print("Not enough matches are found - {}/{}".format(len(good), 4)) return 0, None, 0 else: # print(len(dst_pts), len(src_pts), "配准坐标点") @@ -204,38 +205,66 @@ if __name__ == '__main__': time_all = 0 dots = 0 i = 0 - fourcc = cv2.VideoWriter_fourcc(*'XVID') - capture = cv2.VideoCapture("video/20190926_141816_1_8/20190926_141816_1_8/infrared.mp4") - capture2 = cv2.VideoCapture("video/20190926_141816_1_8/20190926_141816_1_8/visible.mp4") - fps = capture.get(cv2.CAP_PROP_FPS) - out = cv2.VideoWriter('output2.mp4', fourcc, fps, (640, 480)) - # 持续读取摄像头数据 - while True: - read_code, frame = capture.read() # 红外帧 - read_code2, frame2 = capture2.read() # 可见光帧 - if not read_code: - break - i += 1 - # frame = cv2.resize(frame, (1920, 1080)) - # frame2 = cv2.resize(frame2, (640, 512)) - - # 转换为灰度图(红外图像处理) - frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - - # 调用main函数进行融合和检测 - flag, fusion, dot = main(frame2, frame_gray) - - if flag == 1: - # 显示带检测结果的融合图像 - cv2.imshow("Fusion with YOLOv8 Detection", fusion) - out.write(fusion) - - if cv2.waitKey(1) == ord('q'): - break - # 释放资源 - capture.release() - capture2.release() - cv2.destroyAllWindows() - ave = time_all / i - print(ave, "平均时间") - cv2.destroyAllWindows() + # fourcc = cv2.VideoWriter_fourcc(*'XVID') + # capture = cv2.VideoCapture("video/20190926_141816_1_8/20190926_141816_1_8/infrared.mp4") + # capture2 = cv2.VideoCapture("video/20190926_141816_1_8/20190926_141816_1_8/visible.mp4") + # fps = capture.get(cv2.CAP_PROP_FPS) + # out = cv2.VideoWriter('output2.mp4', fourcc, fps, (640, 480)) + # # 持续读取摄像头数据 + # while True: + # read_code, frame = capture.read() # 红外帧 + # read_code2, frame2 = capture2.read() # 可见光帧 + # if not read_code: + # break + # i += 1 + # # frame = cv2.resize(frame, (1920, 1080)) + # # frame2 = cv2.resize(frame2, (640, 512)) + # + # # 转换为灰度图(红外图像处理) + # frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + # + # # 调用main函数进行融合和检测 + # flag, fusion, dot = main(frame2, frame_gray) + # + # if flag == 1: + # # 显示带检测结果的融合图像 + # cv2.imshow("Fusion with YOLOv8 Detection", fusion) + # out.write(fusion) + # + # if cv2.waitKey(1) == ord('q'): + # break + # # 释放资源 + # capture.release() + # capture2.release() + # cv2.destroyAllWindows() + # ave = time_all / i + # print(ave, "平均时间") + # cv2.destroyAllWindows() + + # === 新增静态图片测试代码 === + # 输入可见光和红外图像路径 + visible_path = "../test_images/visible.jpg" # 可见光图片路径 + infrared_path = "../test_images/infrared.jpg" # 红外图片路径 + + # 读取图像 + img_visible = cv2.imread(visible_path) + img_infrared = cv2.imread(infrared_path) + + if img_visible is None or img_infrared is None: + print("Error: 图片加载失败,请检查路径!") + exit() + + # 转换为灰度图(红外图像处理) + img_inf_gray = cv2.cvtColor(img_infrared, cv2.COLOR_BGR2GRAY) + + # 执行融合与检测 + flag, fusion_result, _ = main(img_visible, img_inf_gray) + + if flag == 1: + # 显示并保存结果 + cv2.imshow("Fusion with Detection", fusion_result) + cv2.imwrite("../output/fusion_result.jpg", fusion_result) + cv2.waitKey(0) + cv2.destroyAllWindows() + else: + print("融合失败!")