【问题标题】:how to write data from a point cloud file to a text file如何将数据从点云文件写入文本文件
【发布时间】:2019-12-31 23:04:08
【问题描述】:

我正在尝试制作一个获取点云数据并将数据发布到 txt 文件中的程序,由于某种原因,当我运行我的代码时出现此错误:

File "readpts.py", line 14, in <module>
    f.write("%d    "%float(array[i][0].item()))
io.UnsupportedOperation: not writable

这应该是一个简单的修复我只是不知道我做错了什么。这是我的代码:

import numpy as np
import open3d as o3d



pcd= o3d.io.read_point_cloud("cloud_cd.ply")
#print(pcd)
#print(np.asarray(pcd.points))
array=np.asarray(pcd.points)
f=open("cloud_cd.ply")
#print(type(float(array[0][0].item())))

for i in range(len(array)):
    f.write("%d    "%float(array[i][0].item()))
    f.write("%d    "%float(array[i][1].item()))
    f.write("%d    \n"%float(array[i][2].item()))

【问题讨论】:

    标签: python-3.x io point-clouds


    【解决方案1】:

    您正在以读取模式打开文件,这是使用 open 函数时的默认模式。

    你应该这样做:

    import numpy as np
    import open3d as o3d
    
    pcd= o3d.io.read_point_cloud("cloud_cd.ply")
    array=np.asarray(pcd.points)
    
    with open("points.txt", mode='w') as f:  # I add the mode='w'
        for i in range(len(array)):
            f.write("%f    "%float(array[i][0].item()))
            f.write("%f    "%float(array[i][1].item()))
            f.write("%f    \n"%float(array[i][2].item()))
    

    with 允许即使发生错误也关闭文件。

    编辑

    对于舍入问题,这是由于 %d。为了有一个浮动,用%f替换%d(在上面的代码中完成)。如果您只想保留两位小数:%.2f(更多信息请参见doc)。

    如果你是python3.6+,可以使用formatted string

    【讨论】:

    • 我想你的意思是这样写:with open("points.txt", mode='w') as f:
    • 它现在也可以工作,但它正在以任何我可以解决该问题的方式四舍五入所有数字?
    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 2017-07-26
    • 2015-05-20
    • 2014-06-02
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多