Python Open3D几何图形 基础篇(三) RGBD图像
本文所参考网页:
RGBD images — Open3D 0.15.1 documentation
- Open3D中有一个关于图像的数据结构。它支持各种函数,如
read_image
,write_image
,filter_image
anddraw_geometries
。 Open3D图像可以直接转换为一个numpy数组或从numpy数组转换为Open3D图像。- 一个Open3D 的
RGBDImage
由两个部分组成,RGBDImage.depth
和RGBDImage.color
。我们需要将两幅图像配准(registered)到同一个相机帧中,并具有相同的分辨率。- 以下教程介绍如何从许多著名的RGBD数据集中读取和使用RGBD图像。
Redwood 数据集(Redwood dataset)
-
在本章节会展示如何去读取和可视化Redwood dataset [Choi2015]_ 中的
RGBDImage
-
Redwood 格式将深度存储在16位单通道图像中。整数值表示深度测量值,单位为毫米。这是Open3D解析深度图像的默认格式
print("Read Redwood dataset") redwood_rgbd = o3d.data.SampleRedwoodRGBDImages() color_raw = o3d.io.read_image(redwood_rgbd.color_paths[0]) depth_raw = o3d.io.read_image(redwood_rgbd.depth_paths[0]) rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth( color_raw, depth_raw) print(rgbd_image) """ RGBDImage of size Color image : 640x480, with 1 channels. Depth image : 640x480, with 1 channels. Use numpy.asarray to access buffer data. """
-
默认的转换函数
create_rgbd_image_from_color_and_depth
从一对颜色和深度图像创建RGBDImage。彩色图像被转换为灰度图像,存储在float
范围为[0,1]。深度图像以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], zoom=0.5)
这里使用
PinholeCameraIntrinsicParameters.PrimeSenseDefault
作为默认的相机参数它的图像分辨率为640x480,焦距(fx, fy)=(525.0, 525.0),光学中心(cx, cy)=(319.5, 239.5)。单位矩阵用作默认的外部参数。
pcd.transform
在点云上应用上下翻转变换,以实现更好的可视化效果。
SUN数据集(SUN dataset)
-
在本章节会展示如何去读取和可视化SUN dataset [Song2015]_中的RGBDImage。
-
本教程与上面处理Redwood 数据集的教程几乎相同。不同的地方仅在于使用了
create_rgbd_image_from_sun_format
这个函数去处理SUN数据集中的深度图像print("Read SUN dataset") sun_rgbd = o3d.data.SampleSUNRGBDImage() color_raw = o3d.io.read_image(sun_rgbd.color_path) depth_raw = o3d.io.read_image(sun_rgbd.depth_path) rgbd_image = o3d.geometry.RGBDImage.create_from_sun_format(color_raw, depth_raw) print(rgbd_image) """ RGBDImage of size Color image : 640x480, with 1 channels. Depth image : 640x480, with 1 channels. Use numpy.asarray to access buffer data. """
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], zoom=0.5)
NYU数据集(NYU dataset)
-
在本章节会展示如何去读取和可视化NYU dataset [Silberman2012]_中的
RGBDImage
。 -
本教程几乎与上面处理Redwood 数据集的教程相同,但有两个不同点。
- 首先,NYU图像不是标准的
jpg
或png
格式。因此,使用mpimg.imread
以numpy数组的形式读取彩色图像,并将其转换为Open3DImage
。另一个辅助函数read_nyu_pgm
,调用它可以从NYU 数据集中使用的特殊大端(big-endian)pgm
格式读取深度图像。 - 其次,使用了不同的转换函数
create_rgbd_image_from_nyu_format
解析SUN数据集中的深度图像。(原文也是SUN 我觉得很纳闷)
import matplotlib.image as mpimg import re # This is special function used for reading NYU pgm format # as it is written in big endian byte order. 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. nyu_rgbd = o3d.data.SampleNYURGBDImage() color_raw = mpimg.imread(nyu_rgbd.color_path) depth_raw = read_nyu_pgm(nyu_rgbd.depth_path) 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) """ RGBDImage of size Color image : 640x480, with 1 channels. Depth image : 640x480, with 1 channels. Use numpy.asarray to access buffer data. """
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], zoom=0.5)
- 首先,NYU图像不是标准的
TUM数据集(TUM dataset)
-
在本章节会展示如何去读取和可视化TUM dataset [Strum2012]_ 中的RGBDImage。
-
本教程与上面处理Redwood 数据集的教程几乎相同。不同的地方仅在于使用了
create_rgbd_image_from_tum_format
这个函数去处理TUM 数据集中的深度图像。print("Read TUM dataset") tum_rgbd = o3d.data.SampleSUNRGBDImage() color_raw = o3d.io.read_image(tum_rgbd.color_path) depth_raw = o3d.io.read_image(tum_rgbd.depth_path) rgbd_image = o3d.geometry.RGBDImage.create_from_tum_format(color_raw, depth_raw) print(rgbd_image) """ Read TUM dataset RGBDImage of size Color image : 640x480, with 1 channels. Depth image : 640x480, with 1 channels. Use numpy.asarray to access buffer data. """
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], zoom=0.35)