当前位置:   article > 正文

利用SuperGlue算法实现跨尺度金字塔特征点的高效匹配(含py代码)_superglue 匹配

superglue 匹配

       在计算机视觉领域,特征点匹配是一个基础而关键的任务,广泛应用于图像拼接、三维重建、目标跟踪等方向。传统的特征点匹配方法通常基于相同尺度下提取的特征进行匹配,然而在实际场景中,由于成像距离、分辨率等因素的差异,待匹配图像间存在显著的尺度变化,直接利用原始尺度的特征难以获得理想的匹配效果。为了克服这一难题,构建图像金字塔并在不同层级进行特征提取和匹配成为一种行之有效的策略。本文将给出如何使用图神经网络匹配算法SuperGlue的代码,实现跨金字塔层级的特征点高效匹配,充分利用不同尺度信息,显著提升匹配的准确性和鲁棒性。

1. 文件结构

2. 具体代码 

  1. #! /usr/bin/env python3
  2. import cv2
  3. import torch # 这一句
  4. torch.set_grad_enabled(False) # 这一句
  5. from models.matching import Matching # 这一句
  6. from models.utils import (frame2tensor) # 这一句
  7. import numpy as np
  8. config = {
  9. 'superpoint': {
  10. 'nms_radius': 4,
  11. 'keypoint_threshold': 0.005,
  12. 'max_keypoints': -1
  13. },
  14. 'superglue': {
  15. 'weights': 'outdoor',
  16. 'sinkhorn_iterations': 20,
  17. 'match_threshold': 0.2,
  18. }
  19. }
  20. #
  21. # device = 'cuda' if torch.cuda.is_available() else 'cpu'
  22. device = 'cuda'
  23. matching = Matching(config).eval().to(device) # 这一句
  24. keys = ['keypoints', 'scores', 'descriptors']
  25. ######################################################################################################
  26. def match_frames_with_super_glue(frame0,frame1):
  27. print("正在调用基于 superGlue 匹配的函数进行特征点匹配...") # 添加了print语句
  28. # 将参考帧和当前帧转换为PyTorch张量格式
  29. frame_tensor0 = frame2tensor(frame0, device)
  30. frame_tensor1 = frame2tensor(frame1, device)
  31. # 使用SuperPoint网络提取参考帧的特征点
  32. last_data = matching.superpoint({'image': frame_tensor0})
  33. # 将提取到的参考帧特征点数据转换为字典格式
  34. last_data = {k + '0': last_data[k] for k in keys}
  35. last_data['image0'] = frame_tensor0
  36. # 获取参考帧的特征点坐标
  37. kpts0 = last_data['keypoints0'][0].cpu().numpy()
  38. # 使用SuperGlue网络在参考帧和当前帧之间进行特征点匹配
  39. pred = matching({**last_data, 'image1': frame_tensor1})
  40. # 获取当前帧的特征点坐标
  41. kpts1 = pred['keypoints1'][0].cpu().numpy()
  42. # 获取特征点匹配结果和匹配置信度
  43. matches = pred['matches0'][0].cpu().numpy()
  44. confidence = pred['matching_scores0'][0].cpu().numpy()
  45. # 筛选出有效的匹配对
  46. valid = matches > -1
  47. mkpts0 = kpts0[valid]
  48. mkpts1 = kpts1[matches[valid]]
  49. # 打印匹配结果
  50. #
  51. # print(f"----已经完成帧间的关键点匹配----")
  52. for i, (kp0, kp1) in enumerate(zip(mkpts0, mkpts1)):
  53. print(f"Match {i}: ({kp0[0]:.2f}, {kp0[1]:.2f}) -> ({kp1[0]:.2f}, {kp1[1]:.2f})")
  54. # 确保两个图像都是三通道
  55. if len(frame0.shape) == 2:
  56. vis_frame0 = cv2.cvtColor(frame0, cv2.COLOR_GRAY2BGR)
  57. else:
  58. vis_frame0 = frame0.copy()
  59. if len(frame1.shape) == 2:
  60. vis_frame1 = cv2.cvtColor(frame1, cv2.COLOR_GRAY2BGR)
  61. else:
  62. vis_frame1 = frame1.copy()
  63. # 绘制第一个输入图像及其特征点
  64. vis_frame0_with_kpts = vis_frame0.copy()
  65. for kp in kpts0:
  66. cv2.circle(vis_frame0_with_kpts, (int(kp[0]), int(kp[1])), 3, (0, 255, 0), -1)
  67. cv2.imshow("Input Frame 0 with Keypoints", vis_frame0_with_kpts)
  68. # 绘制第二个输入图像及其特征点
  69. vis_frame1_with_kpts = vis_frame1.copy()
  70. for kp in kpts1:
  71. cv2.circle(vis_frame1_with_kpts, (int(kp[0]), int(kp[1])), 3, (0, 255, 0), -1)
  72. cv2.imshow("Input Frame 1 with Keypoints", vis_frame1_with_kpts)
  73. # 绘制特征点
  74. for kp in mkpts0:
  75. cv2.circle(vis_frame0, (int(kp[0]), int(kp[1])), 3, (0, 255, 0), -1)
  76. for kp in mkpts1:
  77. cv2.circle(vis_frame1, (int(kp[0]), int(kp[1])), 3, (0, 255, 0), -1)
  78. # 调整高度一致,通过在较短的图像上下填充黑色背景
  79. max_height = max(vis_frame0.shape[0], vis_frame1.shape[0])
  80. if vis_frame0.shape[0] < max_height:
  81. diff = max_height - vis_frame0.shape[0]
  82. pad_top = np.zeros((diff // 2, vis_frame0.shape[1], 3), dtype=np.uint8)
  83. pad_bottom = np.zeros((diff - diff // 2, vis_frame0.shape[1], 3), dtype=np.uint8)
  84. vis_frame0 = np.vstack((pad_top, vis_frame0, pad_bottom))
  85. if vis_frame1.shape[0] < max_height:
  86. diff = max_height - vis_frame1.shape[0]
  87. pad_top = np.zeros((diff // 2, vis_frame1.shape[1], 3), dtype=np.uint8)
  88. pad_bottom = np.zeros((diff - diff // 2, vis_frame1.shape[1], 3), dtype=np.uint8)
  89. vis_frame1 = np.vstack((pad_top, vis_frame1, pad_bottom))
  90. # 计算右侧图像的垂直偏移量
  91. right_pad_top = pad_top.shape[0]
  92. # 绘制匹配线段
  93. concat_frame = np.hstack((vis_frame0, vis_frame1))
  94. for kp0, kp1 in zip(mkpts0, mkpts1):
  95. pt0 = (int(kp0[0]), int(kp0[1]))
  96. pt1 = (int(kp1[0]) + vis_frame0.shape[1], int(kp1[1]) + right_pad_top)
  97. cv2.line(concat_frame, pt0, pt1, (0, 255, 0), 1)
  98. # 缩小可视化窗口大小
  99. scale_factor = 1
  100. resized_frame = cv2.resize(concat_frame, None, fx=scale_factor, fy=scale_factor)
  101. # 显示可视化结果
  102. cv2.imshow("Matched Features", resized_frame)
  103. cv2.waitKey(0)
  104. cv2.destroyAllWindows()
  105. return mkpts0, mkpts1, confidence[valid]
  106. def build_pyramid(image, scale=1.2, min_size=(30, 30)):
  107. pyramid = [image]
  108. while True:
  109. last_image = pyramid[-1]
  110. width = int(last_image.shape[1] / scale)
  111. height = int(last_image.shape[0] / scale)
  112. if width < min_size[0] or height < min_size[1]:
  113. break
  114. next_image = cv2.resize(last_image, (width, height))
  115. pyramid.append(next_image)
  116. return pyramid
  117. if __name__ == "__main__":
  118. # 读取两帧图像
  119. frame0 = cv2.imread("/home/fairlee/786D6A341753F4B4/KITTI/sequences_kitti_00_21/01/image_0/000630.png", 0)
  120. frame1 = cv2.imread("/home/fairlee/786D6A341753F4B4/KITTI/sequences_kitti_00_21/01/image_0/000631.png", 0)
  121. # 构建 frame1 的金字塔
  122. pyramid1 = build_pyramid(frame1, scale=1.2)
  123. # # # 显示金字塔层
  124. # for i, layer in enumerate(pyramid1):
  125. # cv2.imshow(f"Layer {i}", layer)
  126. # cv2.waitKey(500) # 显示500毫秒
  127. # cv2.destroyAllWindows()
  128. # 选择合适的金字塔层作为 frame1 的替代
  129. frame1_substitute = pyramid1[2] # 例如,选择第二层
  130. # 调用match_frames_with_super_glue函数进行特征点匹配
  131. mkpts0, mkpts1, confidence = match_frames_with_super_glue(frame0, frame1_substitute)
  132. # 打印匹配结果
  133. print(f"第一帧的特征点匹配到的特征点数量: {len(mkpts0)}")
  134. print(f"第二帧的特征点匹配到的特征点数量: {len(mkpts1)}")
  135. print(f"匹配置信度的长度为: {len(confidence)}")

3. 运行结果

       代码实现展示了该方法的具体流程,通过选取合适的金字塔层作为待匹配图像的替代,实现了跨尺度的特征点匹配。实验结果表明,该方法能够有效地处理存在显著尺度变化的图像,获得数量可观且置信度较高的匹配点对,为后续的图像拼接、三维重建等任务提供了重要的基础。该方法的优越性在于巧妙地结合了图像金字塔的多尺度表示和SuperGlue的强大匹配能力,为解决复杂场景下的特征匹配难题提供了新的思路和方案。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/890931
推荐阅读
相关标签
  

闽ICP备14008679号