亲测代码程序可运行使用,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],)