test_tiff.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 ,r,g,b= 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, :] = (X,Y,Z)
  27. return point_image
  28. def pointscloud2colorimg(points):
  29. # 初始化一个空的目标数组
  30. point_image = np.zeros((height, width, 3), dtype=np.float32)
  31. color_image = np.zeros((height, width, 3), dtype=np.float32)
  32. # 遍历点云中的每个点,进行投影并填充目标数组
  33. for point in points:
  34. X, Y, Z ,r,g,b= point
  35. if Z > 0: # 确保Z值有效
  36. # 计算2D图像坐标
  37. u = int((X * fx) / Z + cx)
  38. v = int((Y * fy) / Z + cy)
  39. # 检查是否在图像边界内
  40. if 0 <= u < width and 0 <= v < height:
  41. # point_image[v, u, :] = point
  42. color_image[v,u]=[r*255,g*255,b*255]
  43. return color_image
  44. # # 使用imageio读取
  45. # loaded_depth_map = imageio.v3.imread(r"depth_map.tiff")
  46. # print(loaded_depth_map.shape)
  47. # print(loaded_depth_map.dtype)
  48. # # print(loaded_depth_map)
  49. # 加载PCD文件
  50. pcd = o3d.io.read_point_cloud(r"F:\test_pointcloud\color2.pcd")
  51. # 打印点的数量
  52. print("Number of points:", len(pcd.points))
  53. # 获取点云数据
  54. points = np.asarray(pcd.points)
  55. colors=np.asarray(pcd.colors)
  56. points = np.hstack((points, colors)) # (N, 6)
  57. # 打印前5个点的坐标
  58. print("First 5 points:\n", points[:5])
  59. print(f'color:{colors}')
  60. #
  61. # print(f'depth :{loaded_depth_map[0,0:5]}')
  62. #
  63. #
  64. # print(loaded_depth_map[102,113])
  65. #
  66. # # 将深度图转换为点云
  67. # height ,width = loaded_depth_map.shape[:2]
  68. # print(f'height:{height},width:{width}')
  69. # point_cloud_from_depth = []
  70. # for v in range(height):
  71. # for u in range(width):
  72. # x_,y_,z_=loaded_depth_map[v,u]
  73. # print(f'x_,y_,z_:({x_},{y_},{z_})')
  74. # depth = loaded_depth_map[v, u][-1]
  75. # print(f'depth:{depth}')
  76. # # if depth > 0: # 忽略无效的深度值
  77. # x = (u - cx) * depth / fx
  78. # y = (v - cy) * depth / fy
  79. # z = depth
  80. # print(f'x,y,z:({x},{y},{z})')
  81. # point_cloud_from_depth.append([x, y, z])
  82. #
  83. # point_cloud_from_depth = np.array(point_cloud_from_depth)
  84. #
  85. # # 打印从深度图生成的点云中的前5个点
  86. # print("First 5 points from depth map:\n", point_cloud_from_depth[:5])
  87. # 目标深度图尺寸
  88. height, width = 2000, 2000
  89. # point_image=pointscloud2depthmap(points)
  90. point_image=pointscloud2colorimg(points)
  91. # 打印结果以验证
  92. print("Shape of the projected point cloud:", point_image.shape)
  93. print("First few pixels (if any):", point_image[:5, :5, :])
  94. # 提取 Z 值作为深度图
  95. depth_map = point_image[:, :, 2]
  96. # depth_map=point_image
  97. # 处理无效点(例如,设置无效点的深度值为一个极大值)
  98. # invalid_depth_value = np.max(depth_map) * 2 # 或者选择其他合适的值
  99. # depth_map[depth_map == 0] = invalid_depth_value # 将所有无效点(Z=0)替换为极大值
  100. # 打印深度图的一些信息以验证
  101. print("Depth map shape:", depth_map.shape)
  102. print("Depth map dtype:", depth_map.dtype)
  103. print("Min depth value:", np.min(depth_map))
  104. print("Max depth value:", np.max(depth_map))
  105. # 保存为 TIFF 文件
  106. # output_tiff_path = 'depth_map.tiff'
  107. color_img_path='color_img.jpg'
  108. # tifffile.imwrite(output_tiff_path, depth_map.astype(np.float16))
  109. cv2.imwrite(color_img_path,point_image)
  110. # print(f"Depth map saved to {output_tiff_path}")