get_full_stitch_order.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. def get_full_stitch_order(row_num: int, column_num: int,debug=False) -> dict:
  2. """
  3. 生成图像拼接顺序
  4. Args:
  5. row_num: 总行数
  6. column_num: 总列数
  7. Returns:
  8. dict: 拼接顺序字典,每个key是拼接步骤序号,value是一个元组(轮数, 图片1, 图片2, 拼接方向, 结果)
  9. """
  10. result_dict = {}
  11. stitch_idx = [1] # 使用列表以便在递归中修改
  12. round_num = [1] # 使用列表以便在递归中修改
  13. # 初始化图像矩阵
  14. initial_matrix = []
  15. for i in range(row_num):
  16. row = []
  17. for j in range(column_num):
  18. row.append(str(i * column_num + j + 1))
  19. initial_matrix.append(row)
  20. if debug:
  21. print("\nInitial matrix:")
  22. for row in initial_matrix:
  23. print(row)
  24. def recursive_stitch(matrix, direction="horizontal"):
  25. """
  26. 递归执行拼接操作
  27. Args:
  28. matrix: 当前待处理的矩阵
  29. direction: 当前的拼接方向 ("horizontal" 或 "vertical")
  30. Returns:
  31. 处理后的矩阵
  32. """
  33. # 基本情况:矩阵只有一个元素
  34. if len(matrix) == 1 and len(matrix[0]) == 1:
  35. return matrix
  36. new_matrix = []
  37. has_stitching = False
  38. if direction == "horizontal":
  39. # 水平拼接
  40. for row in matrix:
  41. new_row = []
  42. for i in range(0, len(row), 2):
  43. if i + 1 < len(row):
  44. # 两两水平拼接
  45. new_name = f"{row[i]}_{row[i+1]}"
  46. result_dict[stitch_idx[0]] = (round_num[0], row[i], row[i+1],
  47. direction, new_name)
  48. stitch_idx[0] += 1
  49. new_row.append(new_name)
  50. has_stitching = True
  51. else:
  52. new_row.append(row[i])
  53. new_matrix.append(new_row)
  54. else: # vertical
  55. # 垂直拼接
  56. max_cols = max(len(row) for row in matrix)
  57. transposed_matrix = []
  58. # 构建转置矩阵
  59. for col in range(max_cols):
  60. col_elements = []
  61. for row in matrix:
  62. if col < len(row):
  63. col_elements.append(row[col])
  64. transposed_matrix.append(col_elements)
  65. # 处理每一列(现在是转置后的行)
  66. result_matrix = []
  67. for col in transposed_matrix:
  68. new_col = []
  69. for i in range(0, len(col), 2):
  70. if i + 1 < len(col):
  71. new_name = f"{col[i]}_{col[i+1]}"
  72. result_dict[stitch_idx[0]] = (round_num[0], col[i],
  73. col[i+1], direction, new_name)
  74. stitch_idx[0] += 1
  75. new_col.append(new_name)
  76. has_stitching = True
  77. else:
  78. new_col.append(col[i])
  79. result_matrix.append(new_col)
  80. # 转置回来
  81. new_matrix = [[row[i] for row in result_matrix if i < len(row)]
  82. for i in range(max(len(row) for row in result_matrix))]
  83. # 如果本轮有拼接操作,增加轮数并打印当前矩阵
  84. if has_stitching:
  85. round_num[0] += 1
  86. if debug:
  87. print(f"\n当前的方向为:{direction}")
  88. print(f"\nAfter Round {round_num[0]} :")
  89. for row in new_matrix:
  90. print(row)
  91. # 判断是否为最后一轮(只剩下一行多列)
  92. if len(new_matrix) == 1 and len(new_matrix[0]) > 1:
  93. # 最后一轮,水平拼接剩余的所有元素
  94. final_row = new_matrix[0]
  95. final_result = []
  96. for i in range(0, len(final_row), 2):
  97. if i + 1 < len(final_row):
  98. new_name = f"{final_row[i]}_{final_row[i+1]}"
  99. result_dict[stitch_idx[0]] = (round_num[0], final_row[i],
  100. final_row[i+1], "horizontal", new_name)
  101. stitch_idx[0] += 1
  102. final_result.append(new_name)
  103. else:
  104. final_result.append(final_row[i])
  105. new_matrix = [final_result]
  106. if debug:
  107. print(f"\nFinal Round {round_num[0]} (horizontal):")
  108. print(new_matrix)
  109. return new_matrix
  110. # 确定下一轮的拼接方向
  111. next_direction = "vertical" if direction == "horizontal" else "horizontal"
  112. # 继续递归
  113. if len(new_matrix) > 1 or len(new_matrix[0]) > 1:
  114. return recursive_stitch(new_matrix, next_direction)
  115. return new_matrix
  116. # 开始递归拼接
  117. recursive_stitch(initial_matrix)
  118. return result_dict
  119. def test_stitch_images():
  120. # 测试2×2的情况
  121. print("Testing 2×2:")
  122. result = get_full_stitch_order(2, 2)
  123. print("\nStitch steps:")
  124. for step, (round_num, img1, img2, direction, result_name) in result.items():
  125. print(f"Step {step} (Round {round_num}): {img1} + {img2} ({direction}) -> {result_name}")
  126. print("\n" + "="*50 + "\nTesting 3×3:")
  127. result = get_full_stitch_order(3, 3)
  128. print("\nStitch steps:")
  129. for step, (round_num, img1, img2, direction, result_name) in result.items():
  130. print(f"Step {step} (Round {round_num}): {img1} + {img2} ({direction}) -> {result_name}")
  131. print("\n" + "="*50 + "\nTesting 4×6:")
  132. result = get_full_stitch_order(4, 6)
  133. print("\nStitch steps:")
  134. for step, (round_num, img1, img2, direction, result_name) in result.items():
  135. print(f"Step {step} (Round {round_num}): {img1} + {img2} ({direction}) -> {result_name}")
  136. print("\n" + "="*50 + "\nTesting 7×5:")
  137. result = get_full_stitch_order(7, 5,debug=True)
  138. print("\nStitch steps:")
  139. for step, (round_num, img1, img2, direction, result_name) in result.items():
  140. print(f"Step {step} (Round {round_num}): {img1} + {img2} ({direction}) -> {result_name}")
  141. if __name__ == "__main__":
  142. test_stitch_images()