ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python 使用open3d 显示pcd点云

2021-12-28 14:36:57  阅读:324  来源: 互联网

标签:files name python self height vis open3d 点云 pcd


需求

  • 读取包含pcd文件的文件夹路径
  • 依次显示pcd点云

代码

#! /usr/bin/env python
# -*- coding: utf-8 -*-#
# -------------------------------------------------------------------------------
# Name:         Open3dShowPcd
# Author:       yunhgu
# Date:         2021/12/22 9:07
# Description: 
# -------------------------------------------------------------------------------
from pathlib import Path

import numpy as np
import open3d as o3d
from traceback import format_exc


class PtVis:
    def __init__(self, name='空格进入下一帧 Z退回上一帧', width=1024, height=768, pcd_files_path=""):
        self.pcd_files_path = pcd_files_path
        self.pcd_files_list = []
        self.index = 0
        self.pcd = None
        self.vis = None
        self.init_pcd_files_list()
        self.init_open3d(name, width, height)

    def init_open3d(self, name, width, height):
        self.vis = o3d.visualization.VisualizerWithKeyCallback()
        self.vis.create_window(window_name=name, width=width, height=height)
        opt = self.vis.get_render_option()
        opt.background_color = np.asarray([0, 0, 0])
        opt.point_size = 2
        self.vis.register_key_callback(90, lambda temp: self.last())
        self.vis.register_key_callback(32, lambda temp: self.next())

    def show_pcd(self):
        # 初始化显示pcd第一帧
        self.pcd = o3d.io.read_point_cloud(self.pcd_files_list[self.index])
        self.vis.add_geometry(self.pcd)
        self.vis.run()

    def next(self):
        if 0 <= self.index < len(self.pcd_files_list) - 1:
            self.index += 1
        self.update(self.index)

    def last(self):
        if 0 < self.index <= len(self.pcd_files_list) - 1:
            self.index -= 1
        self.update(self.index)

    def init_pcd_files_list(self):
        for file in Path(self.pcd_files_path).rglob("*.pcd"):
            self.pcd_files_list.append(str(file))
        self.pcd_files_list = sorted(self.pcd_files_list, key=lambda p: int(Path(p).stem))

    def update(self, index):
        pcd = o3d.io.read_point_cloud(self.pcd_files_list[index])
        self.vis.clear_geometries()  # 清空vis点云
        self.vis.add_geometry(pcd)  # 增加vis中的点云
        self.vis.poll_events()
        self.vis.update_renderer()

    def close(self):
        self.vis.destroy_window()


if __name__ == '__main__':
    pt = None
    try:
        pcd_file_path = input("请输入要展示的pcd文件夹:").strip("\"")
        pt = PtVis(pcd_files_path=pcd_file_path)
        print(f"{pcd_file_path}共有PCD个数:{len(pt.pcd_files_list)}")
        pt.show_pcd()
    except Exception as e:
        print(f"运行出错请联系开发人员:{format_exc}{e}")
    finally:
        pt.close()

展示示例

image

标签:files,name,python,self,height,vis,open3d,点云,pcd
来源: https://www.cnblogs.com/yunhgu/p/15740431.html

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

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

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

ICode9版权所有