open3d 深度图和点云数据互转,RGBD和点云互转

1. 深度图Image和点云

关键代码:

(1) 深度图转点云

pcd = o3d.t.geometry.PointCloud.create_from_depth_image(depth=depth,
                                                        intrinsics=intrinsic,
                                                        depth_scale=5000.0,
                                                        depth_max=10.0)

需要知道相机内外参数。 

(2) 点云转深度图

depth_reproj = pcd.project_to_depth_image(width=640,
                                          height=480,
                                          intrinsics=intrinsic,
                                          depth_scale=5000.0,
                                          depth_max=10.0)

 需要知道相机内外参数。 

point_cloud_to_depth.py

import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    # 1. read
    tum_data = o3d.data.SampleTUMRGBDImage()
    depth = o3d.t.io.read_image(tum_data.depth_path)  # Image

    # 2. depth Image生成PointCloud
    """
    create PointCloud from a depth image and a camera model.
    
    depth (open3d.t.geometry.Image): The input depth image should be a uint16_t image.
    intrinsics (open3d.core.Tensor): Intrinsic parameters of the camera. 相机内参
    extrinsics (open3d.core.Tensor, optional): Extrinsic parameters of the camera. 相机外参
    depth_scale (float, optional, default=1000.0): The depth is scaled by 1 / depth_scale.
    depth_max (float, optional, default=3.0): Truncated at depth_max distance.
    ...params.
    
    """
    intrinsic = o3d.core.Tensor([[535.4, 0, 320.1], [0, 539.2, 247.6],
                                 [0, 0, 1]])
    pcd = o3d.t.geometry.PointCloud.create_from_depth_image(depth=depth,
                                                            intrinsics=intrinsic,
                                                            depth_scale=5000.0,
                                                            depth_max=10.0)
    o3d.visualization.draw([pcd])

    # 3. PointCloud生成depth Image
    depth_reproj = pcd.project_to_depth_image(width=640,
                                              height=480,
                                              intrinsics=intrinsic,
                                              depth_scale=5000.0,
                                              depth_max=10.0)

    fig, axs = plt.subplots(1, 2)
    axs[0].imshow(np.asarray(depth.to_legacy()))  # 原始depth
    axs[1].imshow(np.asarray(depth_reproj.to_legacy()))  # depth->ointCloud->depth
    plt.show()

2. RGBD和点云

深度图-》RGBD-》点云

需要知道相机内外参数。

point_cloud_to_rgbd.py

import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    
    # 1. read depth and color image
    device = o3d.core.Device('CPU:0')
    tum_data = o3d.data.SampleTUMRGBDImage()
    depth = o3d.t.io.read_image(tum_data.depth_path).to(device)
    color = o3d.t.io.read_image(tum_data.color_path).to(device)
    # 2. depth and color 生成rgbd
    rgbd = o3d.t.geometry.RGBDImage(color, depth)
    
    # 3. rgbd生成pcd
    intrinsic = o3d.core.Tensor([[535.4, 0, 320.1], [0, 539.2, 247.6], [0, 0, 1]])
    pcd = o3d.t.geometry.PointCloud.create_from_rgbd_image(rgbd,
                                                           intrinsic,
                                                           depth_scale=5000.0,
                                                           depth_max=10.0)
    o3d.visualization.draw([pcd])
    
    # 4. pcd生成rgbd
    rgbd_reproj = pcd.project_to_rgbd_image(640,
                                            480,
                                            intrinsic,
                                            depth_scale=5000.0,
                                            depth_max=10.0)

    # 5. view
    fig, axs = plt.subplots(1, 2)
    axs[0].imshow(np.asarray(rgbd_reproj.color.to_legacy()))  # 原始rgbd
    axs[1].imshow(np.asarray(rgbd_reproj.depth.to_legacy()))  # rgbd->pcd->rgbd
    plt.show()

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值