key_point_test.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # --- START OF FILE key_point_test.py ---
  2. import cv2
  3. import os
  4. import time
  5. from pathlib import Path
  6. import re
  7. from tqdm import tqdm
  8. # 导入您提供的拼接器类和拼接顺序生成器
  9. from fry_project_classes.stitch_img_key_point import ImageStitcherKeyPoint
  10. from fry_project_classes.get_full_stitch_order import get_full_stitch_order
  11. def natural_sort_key(s):
  12. """
  13. 提供自然排序的键,例如 '2.jpg' 会排在 '10.jpg' 之前。
  14. """
  15. return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', str(s))]
  16. # --- 重构后的 stitch_img 函数 ---
  17. def stitch_img(IMAGE_DIR, OUTPUT_DIR, NUM_COLS: int, NUM_ROWS: int,
  18. ESTIMATE_OVERLAP_HORIZONTAL_PIXELS: int, ESTIMATE_OVERLAP_VERTICAL_PIXELS: int,
  19. BLEND_TYPE: str, FEATURE_DETECTOR: str, DEBUG_MODE: bool,
  20. BLEND_RATIO: float, LIGHT_COMPENSATION: bool, LIGHT_COMPENSATION_WIDTH: int):
  21. OUTPUT_DIR.mkdir(exist_ok=True)
  22. print("--- 图像拼接开始 ---")
  23. print(f"配置: {NUM_ROWS}行 x {NUM_COLS}列")
  24. print(f"图片目录: {IMAGE_DIR}")
  25. print(f"输出目录: {OUTPUT_DIR}")
  26. print(f"水平重叠预估: {ESTIMATE_OVERLAP_HORIZONTAL_PIXELS}px, 垂直重叠预估: {ESTIMATE_OVERLAP_VERTICAL_PIXELS}px")
  27. print(f"特征检测器: {FEATURE_DETECTOR}, 融合模式: {BLEND_TYPE}, 融合权重: {BLEND_RATIO}")
  28. print(f"光照补偿: {'启用' if LIGHT_COMPENSATION else '禁用'}, 补偿宽度: {LIGHT_COMPENSATION_WIDTH}px")
  29. # --- 1. 加载并排序所有图片 ---
  30. image_paths = sorted(list(IMAGE_DIR.glob("*.jpg")), key=natural_sort_key)
  31. if len(image_paths) != NUM_COLS * NUM_ROWS:
  32. print(f"错误: 找到 {len(image_paths)} 张图片, 但预期需要 {NUM_COLS * NUM_ROWS} 张。")
  33. return
  34. # 将所有图片读入内存,并用一个字典存储
  35. images_dict = {}
  36. for i, path in enumerate(image_paths):
  37. img = cv2.imread(str(path))
  38. if img is None:
  39. print(f"错误: 无法读取图片 {path}")
  40. return
  41. # 使用从 '1' 开始的字符串作为键
  42. images_dict[str(i + 1)] = img
  43. # --- 2. 获取拼接顺序 ---
  44. full_stitch_order_dict = get_full_stitch_order(NUM_ROWS, NUM_COLS)
  45. print(f"\n--- 获取到 {len(full_stitch_order_dict)} 步拼接指令 ---")
  46. # --- 3. 按照指令集执行拼接 ---
  47. final_image = None
  48. progress_bar = tqdm(full_stitch_order_dict.items(), desc="执行拼接")
  49. for step, (round_num, img1_name, img2_name, direction, result_name) in progress_bar:
  50. progress_bar.set_description(f"步骤 {step}: {img1_name} + {img2_name} -> {result_name}")
  51. img1 = images_dict[img1_name]
  52. img2 = images_dict[img2_name]
  53. # 根据方向选择重叠像素
  54. overlap_pixels = 0
  55. if direction == 'horizontal':
  56. overlap_pixels = ESTIMATE_OVERLAP_HORIZONTAL_PIXELS
  57. elif direction == 'vertical':
  58. overlap_pixels = ESTIMATE_OVERLAP_VERTICAL_PIXELS
  59. else:
  60. raise ValueError(f"未知的拼接方向: {direction}")
  61. # 每次都创建一个新的拼接器实例
  62. stitcher = ImageStitcherKeyPoint(
  63. estimate_overlap_pixels=overlap_pixels,
  64. stitch_type=direction,
  65. blend_type=BLEND_TYPE,
  66. feature_detector=FEATURE_DETECTOR,
  67. blend_ratio=BLEND_RATIO,
  68. # 同样添加光照补偿参数,供底层的融合模块使用
  69. light_uniformity_compensation_enabled=LIGHT_COMPENSATION,
  70. light_uniformity_compensation_width=LIGHT_COMPENSATION_WIDTH,
  71. debug=DEBUG_MODE,
  72. debug_dir=str(OUTPUT_DIR / f'debug_{result_name}')
  73. )
  74. # 执行拼接
  75. stitched_image = stitcher.stitch_main(img1, img2)
  76. # 将新生成的图片存入字典,用于下一步拼接
  77. images_dict[result_name] = stitched_image
  78. final_image = stitched_image
  79. if DEBUG_MODE:
  80. intermediate_path = OUTPUT_DIR / f"intermediate_{result_name}.jpg"
  81. cv2.imwrite(str(intermediate_path), stitched_image)
  82. # --- 4. 保存最终结果 ---
  83. if final_image is not None:
  84. final_output_path = OUTPUT_DIR / "final_stitched_image.jpg"
  85. cv2.imwrite(str(final_output_path), final_image)
  86. print("\n--- 所有拼接任务完成!---")
  87. print(f"最终的全景图已保存至: {final_output_path}")
  88. else:
  89. print("\n--- 拼接失败,没有生成最终图像 ---")
  90. def main():
  91. """
  92. 主执行函数
  93. """
  94. # --- 1. 配置参数 ---
  95. # 图片和输出目录设置
  96. IMAGE_DIR = Path(r"C:\Code\ML\Project\StitchImageServer\temp\input\front_0_1")
  97. # 拼图网格设置
  98. NUM_COLS = 4
  99. NUM_ROWS = 6
  100. # !!!关键拼接参数,您可能需要根据实际图片进行调整!!!
  101. # 关键点匹配对这个参数不敏感,但它仍然用于界定初始搜索区域
  102. ESTIMATE_OVERLAP_HORIZONTAL_PIXELS = 405
  103. ESTIMATE_OVERLAP_VERTICAL_PIXELS = 440
  104. # --- 新增和修改的参数,与原始项目对齐 ---
  105. BLEND_RATIO = 0.5 # 融合权重,对 'half_importance_add_weight' 等模式有效
  106. LIGHT_COMPENSATION = True # 是否开启光照补偿
  107. LIGHT_COMPENSATION_WIDTH = 15 # 光照补偿的计算宽度 (请根据原始项目调整)
  108. # 可测试的融合模式列表
  109. blend_type_list = ["half_importance_add_weight"]
  110. # 可测试的特征检测器列表
  111. feature_detector_list = ["sift", "orb", "akaze", "brisk", "combine"]
  112. # 是否开启调试模式(会生成大量中间过程图片,用于分析问题)
  113. DEBUG_MODE = True
  114. for feature_detector in feature_detector_list:
  115. for blend_type in blend_type_list:
  116. base_dir_path = r"C:\Code\ML\Project\StitchImageServer\temp\output"
  117. # 创建更详细的输出文件夹名
  118. img_dir_name = f"keypoint_{feature_detector}_{blend_type}"
  119. OUTPUT_DIR = Path(os.path.join(base_dir_path, img_dir_name))
  120. print("\n" + "=" * 50)
  121. print(f"开始测试配置: 检测器={feature_detector}, 融合模式={blend_type}")
  122. print("=" * 50)
  123. one_config_time = time.time()
  124. stitch_img(
  125. IMAGE_DIR=IMAGE_DIR,
  126. OUTPUT_DIR=OUTPUT_DIR,
  127. NUM_COLS=NUM_COLS,
  128. NUM_ROWS=NUM_ROWS,
  129. ESTIMATE_OVERLAP_HORIZONTAL_PIXELS=ESTIMATE_OVERLAP_HORIZONTAL_PIXELS,
  130. ESTIMATE_OVERLAP_VERTICAL_PIXELS=ESTIMATE_OVERLAP_VERTICAL_PIXELS,
  131. BLEND_TYPE=blend_type,
  132. FEATURE_DETECTOR=feature_detector,
  133. DEBUG_MODE=DEBUG_MODE,
  134. BLEND_RATIO=BLEND_RATIO,
  135. LIGHT_COMPENSATION=LIGHT_COMPENSATION,
  136. LIGHT_COMPENSATION_WIDTH=LIGHT_COMPENSATION_WIDTH
  137. )
  138. print(f"配置 {img_dir_name} 完成, 耗时: {time.time() - one_config_time:.2f} 秒")
  139. if __name__ == '__main__':
  140. start_time = time.time()
  141. main()
  142. end_time = time.time()
  143. print(f"\n总耗时: {end_time - start_time:.2f} 秒")