本文主要是介绍python 使用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()
展示示例
这篇关于python 使用open3d 显示pcd点云的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!