|
|
@@ -17,7 +17,7 @@ def pointscloud2depthmap(points):
|
|
|
point_image = np.zeros((height, width, 3), dtype=np.float32)
|
|
|
# 遍历点云中的每个点,进行投影并填充目标数组
|
|
|
for point in points:
|
|
|
- X, Y, Z = point
|
|
|
+ X, Y, Z ,r,g,b= point
|
|
|
if Z > 0: # 确保Z值有效
|
|
|
# 计算2D图像坐标
|
|
|
u = int((X * fx) / Z + cx)
|
|
|
@@ -28,7 +28,24 @@ def pointscloud2depthmap(points):
|
|
|
point_image[v, u, :] = point
|
|
|
|
|
|
return point_image
|
|
|
+def pointscloud2colorimg(points):
|
|
|
+ # 初始化一个空的目标数组
|
|
|
+ point_image = np.zeros((height, width, 3), dtype=np.float32)
|
|
|
+ color_image = np.zeros((height, width, 3), dtype=np.float32)
|
|
|
+ # 遍历点云中的每个点,进行投影并填充目标数组
|
|
|
+ for point in points:
|
|
|
+ X, Y, Z ,r,g,b= point
|
|
|
+ if Z > 0: # 确保Z值有效
|
|
|
+ # 计算2D图像坐标
|
|
|
+ u = int((X * fx) / Z + cx)
|
|
|
+ v = int((Y * fy) / Z + cy)
|
|
|
+
|
|
|
+ # 检查是否在图像边界内
|
|
|
+ if 0 <= u < width and 0 <= v < height:
|
|
|
+ # point_image[v, u, :] = point
|
|
|
+ color_image[v,u]=[r*255,g*255,b*255]
|
|
|
|
|
|
+ return color_image
|
|
|
|
|
|
|
|
|
# # 使用imageio读取
|
|
|
@@ -41,7 +58,7 @@ def pointscloud2depthmap(points):
|
|
|
|
|
|
|
|
|
# 加载PCD文件
|
|
|
-pcd = o3d.io.read_point_cloud(r"F:\DevTools\datasets\test.pcd")
|
|
|
+pcd = o3d.io.read_point_cloud(r"F:\test_pointcloud\color.pcd")
|
|
|
|
|
|
# 打印点的数量
|
|
|
print("Number of points:", len(pcd.points))
|
|
|
@@ -49,8 +66,12 @@ print("Number of points:", len(pcd.points))
|
|
|
# 获取点云数据
|
|
|
points = np.asarray(pcd.points)
|
|
|
|
|
|
+colors=np.asarray(pcd.colors)
|
|
|
+points = np.hstack((points, colors)) # (N, 6)
|
|
|
+
|
|
|
# 打印前5个点的坐标
|
|
|
print("First 5 points:\n", points[:5])
|
|
|
+print(f'color:{colors}')
|
|
|
#
|
|
|
# print(f'depth :{loaded_depth_map[0,0:5]}')
|
|
|
#
|
|
|
@@ -91,8 +112,8 @@ height, width = 2000, 2000
|
|
|
|
|
|
|
|
|
|
|
|
-point_image=pointscloud2depthmap(points)
|
|
|
-
|
|
|
+# point_image=pointscloud2depthmap(points)
|
|
|
+point_image=pointscloud2colorimg(points)
|
|
|
# 打印结果以验证
|
|
|
print("Shape of the projected point cloud:", point_image.shape)
|
|
|
print("First few pixels (if any):", point_image[:5, :5, :])
|
|
|
@@ -111,8 +132,10 @@ print("Min depth value:", np.min(depth_map))
|
|
|
print("Max depth value:", np.max(depth_map))
|
|
|
|
|
|
# 保存为 TIFF 文件
|
|
|
-output_tiff_path = 'depth_map.tiff'
|
|
|
-tifffile.imwrite(output_tiff_path, depth_map.astype(np.float16))
|
|
|
+# output_tiff_path = 'depth_map.tiff'
|
|
|
+color_img_path='color_img.jpg'
|
|
|
|
|
|
-print(f"Depth map saved to {output_tiff_path}")
|
|
|
+# tifffile.imwrite(output_tiff_path, depth_map.astype(np.float16))
|
|
|
+cv2.imwrite(color_img_path,point_image)
|
|
|
+# print(f"Depth map saved to {output_tiff_path}")
|
|
|
|