将 .ply 格式转换为 .pcd 格式

4
我是一个有用的助手,可以为您翻译文本。
我有一个.ply格式的模型,并试图创建一个.pcd文件。在检查了.pcd格式应该看起来像什么并编写了一些代码将其转换为.pcd格式之后,我的结果是该模型仅为黑色,而不是多彩的像.ply格式的模型那样。
在.ply格式中,每个点线都有7个参数(x、y、z、r、g、b、a),在.pcd中应该是(x y z rgb)。我不确定如何从.ply文件中评估rgb。
以下是我的某些.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格式生成它?

这是我目前在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

期望结果: 在此输入图片描述

当前结果: 在此输入图片描述

3个回答

4

我遇到了类似的问题,后来通过使用Open3D库找到了一种非常方便的方法。

只需要使用pip pip install open3d 或conda conda install -c open3d-admin open3d 安装该库,并运行以下三行代码:

import open3d as o3d
pcd = o3d.io.read_point_cloud("source_pointcloud.ply")
o3d.io.write_point_cloud("sink_pointcloud.pcd", pcd)

它运行良好,并且保持了颜色(通过使用 CloudCompare 进行检查)。


0

你可以从这里获取r、g、b和rgb之间的关系。

PointXYZRGB p;
// unpack rgb into r/g/b
uint32_t rgb = *reinterpret_cast<int*>(&p.rgb);
uint8_t r = (rgb >> 16) & 0x0000ff;
uint8_t g = (rgb >> 8)  & 0x0000ff;
uint8_t b = (rgb)       & 0x0000ff;

好的,你可以更改

output.write(" ".join(s.split(" ", 4)[:4]))

x,y,z,r,g,b = s.split(" ", 6)[:6]
rgb = str((int(r)<<16)  + (int(g)<<8) + (int(b)))
output.write(" ".join((x,y,z,rgb)))

1
请说明这与所讨论的.ply和.pcd格式有何关联。 - JJJ

-1

我知道我来晚了,但是为什么不给python-pcl库一个机会呢?具体来说,你可以这样使用pcl.save()方法:

cloud_in = pcl.load("path-to-ply-cloud.ply")
pcl.save(cloud_in, "path-to-ply-cloud.pcd")

请注意,pcl.save方法根据您提供的文件扩展名保存云,可以在文件路径或方法参数中提供。也许你打算编写自己的函数,但我仍然希望保留这个答案,希望对您或其他人有用。

1
目前来看,这是一个非常有bug且未完成的Python库。 - Schütze

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接