点云 3D 可视化 - Open3D 库

1. 文章信息

(1)标题:Open3D: A Modern Library for 3D Data Processing (2018)

(2)文章链接:https://arxiv.org/pdf/1801.09847.pdf

(3)Open3D 库网址:http://www.open3d.org/

2. Open3D 库简介

  • Open3D 是一个开源库,支持快速开发处理 3D 数据的软件。Open3D 前端在 C++ 和 Python 中公开了一组精心选择的数据结构和算法。后端经过高度优化,并设置为并行化。Open3D 是从一块干净的石板上开发出来的,有一个小的、经过仔细考虑的依赖项集。它可以在不同的平台上设置,并从源代码进行编译,只需很少的工作量。代码干净、样式一致,并通过清晰的代码审查机制进行维护。Open3D已经在许多已发表的研究项目中使用,并积极部署在云端。我们欢迎开源社区的贡献。核心特性如下:
  • Simple installation via conda and pip
  • 3D data structures
  • 3D data processing algorithms
  • Scene reconstruction
  • Surface alignment
  • PBR rendering
  • 3D visualization
  • Python binding
  • 安装方法
conda install open3d  或者  pip install open3d

2. 3D 可视化使用

2.1 单帧点云

import numpy as np
import open3d as o3d


def hsv2rgb(h_, s_, v_):
    r_ = 0
    g_ = 0
    b_ = 0

    c_ = v_ * s_
    x_ = c_ * (1 - (abs((h_ / 60) % 2 - 1)))
    m = v_ - c_

    if (h_ >= 0) and (h_ < 60):
        r_ = c_ + m
        g_ = x_ + m
        b_ = 0 + m
    elif (h_ >= 60) and (h_ < 120):
        r_ = x_ + m
        g_ = c_ + m
        b_ = 0 + m
    elif (h_ >= 120) and (h_ < 180):
        r_ = 0 + m
        g_ = c_ + m
        b_ = x_ + m
    elif (h_ >= 180) and (h_ < 240):
        r_ = 0 + m
        g_ = x_ + m
        b_ = c_ + m
    elif (h_ >= 240) and (h_ < 300):
        r_ = x_ + m
        g_ = 0 + m
        b_ = c_ + m
    elif (h_ >= 300) and (h_ < 360):
        r_ = c_ + m
        g_ = 0 + m
        b_ = x_ + m

    return np.array([r_, g_, b_])  # 0~1


data_path = '/home/hjw/point_cloud_test/point_cloud_test_data.npy'  # 本人自己的测试数据,直接替换即可
point_cloud = np.load(data_path).astype(np.float32, copy=False)

point_xyz = point_cloud[:, :3]  # x, y, z
point_intensity = point_cloud[:, 3]  # intensity

# point_color = []  # user defined colormap
# colormap = np.array([[150, 150, 150],
#                     [255, 0, 255],
#                     [0, 255, 0],
#                     [255, 255, 0]]) / 255.0
# for point_id in range(point_cloud.shape[0]):
#     color_value = 0
#     point_color.append(colormap[color_value])

# point_color = []  # ros colormap
# for point_id in range(point_cloud.shape[0]):
#     color_value = max(min(point_intensity[point_id], 255), 0) / 255.0 * 240.0
#     point_color.append(hsv2rgb(color_value, 1, 1))

point_color = []  # pc colormap
for point_id in range(point_cloud.shape[0]):
    color_value = max(min(255 - point_intensity[point_id], 255), 0) / 255.0 * 240.0
    point_color.append(hsv2rgb(color_value, 1, 1))

pcd = o3d.geometry.PointCloud()  # 传入3d点云

pcd.points = o3d.utility.Vector3dVector(point_xyz)  # point_xyz 二维 numpy 矩阵,将其转换为 open3d 点云格式

pcd.colors = o3d.utility.Vector3dVector(point_color)  # 根据 intensity 和 colormap 着色

vis = o3d.visualization.Visualizer()
vis.create_window(width=800, height=600)  # 创建窗口
render_option = vis.get_render_option()  # 渲染配置
render_option.background_color = np.array([0, 0, 0])  # 设置点云渲染参数,背景颜色
render_option.point_size = 1.0  # 设置渲染点的大小
vis.add_geometry(pcd)  # 添加点云

view_control = vis.get_view_control()  # 视角配置
# view_control.change_field_of_view(step=30)
# view_control.rotate(90.0, 10.0)
# view_control.set_front([0, 1, 0.5])
# view_control.set_lookat([1, 0, 0])
# view_control.set_up([0, -1, 0])
# view_control.translate(-50, -100)
view_control.set_zoom(0.2)

vis.run()

vis.destroy_window()

详细参数介绍见 API 网址:http://www.open3d.org/docs/release/python_api/open3d.visualization.ViewControl.html

请添加图片描述

2.2 多帧点云

import os
import numpy as np
import open3d as o3d

folder_path = '/home/hjw/point_cloud_test/'  # 本人自己的测试数据路径,直接替换即可
files = os.listdir(folder_path)

vis = o3d.visualization.Visualizer()
vis.create_window(width=800, height=600)  # 创建窗口
pcd = o3d.geometry.PointCloud()
to_reset = True
vis.add_geometry(pcd)

for file_index in range(len(files)):

    data_path = os.path.join(folder_path, files[file_index])
    point_cloud = np.load(data_path).astype(np.float32, copy=False)
    
    point_xyz = point_cloud[:, :3]  # x, y, z
    point_intensity = point_cloud[:, 3]  # intensity

    pcd.points = o3d.utility.Vector3dVector(point_xyz)
    
    vis.update_geometry(pcd)
    
    if to_reset:
        vis.reset_view_point(True)
        to_reset = False
    
    vis.poll_events()
    vis.update_renderer()

猜你喜欢

转载自blog.csdn.net/i6101206007/article/details/123403598