ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Geometry模块之File IO

2022-08-20 12:01:29  阅读:171  来源: 互联网

标签:False File Geometry filename write IO file progress True


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

函数参数解释:

  • filename:要读取的点云文件名(文件路径),也是最常用的一个参数
  • format:读取的点云文件的格式,默认为 auto(根据后缀名判断格式),若指定格式与读取的格式不一致,会报错 RPly: Wrong magic number. Expected 'xxx'
  • remove_nan_points:默认为 False,若设置为 True,会将所有包含 NaN值的点云数据删除
  • remove_infinite_points:默认为 False,若设置为 True,会将所有包含无限值的点云数据删除
  • print_progress:默认为 False,若设置为 True,控制台会输出一个进度条

函数返回值:读取成功后,会返回一个 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

函数参数解释:

  • filename:要保存的点云文件名(文件路径),也是最常用的一个参数
  • pointcloud:进行 I/O 操作的 PointCloud 对象(要进行保存操作的点云数据对象)
  • write_ascii:默认为 False,若设置为 True,会将文件以 ASCII 码格式输出,否则会以默认的二进制格式输出
  • compressed:默认为 False,若设置为 True,会将文件以压缩格式输出
  • print_progress:默认为 False,若设置为 True,控制台会输出一个进度条

函数返回值:保存成功后会返回一个布尔值,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

函数参数解释:

  • filename:要保存的网格文件名(文件路径),也是最常用的一个参数
  • enable_post_processing
  • print_progress:默认为 False,若设置为 True,控制台会输出一个进度条

函数返回值:读取成功后,会返回一个 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

函数参数解释:

  • filename:要保存的网格文件名(文件路径),也是最常用的一个参数
  • pointcloud:进行 I/O 操作的 TriangleMesh 对象(要进行保存操作的网格数据对象)
  • write_ascii:默认为 False,若设置为 True,会将文件以 ASCII 码格式输出,否则会以默认的二进制格式输出
  • compressed:默认为 False,若设置为 True,会将文件以压缩格式输出
  • write_vertex_normals:默认为 True,会保存网格的顶点法线信息
  • write_vertex_colors:默认为 True,会保存网格的顶点颜色信息
  • write_triangle_uvs:默认为 True,会保存由构成三角形的点的索引表示的 UV 列表
  • print_progress:默认为 False,若设置为 True,控制台会输出一个进度条

函数返回值:保存成功后会返回一个布尔值,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

函数参数:

  • filename:要保存的图片文件名(文件路径)
  • image:进行保存操作的 Image对象
  • quality:默认为 -1,代表输出图片的质量

返回值:保存成功后会返回一个布尔值,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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有