pmd.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import time
  2. import os
  3. import sys
  4. sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
  5. import time
  6. import glob
  7. import numpy as np
  8. np.random.seed(42)
  9. from datetime import datetime
  10. import json
  11. from scipy.io import savemat, loadmat
  12. from src.utils import get_meshgrid, get_world_points, get_camera_points, get_screen_points, write_point_cloud, get_white_mask, get_meshgrid_contour, post_process
  13. from src.phase import extract_phase, unwrap_phase
  14. from src.recons import reconstruction_cumsum
  15. import matplotlib.pyplot as plt
  16. from src.calibration import calibrate_world, calibrate_screen, map_screen_to_world
  17. import argparse
  18. from src.vis import plot_coords
  19. import cv2
  20. from src.eval import get_eval_result, find_notch
  21. import pickle
  22. def pmdstart(config_path, img_folder):
  23. start_time = time.time()
  24. print(f"config_path: {config_path}")
  25. #time.sleep(15)
  26. main(config_path, img_folder)
  27. print(f"img_folder: {img_folder}")
  28. print('test pass')
  29. end_time = time.time()
  30. print(f"Time taken: {end_time - start_time} seconds")
  31. return True
  32. def main(config_path, img_folder):
  33. cfg = json.load(open(config_path, 'r'))
  34. n_cam = 4
  35. num_freq = cfg['num_freq']
  36. save_path = None
  37. debug = False
  38. grid_spacing = cfg['grid_spacing']
  39. num_freq = cfg['num_freq']
  40. smooth = True
  41. align = True
  42. denoise = True
  43. print(f"开始执行时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  44. print("\n1. 相机标定")
  45. preprocess_start = time.time()
  46. with open(os.path.join('config', cfg['cam_params']), 'rb') as pkl_file:
  47. cam_params = pickle.load(pkl_file)
  48. with open(os.path.join('config', cfg['screen_params']), 'rb') as pkl_file:
  49. screen_params = pickle.load(pkl_file)[0]
  50. preprocess_end = time.time()
  51. print(f" 完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  52. print(f" 耗时: {preprocess_end - preprocess_start:.2f} 秒")
  53. # import pdb; pdb.set_trace()
  54. print("\n2. 屏幕标定")
  55. screen_cal_start = time.time()
  56. screen_to_world = map_screen_to_world(screen_params, cam_params[0])
  57. screen_cal_end = time.time()
  58. print(f" 完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  59. print(f" 耗时: {screen_cal_end - screen_cal_start:.2f} 秒")
  60. print("\n3. 相位提取,相位展开")
  61. phase_start = time.time()
  62. x_uns, y_uns = [], []
  63. binary_masks = []
  64. for cam_id in range(n_cam):
  65. white_path = os.path.join(img_folder, f'{cam_id}_frame_24.bmp')
  66. binary = get_white_mask(white_path)
  67. binary_masks.append(binary)
  68. phases = extract_phase(img_folder, cam_id, binary, cam_params[cam_id]['camera_matrix'], cam_params[cam_id]['distortion_coefficients'], num_freq=num_freq)
  69. x_un, y_un = unwrap_phase(phases, save_path, num_freq, debug=False)
  70. x_uns.append(x_un)
  71. y_uns.append(y_un)
  72. phase_end = time.time()
  73. print(f" 完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  74. print(f" 耗时: {phase_end - phase_start:.2f} 秒")
  75. print("\n4. 获得不同坐标系下点的位置")
  76. get_point_start = time.time()
  77. total_cloud_point = np.empty((0, 3))
  78. for i in range(n_cam):
  79. if i > 5:
  80. continue
  81. contours_point = get_meshgrid_contour(binary_masks[i], grid_spacing, save_path, debug=False)
  82. world_points = get_world_points(contours_point, cam_params[i], grid_spacing, cfg['d'], save_path, debug=debug)
  83. camera_points, u_p, v_p = get_camera_points(world_points, cam_params[i], save_path, i, debug=debug)
  84. point_data = {'x_w': world_points[:, 0], 'y_w': world_points[:, 1], 'z_w': world_points[:, 2],
  85. 'x_c': camera_points[:, 0], 'y_c': camera_points[:, 1], 'z_c': camera_points[:, 2],
  86. 'u_p': u_p, 'v_p': v_p}
  87. screen_points = get_screen_points(point_data, x_uns[i], y_uns[i], screen_params, screen_to_world, cfg, save_path, i, debug=debug)
  88. #plot_coords(world_points, camera_points, screen_points)
  89. z, smoothed, aligned, denoised = reconstruction_cumsum(world_points, camera_points, screen_points, save_path, i, debug=debug, smooth=smooth, align=align, denoise=denoise)
  90. write_point_cloud(os.path.join(img_folder, str(i) + '_cloudpoint.txt'), world_points[:, 0], world_points[:, 1], 1000 * denoised[:,2])
  91. total_cloud_point = np.vstack([total_cloud_point, np.column_stack((denoised[:, 0], denoised[:, 1], 1000 * denoised[:,2]))])
  92. write_point_cloud(os.path.join(img_folder, 'cloudpoint.txt'), total_cloud_point[:, 0], total_cloud_point[:, 1], total_cloud_point[:,2])
  93. if debug:
  94. fig = plt.figure()
  95. ax = fig.add_subplot(111, projection='3d')
  96. # 提取 x, y, z 坐标
  97. x_vals = total_cloud_point[:, 0]
  98. y_vals = total_cloud_point[:, 1]
  99. z_vals = total_cloud_point[:, 2]
  100. # 绘制3D点云
  101. ax.scatter(x_vals, y_vals, z_vals, c=z_vals, cmap='viridis', marker='o')
  102. # 设置轴标签和标题
  103. ax.set_xlabel('X (mm)')
  104. ax.set_ylabel('Y (mm)')
  105. ax.set_zlabel('Z (mm)')
  106. ax.set_title('3D Point Cloud Visualization')
  107. plt.show()
  108. get_point_end = time.time()
  109. print(f" 完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  110. print(f" 耗时: {get_point_end - get_point_start:.2f} 秒")
  111. print("\n5. 后处理")
  112. post_process_start = time.time()
  113. post_process(img_folder, debug)
  114. post_process_end = time.time()
  115. print(f" 完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  116. print(f" 耗时: {post_process_end - post_process_start:.2f} 秒")
  117. print("\n6. 评估")
  118. eval_start = time.time()
  119. point_cloud_path = os.path.join(img_folder, str(i) + '_cloudpoint.txt')
  120. json_path = os.path.join(img_folder, str(i) + 'result.json')
  121. theta_notch = 0
  122. get_eval_result(point_cloud_path, json_path, theta_notch)
  123. eval_end = time.time()
  124. print(f" 完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  125. print(f" 耗时: {eval_end - eval_start:.2f} 秒")
  126. return True
  127. if __name__ == '__main__':
  128. config_path = 'D:\\code\\pmdrecons-fourcam\\config\\cfg_3freq_wafer.json'
  129. img_folder = 'D:\\file\\20240913-data\\20240913105234292'
  130. pmdstart(config_path, img_folder)