【问题标题】:Add new points in .PLY file在 .PLY 文件中添加新点
【发布时间】:2022-09-24 02:12:48
【问题描述】:

我正在尝试使用 PlyData 在我的 .ply 文件中添加新行:

from plyfile import PlyData, PlyElement
import numpy

with open(filepath, \'rb\') as f:
    plydata = PlyData.read(f)
    vertex = numpy.array([([0, 1, 2], 255, 255, 255), ([0, 2, 3], 255, 0, 0)],
                         dtype=[(\'vertex_indices\', \'i4\', (3,)), (\'red\', \'u1\'), (\'green\', \'u1\'), (\'blue\', \'u1\')])
new_vertex = PlyElement.describe(vertex, \'vertex\')
with open(\'colored_points.ply\', mode=\'wb\') as f:
    PlyData([plydata, new_vertex], text=True).write(f)

这种方法给了我这个错误:

AttributeError: \'PlyData\' object has no attribute \'name\'

谢谢!

  • 你好!我们或许可以提供帮助,但我们需要更多信息。为什么你需要另一种方法来做到这一点?您当前使用的方法不令人满意吗?请解释为什么它不令人满意。是否有错误消息,数据是否错误等?请提供详细信息。另请参阅What Do You Mean \"It Doesn\'t Work\"? 以获取有关如何使您的问题更明确的灵感。
  • 另外,出于礼貌,您能否编辑您的问题,在代码开头添加相关的import 行?我可以大胆猜测它是from plyfile import PlyData, PlyElement,但这只是一个猜测。如果有人想帮助你,他们会做的第一件事就是尝试运行你的代码,如果没有导入它就会崩溃。
  • 无论如何,如果你真的在寻找其他选择,这里有这个:github.com/daavoo/pyntcloud/blob/master/pyntcloud/io/ply.py
  • 好的,我修改了谢谢

标签: python point-cloud-library 3d-model


【解决方案1】:

解释异常:

PlyData 代表整个层并分为多个元素,您可以通过以下方式访问它们:plydata.elements

具有“顶点”元素的层文件示例(包含全部点)和一个“脸”元素(包含全部脸):

plydata.elements
(PlyElement('vertex', (PlyProperty('x', 'float'), PlyProperty('y', 'float'), PlyProperty('z', 'float'), PlyProperty('red', 'uchar'), PlyProperty('green', 'uchar'), PlyProperty('blue', 'uchar')), count=46596, comments=[]),
 PlyElement('face', (PlyListProperty('vertex_indices', 'uchar', 'int'),), count=91800, comments=[]))

当您编写PlyData([.., ..], ..) 时,它需要PlyElement 的列表,而ply​​data 是PlyData

如何将点添加到层:

您可以使用其名称访问元素,如字典:plydata['vertex'],以及实际数据(麻木的) 与plydata['vertex'].data

新点应直接添加到此数据

new_vertex = numpy.array(
    (1., 2., 3., 10, 20, 30),
    dtype=plydata['vertex'].data.dtype
)
plydata['vertex'].data = np.r_[plydata['vertex'].data, new_vertex]

...

plydata.write(f)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-19
    • 2013-06-14
    • 1970-01-01
    • 2013-02-18
    • 2016-08-30
    • 1970-01-01
    • 2021-06-27
    相关资源
    最近更新 更多