问题描述
我有一个 .ply 格式模型并试图创建一个 .pcd 文件.在检查 .pcd 格式应该是什么样子并编写一些代码将其转换为 .pcd 格式后,我的结果是模型只有黑色,而不是像 .ply 格式模型那样多色.
I've a .ply format model and trying to create a .pcd file one. After checking how .pcd format should look like and writing some code to convert it to .pcd format, my results are that the model is black colored only, instead of multi-colored like the .ply format model.
在 .ply 格式中,每条点线 (x,y,z,r,g,b,a) 中有 7 个参数,而在 .pcd 中,它应该是 (x y z rgb).我不确定如何从 .ply 文件评估 rgb.
In the .ply format there are 7 parameters in every point line (x,y,z,r,g,b,a) and on .pcd one it should be (x y z rgb). I'm not sure how to evaluate the rgb from the .ply file.
这是我的一些 .ply 文件数据:
ply
format ascii 1.0
comment VCGLIB generated
element vertex 130474
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
property uchar alpha
element face 0
property list uchar int vertex_indices
end_header
169.345 0.00190678 -356.222 128 138 80 255
170.668 0.00202459 -355.459 58 36 16 255
170.6 0.00285877 -355.877 59 46 45 255
170.307 0.00326565 -354.98 149 107 81 255
170.581 0.00329066 -355.646 61 38 28 255
以及一些使用代码后的.pcd文件数据:
# .PCD v.7 - Point Cloud Data file format
VERSION .7
FIELDS x y z rgb
SIZE 4 4 4 1
TYPE F F F F
COUNT 1 1 1 1
WIDTH 130474
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 130474
DATA ascii
169.345 0.00190678 -356.222 128
170.668 0.00202459 -355.459 58
170.6 0.00285877 -355.877 59
这是 .pcd 的外观(可在点云网站中找到)
# .PCD v.7 - Point Cloud Data file format
VERSION .7
FIELDS x y z rgb
SIZE 4 4 4 4
TYPE F F F F
COUNT 1 1 1 1
WIDTH 213
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 213
DATA ascii
0.93773 0.33763 0 4.2108e+06
0.90805 0.35641 0 4.2108e+06
0.81915 0.32 0 4.2108e+06
问题:这些值是什么:4.2108e+06 作为第四个参数,我如何通过 .ply 格式生成它?
Question: What are these values: 4.2108e+06 as the 4th parameter and how can I generate it through the .ply format?
这是我目前在 PyCharm 上使用的代码:
#!/usr/bin/env python
import sys
import os
header = "# .PCD v.7 - Point Cloud Data file format\n\
VERSION .7\n\
FIELDS x y z rgb\n\
SIZE 4 4 4 1\n\
TYPE F F F F\n\
COUNT 1 1 1 1\n\
WIDTH XXXX\n\
HEIGHT 1\n\
VIEWPOINT 0 0 0 1 0 0 0\n\
POINTS XXXX\n\
DATA ascii"
def convertionOfPlyToPcd(ply_file, pcd_file):
input_file = open(ply_file)
out = pcd_file
output = open(out, 'w')
write_points = False
points_counter = 0
nr_points = 0
for s in input_file.readlines():
if s.find("element vertex") != -1:
nr_points = int(s.split(" ")[2].rstrip().lstrip())
new_header = header.replace("XXXX", str(nr_points))
output.write(new_header)
output.write("\n")
if s.find("end_header") != -1:
write_points = True
continue
if write_points and points_counter < nr_points:
points_counter = points_counter + 1
output.write(" ".join(s.split(" ", 4)[:4]))
output.write("\n")
input_file.close()
output.close()
if __name__ == "__main__":
# We request the path to the script, if it's not found - exit
if sys.argv[0] == "":
sys.exit(1)
# PLY file - We convert this format to PCD format
ply_file = sys.argv[1]
# PCD file - generated from PLY file
pcd_file = sys.argv[2]
# Function which converts .ply format files to .pcd files
convertionOfPlyToPcd(ply_file, pcd_file)
对代码进行这些更改后,结果是白色浊点而不是黑色:
header = "# .PCD v.7 - Point Cloud Data file format\n\
VERSION .7\n\
FIELDS x y z\n\
SIZE 4 4 4\n\
TYPE F F F \n\
COUNT 1 1 1\n\
WIDTH XXXX\n\
HEIGHT 1\n\
VIEWPOINT 0 0 0 1 0 0 0\n\
POINTS XXXX\n\
DATA ascii"
用于比较的软件:CloudCompare
预期结果:
当前结果:
推荐答案
我遇到了类似的问题,并通过使用 Open3D 库.
I was struggling with a similar problem and found a quite convenient method for me by using the Open3D library.
只需使用 pip pip install open3d
或 conda conda install -c open3d-admin open3d
安装库并运行以下三行:
Simply install the library using pip pip install open3d
or conda conda install -c open3d-admin open3d
and run these three lines:
import open3d as o3d
pcd = o3d.io.read_point_cloud("source_pointcloud.ply")
o3d.io.write_point_cloud("sink_pointcloud.pcd", pcd)
它运行良好并保持颜色(使用 CloudCompare 检查).
It worked fine and kept the colors (checked by using CloudCompare).
这篇关于.ply 格式到 .pcd 格式的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!