Open3d(五)——RGBD数据集

亲测代码程序可运行使用,open3d版本0.13.0。

open3d数据资源下载:GitHub - Cobotic/Open3D: Open3D: A Modern Library for 3D Data Processing

代码执行功能有:Redwood dataset、SUN dataset、NYU dataset、TUM dataset这四个数据集的读取和使用RGBD图像,详情请见代码。

'''
Author: dongcidaci
Date: 2021-09-10 15:54:33
LastEditTime: 2021-09-10 16:09:18
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \open3d_code\05_RGBDImage.py
'''
import open3d as o3d
import numpy as np
import copy
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import re

#介绍如何从一些著名的RGBD数据集去读取和使用RGBD图像
#Redwood dataset,读取和可视化
#Redwood格式数据将深度存储在16-bit单通道图像中。整数值表示深度,以毫米为单位。它是Open3d解析深度图像的默认格式。
color_raw = o3d.io.read_image("test_data/RGBD/color/00000.jpg")
depth_raw = o3d.io.read_image("test_data/RGBD/depth/00000.png")
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
    color_raw, depth_raw)
print(rgbd_image)
#默认的转换函数 create_rgbd_image_from_color_and_depth 从成对的彩色图(color image)和深度图(depth image)中生成RGBDImage。
# Color图像被转换为灰度图,储存成[0,1]之间的float类型的数据。深度图像也通过float类型存储,表示深度值(单位:米)。
#转换后的结果能够通过numpy数组表示。
plt.subplot(1, 2, 1)
plt.title('Redwood grayscale image')
plt.imshow(rgbd_image.color)
plt.subplot(1, 2, 2)
plt.title('Redwood depth image')
plt.imshow(rgbd_image.depth)
plt.show()
#给定一组相机参数,RGBD图像能够转换成点云。
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
        rgbd_image,
        o3d.camera.PinholeCameraIntrinsic(
            o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
# Flip it, otherwise the pointcloud will be upside down
pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
o3d.visualization.draw_geometries([pcd],)
#这里我们使用PinholeCameraIntrinsicParameters.PrimeSenseDefault 作为默认的相机参数,它的图像分辨率为640x480,焦距(fx,fy)=(525.0,525.0),光学中心(cx,cy)=(319.5,239.5)。
#使用单位矩阵作为默认的外部参数。pcd.transform在点云上应用上下翻转实现更好的可视化的目的。

#SUN dataset数据集
#与上面处理Redwood数据几乎相同。唯一的不同是我们使用create_rgbd_image_from_sun_format转换函数来从SUN数据集解析深度图像。
color_raw = o3d.io.read_image("test_data/RGBD/other_formats/SUN_color.jpg")
depth_raw = o3d.io.read_image("test_data/RGBD/other_formats/SUN_depth.png")
rgbd_image = o3d.geometry.RGBDImage.create_from_sun_format(color_raw, depth_raw)
print(rgbd_image)

plt.subplot(1, 2, 1)
plt.title('SUN grayscale image')
plt.imshow(rgbd_image.color)
plt.subplot(1, 2, 2)
plt.title('SUN depth image')
plt.imshow(rgbd_image.depth)
plt.show()

pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
        rgbd_image,
        o3d.camera.PinholeCameraIntrinsic(
            o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
# Flip it, otherwise the pointcloud will be upside down
pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
o3d.visualization.draw_geometries([pcd],)

#NYU datase数据集
#与Redwood几乎相同,只有两处不一样。
# 首先,NYU图像不是标准的jpg或者png格式,因此我们需要使用 mpimg.imread来读取一个color图像为一个numpy数组,并将其转化为Open3d图像。
# 还需要使用一个额外的辅助函数read_nyu_pgm来从 NYU数据集使用的特殊大端模式(special big endian) pgm格式的数据中读取深度图像。
# 其次我们使用create_rgbd_image_from_nyu_format转换函数来从NYU数据集中解析深度图。
def read_nyu_pgm(filename, byteorder='>'):
    with open(filename, 'rb') as f:
        buffer = f.read()
    try:
        header, width, height, maxval = re.search(
            b"(^P5\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
    except AttributeError:
        raise ValueError("Not a raw PGM file: '%s'" % filename)
    img = np.frombuffer(buffer,
                        dtype=byteorder + 'u2',
                        count=int(width) * int(height),
                        offset=len(header)).reshape((int(height), int(width)))
    img_out = img.astype('u2')
    return img_out


print("Read NYU dataset")
# Open3D does not support ppm/pgm file yet. Not using o3d.io.read_image here.
# MathplotImage having some ISSUE with NYU pgm file. Not using imread for pgm.
color_raw = mpimg.imread("test_data/RGBD/other_formats/NYU_color.ppm")
depth_raw = read_nyu_pgm("test_data/RGBD/other_formats/NYU_depth.pgm")
color = o3d.geometry.Image(color_raw)
depth = o3d.geometry.Image(depth_raw)
rgbd_image = o3d.geometry.RGBDImage.create_from_nyu_format(color, depth)
print(rgbd_image)
plt.subplot(1, 2, 1)
plt.title('NYU grayscale image')
plt.imshow(rgbd_image.color)
plt.subplot(1, 2, 2)
plt.title('NYU depth image')
plt.imshow(rgbd_image.depth)
plt.show()
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
        rgbd_image,
        o3d.camera.PinholeCameraIntrinsic(
            o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
# Flip it, otherwise the pointcloud will be upside down
pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
o3d.visualization.draw_geometries([pcd],)

#TUM dataset数据集
#和Redwood数据集的介绍也几乎一样。只有一点不同是我们使用create_rgbd_image_from_tum_format函数去从TUM数据集中解析深度数据。
color_raw = o3d.io.read_image("test_data/RGBD/other_formats/TUM_color.png")
depth_raw = o3d.io.read_image("test_data/RGBD/other_formats/TUM_depth.png")
rgbd_image = o3d.geometry.RGBDImage.create_from_tum_format(color_raw, depth_raw)
print(rgbd_image)
plt.subplot(1, 2, 1)
plt.title('TUM grayscale image')
plt.imshow(rgbd_image.color)
plt.subplot(1, 2, 2)
plt.title('TUM depth image')
plt.imshow(rgbd_image.depth)
plt.show()
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
        rgbd_image,
        o3d.camera.PinholeCameraIntrinsic(
            o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
# Flip it, otherwise the pointcloud will be upside down
pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
o3d.visualization.draw_geometries([pcd],)

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
将Kinetics数据集转换为RGBD数据集格式需要进行以下步骤: 1. 下载Kinetics数据集,并将其解压缩。 2. 安装OpenCV库,用于读取和处理视频帧。 3. 对于每个视频,将其分成RGB和深度图像。可以使用深度相机或者RGB-D相机来获取深度图像。 4. 将RGB和深度图像保存为PNG格式,命名为`rgb_{frame_number}.png`和`depth_{frame_number}.png`。 5. 创建一个`.txt`文件,其中每一行包含视频的路径、帧率和帧数。例如: ``` path/to/video1.mp4 24 120 path/to/video2.mp4 30 150 ``` 6. 将所有的RGB和深度图像以及`.txt`文件放入同一个文件夹中。 7. 运行代码,将Kinetics数据集转换为RGBD数据集格式。以下是一个Python脚本,可以将Kinetics数据集转换为RGBD数据集格式: ```python import cv2 import os import numpy as np input_folder = "path/to/kinetics/dataset" output_folder = "path/to/rgbd/dataset" if not os.path.exists(output_folder): os.makedirs(output_folder) for root, dirs, files in os.walk(input_folder): for file in files: if file.endswith(".mp4"): video_path = os.path.join(root, file) video_name = os.path.splitext(file)[0] output_dir = os.path.join(output_folder, video_name) if not os.path.exists(output_dir): os.makedirs(output_dir) cap = cv2.VideoCapture(video_path) fps = cap.get(cv2.CAP_PROP_FPS) num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) for i in range(num_frames): ret, frame = cap.read() if not ret: break # Convert frame to grayscale and apply a threshold gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) _, threshold_frame = cv2.threshold(gray_frame, 1, 255, cv2.THRESH_BINARY) # Split the thresholded frame into RGB and depth images depth_frame = np.stack((threshold_frame,)*3, axis=-1) rgb_frame = frame # Save the RGB and depth images as PNG files depth_filename = os.path.join(output_dir, "depth_{}.png".format(i)) rgb_filename = os.path.join(output_dir, "rgb_{}.png".format(i)) cv2.imwrite(depth_filename, depth_frame) cv2.imwrite(rgb_filename, rgb_frame) # Write the video information to a text file txt_filename = os.path.join(output_dir, "info.txt") with open(txt_filename, "w") as f: f.write("{} {} {}".format(video_path, fps, num_frames)) ``` 这个脚本将Kinetics数据集中的每个视频转换为RGBD数据集格式,并将其保存在指定的输出文件夹中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值