test_tiff.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import cv2
  2. import numpy as np
  3. import imageio
  4. import open3d as o3d
  5. from tifffile import tifffile
  6. # 相机内参矩阵 [fx, 0, cx; 0, fy, cy; 0, 0, 1]
  7. K = np.array([
  8. [1.30449e3, 0, 5.2602e2],
  9. [0, 1.30449e3, 1.07432e3],
  10. [0, 0, 1]
  11. ])
  12. fx, fy = K[0, 0], K[1, 1]
  13. cx, cy = K[0, 2], K[1, 2]
  14. def pointscloud2depthmap(points):
  15. # 初始化一个空的目标数组
  16. point_image = np.zeros((height, width, 3), dtype=np.float32)
  17. # 遍历点云中的每个点,进行投影并填充目标数组
  18. for point in points:
  19. X, Y, Z = point
  20. if Z > 0: # 确保Z值有效
  21. # 计算2D图像坐标
  22. u = int((X * fx) / Z + cx)
  23. v = int((Y * fy) / Z + cy)
  24. # 检查是否在图像边界内
  25. if 0 <= u < width and 0 <= v < height:
  26. point_image[v, u, :] = point
  27. return point_image
  28. # # 使用imageio读取
  29. # loaded_depth_map = imageio.v3.imread(r"depth_map.tiff")
  30. # print(loaded_depth_map.shape)
  31. # print(loaded_depth_map.dtype)
  32. # # print(loaded_depth_map)
  33. # 加载PCD文件
  34. pcd = o3d.io.read_point_cloud(r"F:\DevTools\datasets\test.pcd")
  35. # 打印点的数量
  36. print("Number of points:", len(pcd.points))
  37. # 获取点云数据
  38. points = np.asarray(pcd.points)
  39. # 打印前5个点的坐标
  40. print("First 5 points:\n", points[:5])
  41. #
  42. # print(f'depth :{loaded_depth_map[0,0:5]}')
  43. #
  44. #
  45. # print(loaded_depth_map[102,113])
  46. #
  47. # # 将深度图转换为点云
  48. # height ,width = loaded_depth_map.shape[:2]
  49. # print(f'height:{height},width:{width}')
  50. # point_cloud_from_depth = []
  51. # for v in range(height):
  52. # for u in range(width):
  53. # x_,y_,z_=loaded_depth_map[v,u]
  54. # print(f'x_,y_,z_:({x_},{y_},{z_})')
  55. # depth = loaded_depth_map[v, u][-1]
  56. # print(f'depth:{depth}')
  57. # # if depth > 0: # 忽略无效的深度值
  58. # x = (u - cx) * depth / fx
  59. # y = (v - cy) * depth / fy
  60. # z = depth
  61. # print(f'x,y,z:({x},{y},{z})')
  62. # point_cloud_from_depth.append([x, y, z])
  63. #
  64. # point_cloud_from_depth = np.array(point_cloud_from_depth)
  65. #
  66. # # 打印从深度图生成的点云中的前5个点
  67. # print("First 5 points from depth map:\n", point_cloud_from_depth[:5])
  68. # 目标深度图尺寸
  69. height, width = 2000, 2000
  70. point_image=pointscloud2depthmap(points)
  71. # 打印结果以验证
  72. print("Shape of the projected point cloud:", point_image.shape)
  73. print("First few pixels (if any):", point_image[:5, :5, :])
  74. # 提取 Z 值作为深度图
  75. depth_map = point_image[:, :, 2]
  76. # depth_map=point_image
  77. # 处理无效点(例如,设置无效点的深度值为一个极大值)
  78. # invalid_depth_value = np.max(depth_map) * 2 # 或者选择其他合适的值
  79. # depth_map[depth_map == 0] = invalid_depth_value # 将所有无效点(Z=0)替换为极大值
  80. # 打印深度图的一些信息以验证
  81. print("Depth map shape:", depth_map.shape)
  82. print("Depth map dtype:", depth_map.dtype)
  83. print("Min depth value:", np.min(depth_map))
  84. print("Max depth value:", np.max(depth_map))
  85. # 保存为 TIFF 文件
  86. output_tiff_path = 'depth_map.tiff'
  87. tifffile.imwrite(output_tiff_path, depth_map.astype(np.float16))
  88. print(f"Depth map saved to {output_tiff_path}")