open3d连续读取pcd文件及实现点云视角转换

目录

  • 1.保存视角参数
  • 2.加载pcd文件及视角转换

1.保存视角参数

save_view_point和load_view_point函数用于将所需视角下的参数写入到json文件。

import open3d as o3d

def save_view_point(pcd, filename):
    vis = o3d.visualization.Visualizer()
    vis.create_window(window_name='pcd', width=800, height=600)
    vis.add_geometry(pcd)
    vis.run()  # user changes the view and press "q" to terminate
    param = vis.get_view_control().convert_to_pinhole_camera_parameters()
    o3d.io.write_pinhole_camera_parameters(filename, param)
    vis.destroy_window()
    
def load_view_point(pcd, filename):
    vis = o3d.visualization.Visualizer()
    vis.create_window(window_name='pcd', width=800, height=600)
    ctr = vis.get_view_control()
    param = o3d.io.read_pinhole_camera_parameters(filename)
    vis.add_geometry(pcd)
    ctr.convert_from_pinhole_camera_parameters(param)
    vis.run()
    vis.destroy_window()


if __name__ == "__main__":
    pcd = o3d.io.read_point_cloud("road.pcd")   #传入自己当前的pcd文件
    save_view_point(pcd, "viewpoint.json")  #保存好得json文件位置
    load_view_point(pcd, "viewpoint.json")  #加载修改时较后的pcd文件

运行此段代码,并进行调整视角。

2.加载pcd文件及视角转换

该脚本读取上面设置好的视角参数,在固定背景点云中遍历显示pcd文件(没有背景pcd得可以将背景部分注释掉)

import open3d as o3d
import os
import numpy as np
import time

def vis_pcd(pcd_path,road_pcd,json_path):
    # 初始化窗口
    vis = o3d.visualization.Visualizer()
    vis.create_window(window_name='pcd', width=800, height=600)

    # 可视化参数设置
    opt = vis.get_render_option()
    # 设置背景色
    opt.background_color = np.asarray([0, 0, 0])
    # 设置点云大小
    opt.point_size = 2
    opt.show_coordinate_frame = True

    # 读取viewpoint参数
    param = o3d.io.read_pinhole_camera_parameters(json_path)
    ctr = vis.get_view_control()
    # 转换视角
    ctr.convert_from_pinhole_camera_parameters(param)

    # 创造pcd类型数据
    pointcloud = o3d.geometry.PointCloud()
    road_pc = o3d.geometry.PointCloud()

    # 加载背景点云
    road = o3d.io.read_point_cloud(road_pcd)
    # 转为numpy格式
    road = np.asarray(road.points).reshape((-1, 3))
    road_pc.points = o3d.utility.Vector3dVector(road)
    # 调整点云颜色
    road_pc.paint_uniform_color([0,1,0])
    vis.add_geometry(road_pc)

    to_reset = True

    # 读取点云文件加中文件名
    files = os.listdir(pcd_path)
    # t0 = time.time()
    for f in files:
        # t1 = time.time()
        # 加载点云文件
        pcd = o3d.io.read_point_cloud(pcd_path + f)
        # 设置休眠时间
        time.sleep(0.2)
        # 转换数据格式
        pcd = np.asarray(pcd.points).reshape((-1, 3))
        pointcloud.points = o3d.utility.Vector3dVector(pcd)
        # 调整点云颜色
        pointcloud.paint_uniform_color([1, 0, 0])

        vis.update_geometry(pointcloud)
        vis.add_geometry(pointcloud)
        ctr.convert_from_pinhole_camera_parameters(param)
        # t2 = time.time()
        # print("time:",(t2-t1))
        if to_reset:
            vis.reset_view_point(True)
            to_reset = False
        vis.poll_events()
        vis.update_renderer()
    # t3 = time.time()
    # print("all time:",t3-t0)
if __name__ == '__main__':
    vis_pcd("./data/pcd/",“road.pcd”,“viewpoint.json”)
    #参数1:存放pcd点云得文件夹,参数2:背景pcd,参数3:视角json文件

open3d连续读取pcd文件及实现点云视角转换_第1张图片

viewpoint.json内部信息如下:

是一些坐标变换的参数

{
	"class_name" : "PinholeCameraParameters",
	"extrinsic" : 
	[
		-0.14235490862151962,
		-0.8290631685657337,
		-0.54073037876478225,
		0.0,
		-0.9067732196814059,
		0.32826468205903769,
		-0.26458387437879843,
		0.0,
		0.39685943110876892,
		0.45265501327216623,
		-0.7985023674978049,
		0.0,
		-35.991682946262912,
		29.330172681787637,
		35.281692583343712,
		1.0
	],
	"intrinsic" : 
	{
		"height" : 600,
		"intrinsic_matrix" : 
		[
			519.6152422706632,
			0.0,
			0.0,
			0.0,
			519.6152422706632,
			0.0,
			399.5,
			299.5,
			1.0
		],
		"width" : 800
	},
	"version_major" : 1,
	"version_minor" : 0
}

你可能感兴趣的:(实用技巧,open3d,python)