open3d保存的ply文件乱码

发布时间 2023-11-28 21:06:54作者: 多加辣

出现的问题

1)我的代码如下

import cv2
import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt
from plyfile import PlyData, PlyElement
if __name__ == '__main__':
depth = o3d.t.io.read_image('D:/biaoding/lion/depth_5.png')
# print(np.array(depth))
intrinsic = o3d.core.Tensor([[365.7593, 0, 254.2594], [0, 365.7593, 204.1283],
[0, 0, 1]])
pcd = o3d.t.geometry.PointCloud.create_from_depth_image(depth,
intrinsic,
depth_scale=4000.0,
depth_max=0.8)
point_cloud_array = pcd.point.positions.numpy()
   pcd1 = o3d.geometry.PointCloud()
   pcd1.points = o3d.utility.Vector3dVector(point_cloud_array)
   o3d.io.write_point_cloud("depth.ply", pcd1)
2)我的.ply文件打开的样子

原因和解决方案:

       在保存PLY文件时,Open3D默认使用二进制格式,并且不提供直接指定编码方式的选项。在保存多维数组作为点云数据时,可以使用numpy.savetxt()函数将数据以ASCII格式保存到文件中。然后,使用open()函数以ASCII编码打开文件,并将数据逐行写入PLY文件。

解决代码:

# 以ASCII格式保存点云数据到文件
np.savetxt('point_cloud.txt', point_cloud_array, delimiter=' ', fmt='%.6f')

# 打开文件并逐行写入PLY文件
with open('point_cloud.txt', 'r', encoding='ascii') as file:
lines = file.readlines()
num_points = len(lines)

# 创建PLY文件并写入头信息
with open('point.ply', 'w', encoding='ascii') as ply_file:
ply_file.write('ply\n')
ply_file.write('format ascii 1.0\n')
ply_file.write('element vertex {}\n'.format(num_points))
ply_file.write('property float x\n')
ply_file.write('property float y\n')
ply_file.write('property float z\n')
ply_file.write('end_header\n')

# 逐行写入点云数据
for line in lines:
ply_file.write(line)