val_expansion.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import json
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import cv2
  6. from tifffile import tifffile
  7. def draw_lines_on_resized_tiff(json_path, tiff_dir, output_dir=None):
  8. with open(json_path, 'r', encoding='utf-8') as f:
  9. data = json.load(f)
  10. if output_dir:
  11. os.makedirs(output_dir, exist_ok=True)
  12. target_width, target_height = 826, 818 # 目标尺寸
  13. for item in data:
  14. original_img_name = item["image"]
  15. base_name = os.path.splitext(original_img_name)[0].replace("_color", "_depth")
  16. tiff_name = "filled_" + base_name + ".tiff"
  17. tiff_path = os.path.join(tiff_dir, tiff_name)
  18. if not os.path.exists(tiff_path):
  19. print(f"[!] TIFF file not found: {tiff_path}")
  20. continue
  21. tiff_np = tifffile.imread(tiff_path)
  22. orig_height, orig_width = tiff_np.shape[:2]
  23. resized_img = cv2.resize(tiff_np, (target_width, target_height), interpolation=cv2.INTER_LINEAR)
  24. fig, ax = plt.subplots(figsize=(target_width / 100, target_height / 100), dpi=100)
  25. if resized_img.ndim == 2:
  26. ax.imshow(resized_img, cmap='gray')
  27. else:
  28. ax.imshow(resized_img)
  29. scale_x = target_width / orig_width
  30. scale_y = target_height / orig_height
  31. for line in item["lines"]:
  32. start_2000 = np.array(line["start"])
  33. end_2000 = np.array(line["end"])
  34. scale_line_x = orig_width / 2000
  35. scale_line_y = orig_height / 2000
  36. start_orig = start_2000 * np.array([scale_line_x, scale_line_y])
  37. end_orig = end_2000 * np.array([scale_line_x, scale_line_y])
  38. start = start_orig * np.array([scale_x, scale_y])
  39. end = end_orig * np.array([scale_x, scale_y])
  40. x0, y0 = int(round(start[0])), int(round(start[1]))
  41. x1, y1 = int(round(end[0])), int(round(end[1]))
  42. x0 = np.clip(x0, 0, target_width - 1)
  43. y0 = np.clip(y0, 0, target_height - 1)
  44. x1 = np.clip(x1, 0, target_width - 1)
  45. y1 = np.clip(y1, 0, target_height - 1)
  46. val_start = resized_img[y0, x0]
  47. val_end = resized_img[y1, x1]
  48. ax.plot([x0, x1], [y0, y1], 'r-', linewidth=1)
  49. ax.scatter([x0, x1], [y0, y1], c='blue', s=10)
  50. ax.text(x0, y0, f"{val_start}", color='white', fontsize=8,
  51. bbox=dict(facecolor='black', alpha=0.5))
  52. ax.text(x1, y1, f"{val_end}", color='white', fontsize=8,
  53. bbox=dict(facecolor='black', alpha=0.5))
  54. ax.set_title(original_img_name)
  55. ax.axis('off')
  56. if output_dir:
  57. save_name = base_name + "_vis_resized.jpg"
  58. save_path = os.path.join(output_dir, save_name)
  59. plt.savefig(save_path, bbox_inches='tight', dpi=100)
  60. plt.close(fig)
  61. else:
  62. plt.show()
  63. print("✅ 所有图像处理完成。")
  64. if __name__ == '__main__':
  65. json_path = r"G:\python_ws_g\data\pcd2color_result\a_predict_restnet50\predictions.json"
  66. tiff_dir = r"G:\python_ws_g\data\pcd2color_result\dilated_tiff_filled"
  67. output_dir = r"G:\python_ws_g\data\pcd2color_result\line_vis_resized"
  68. draw_lines_on_resized_tiff(json_path, tiff_dir, output_dir)