其他分享
首页 > 其他分享> > Geometry模块之File IO

Geometry模块之File IO

作者:互联网

1 读取 PointCloud

  1. 首先要明白,该库所支持的点云文件类型都有哪些,官网写的很清楚:
文件类型 类型描述
xyz 每一行由 [x, y, z] 三维坐标构成
xyzn 每一行由 [x, y, z, nx, ny, xz] 构成
除过三维坐标,还包含每个点的三维法向量
xyzrgb 每一行由 [x, y, z, r, g, b]构成
除过三维坐标,还包含该点的 RGB 颜色数据
RBG 的数值均为单精度浮点型,范围为 [0, 1]
pts 文件第一行记录的是该文件的点总数
剩下的每一行数据格式遵循以下所列出的其中一种:
[x, y, z, i, r, g, b][x, y, z, r, g, b][x, y, z, i][x, y, z]
其中:x、y、z、i的类型均为 doubler、g、b类型均为 uint8
ply 多边形三角网文件格式
pcd 是 PCL 官方指定格式,具有 ASCII 和 Binary 两种数据存储类型
pcd 格式具有文件头,用于描绘点云的整体信息
  1. 读取点云文件只需要调用一个名为 read_point_cloud()的函数便可实现,该函数源码解释如下:
def read_point_cloud(filename, format='auto', remove_nan_points=False, remove_infinite_points=False, print_progress=False): 
    """
    read_point_cloud(filename, format='auto', remove_nan_points=False, remove_infinite_points=False, print_progress=False)
    Function to read PointCloud from file
    
    Args:
        filename (str): Path to file.
        format (str, optional, default='auto'): The format of the input file. When not specified or set as ``auto``, the format is inferred from file extension name.
        remove_nan_points (bool, optional, default=False): If true, all points that include a NaN are removed from the PointCloud.
        remove_infinite_points (bool, optional, default=False): If true, all points that include an infinite value are removed from the PointCloud.
        print_progress (bool, optional, default=False): If set to true a progress bar is visualized in the console
    
    Returns:
        open3d.geometry.PointCloud
    """
    pass

函数参数解释:

函数返回值:读取成功后,会返回一个 PointCloud点云对象

  1. 读取 pcd 点云文件,代码示例
file = "../../data/pcd/fragment.pcd"

# 不指定文件格式
pcd_file_01 = o3d.io.read_point_cloud(filename=file)

# 指定文件格式
pcd_file_02 = o3d.io.read_point_cloud(filename=file, format='pcd')

2 保存 PointCloud

  1. 保存点云文件调用函数 write_point_cloud()实现,该函数的源码解释如下:
def write_point_cloud(filename, pointcloud, write_ascii=False, compressed=False, print_progress=False): # real signature unknown; restored from __doc__
    """
    write_point_cloud(filename, pointcloud, write_ascii=False, compressed=False, print_progress=False)
    Function to write PointCloud to file
    
    Args:
        filename (str): Path to file.
        pointcloud (open3d.geometry.PointCloud): The ``PointCloud`` object for I/O
        write_ascii (bool, optional, default=False): Set to ``True`` to output in ascii format, otherwise binary format will be used.
        compressed (bool, optional, default=False): Set to ``True`` to write in compressed format.
        print_progress (bool, optional, default=False): If set to true a progress bar is visualized in the console
    
    Returns:
        bool
    """
    pass

函数参数解释:

函数返回值:保存成功后会返回一个布尔值,1表示成功;0表示失败

  1. 保存 pcd 点云文件,代码示例
file = "../../data/pcd/fragment.pcd"
pcd_file = o3d.io.read_point_cloud(filename=file)

# 将 pcd 转为 ply 并保存
save_path = "../../data/ply/fragment_test.ply"
o3d.io.write_point_cloud(save_path, pcd_file)

3 读取 Mesh

  1. 首先要明白,该库所支持的网格文件类型都有哪些,官网写的很清楚:
文件类型 类型描述
ply 多边形三角网文件格式,常用格式
stl STL文件仅描述三维物体的表面几何形状,没有颜色、材质贴图或其它常见三维模型的属性
obj 标准的 3D 模型文件格式
off 是一种3D 文本格式,通过定义点、线、面的方式来描述 3D 物体
gtlf / glb 是以图形语言传输格式(glTF)保存的 3D 模型,它以二进制格式存储有关 3D 模型的信息
包括节点层级、摄像机、材质、动画和网格。GLB 文件是 GLTF 文件的二进制形式
  1. 要想读取 mesh 网格文件,需要调用 read_triangle_mesh()函数,该函数源码如下:
def read_triangle_mesh(filename, enable_post_processing=False, print_progress=False): # real signature unknown; restored from __doc__
    """
    read_triangle_mesh(filename, enable_post_processing=False, print_progress=False)
    Function to read TriangleMesh from file
    
    Args:
        filename (str): Path to file.
        enable_post_processing (bool, optional, default=False)
        print_progress (bool, optional, default=False): If set to true a progress bar is visualized in the console
    
    Returns:
        open3d.geometry.TriangleMesh
    """
    pass

函数参数解释:

函数返回值:读取成功后,会返回一个 TriangleMesh网格对象

  1. 读取 mesh 网格文件,代码示例
file = "../data/ply/ArmadilloMesh.ply"
ply_file = o3d.io.read_triangle_mesh(filename=file)

4 保存 Mesh

  1. 保存网格文件调用函数 write_triangle_mesh()实现,该函数的源码解释如下:
def write_triangle_mesh(filename, mesh, write_ascii=False, compressed=False, write_vertex_normals=True, write_vertex_colors=True, write_triangle_uvs=True, print_progress=False): 
    """
    write_triangle_mesh(filename, mesh, write_ascii=False, compressed=False, write_vertex_normals=True, write_vertex_colors=True, write_triangle_uvs=True, print_progress=False)
    Function to write TriangleMesh to file
    
    Args:
        filename (str): Path to file.
        mesh (open3d.geometry.TriangleMesh): The ``TriangleMesh`` object for I/O
        write_ascii (bool, optional, default=False): Set to ``True`` to output in ascii format, otherwise binary format will be used.
        compressed (bool, optional, default=False): Set to ``True`` to write in compressed format.
        write_vertex_normals (bool, optional, default=True): Set to ``False`` to not write any vertex normals, even if present on the mesh
        write_vertex_colors (bool, optional, default=True): Set to ``False`` to not write any vertex colors, even if present on the mesh
        write_triangle_uvs (bool, optional, default=True): Set to ``False`` to not write any triangle uvs, even if present on the mesh. For ``obj`` format, mtl file is saved only when ``True`` is set
        print_progress (bool, optional, default=False): If set to true a progress bar is visualized in the console
    
    Returns:
        bool
    """
    pass

函数参数解释:

函数返回值:保存成功后会返回一个布尔值,1表示成功;0表示失败

  1. 保存 mesh 网格文件,代码示例
file = "../data/ArmadilloMesh.ply"
ply_file = o3d.io.read_triangle_mesh(filename=file)

# 将 ply 转为 obj 并保存
save_path = "../data/ArmadilloMesh_test.obj"
o3d.io.write_triangle_mesh(save_path, ply_file)

5 读取 Image

  1. Open3D 库可以读取的图片格式只有 jpg、png 两种
  2. 要读取图片只需要调用 read_image()函数,函数源码如下:
def read_image(filename):
    """
    read_image(filename)
    Function to read Image from file
    
    Args:
        filename (str): Path to file.
    
    Returns:
        open3d.geometry.Image
    """
    pass

函数参数只有一个,filename:要读取的图片文件名(文件路径)
返回值:读取成功后,会返回一个 Image对象

  1. 读取 image 图片文件,代码示例
file = "../data/bg.png"
img = o3d.io.read_image(filename=file)

6 保存 Image

  1. 保存图片只需要调用 write_image()函数,函数源码如下:
def write_image(filename, image, quality=-1): 
    """
    write_image(filename, image, quality=-1)
    Function to write Image to file
    
    Args:
        filename (str): Path to file.
        image (open3d.geometry.Image): The ``Image`` object for I/O
        quality (int, optional, default=-1): Quality of the output file.
    
    Returns:
        bool
    """
    pass

函数参数:

返回值:保存成功后会返回一个布尔值,1表示成功;0表示失败

  1. 保存 image 图片文件,代码示例
file = "../data/bg.png"
img = o3d.io.read_image(filename=file)

save_path = "../data/bg_test.png"
o3d.io.write_image(save_path, img)

标签:False,File,Geometry,filename,write,IO,file,progress,True
来源: https://www.cnblogs.com/wudaojiuxiao/p/16607455.html