RGBD Image

rgbd_datasets.py

  1# ----------------------------------------------------------------------------
  2# -                        Open3D: www.open3d.org                            -
  3# ----------------------------------------------------------------------------
  4# The MIT License (MIT)
  5#
  6# Copyright (c) 2018-2021 www.open3d.org
  7#
  8# Permission is hereby granted, free of charge, to any person obtaining a copy
  9# of this software and associated documentation files (the "Software"), to deal
 10# in the Software without restriction, including without limitation the rights
 11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12# copies of the Software, and to permit persons to whom the Software is
 13# furnished to do so, subject to the following conditions:
 14#
 15# The above copyright notice and this permission notice shall be included in
 16# all copies or substantial portions of the Software.
 17#
 18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 24# IN THE SOFTWARE.
 25# ----------------------------------------------------------------------------
 26
 27import open3d as o3d
 28import numpy as np
 29import matplotlib.image as mpimg
 30import re
 31
 32
 33def visualize_rgbd(rgbd_image):
 34    print(rgbd_image)
 35
 36    o3d.visualization.draw_geometries([rgbd_image])
 37
 38    pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
 39        rgbd_image,
 40        o3d.camera.PinholeCameraIntrinsic(
 41            o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
 42    # Flip it, otherwise the pointcloud will be upside down.
 43    pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
 44    o3d.visualization.draw_geometries([pcd])
 45
 46
 47# This is special function used for reading NYU pgm format
 48# as it is written in big endian byte order.
 49def read_nyu_pgm(filename, byteorder='>'):
 50    with open(filename, 'rb') as f:
 51        buffer = f.read()
 52    try:
 53        header, width, height, maxval = re.search(
 54            b"(^P5\s(?:\s*#.*[\r\n])*"
 55            b"(\d+)\s(?:\s*#.*[\r\n])*"
 56            b"(\d+)\s(?:\s*#.*[\r\n])*"
 57            b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
 58    except AttributeError:
 59        raise ValueError("Not a raw PGM file: '%s'" % filename)
 60    img = np.frombuffer(buffer,
 61                        dtype=byteorder + 'u2',
 62                        count=int(width) * int(height),
 63                        offset=len(header)).reshape((int(height), int(width)))
 64    img_out = img.astype('u2')
 65    return img_out
 66
 67
 68def nyu_dataset():
 69    print("Read NYU dataset")
 70    # Open3D does not support ppm/pgm file yet. Not using o3d.io.read_image here.
 71    # MathplotImage having some ISSUE with NYU pgm file. Not using imread for pgm.
 72    nyu_data = o3d.data.SampleNYURGBDImage()
 73    color_raw = mpimg.imread(nyu_data.color_path)
 74    depth_raw = read_nyu_pgm(nyu_data.depth_path)
 75    color = o3d.geometry.Image(color_raw)
 76    depth = o3d.geometry.Image(depth_raw)
 77    rgbd_image = o3d.geometry.RGBDImage.create_from_nyu_format(
 78        color, depth, convert_rgb_to_intensity=False)
 79
 80    print("Displaying NYU color and depth images and pointcloud ...")
 81    visualize_rgbd(rgbd_image)
 82
 83
 84def redwood_dataset():
 85    print("Read Redwood dataset")
 86    redwood_data = o3d.data.SampleRedwoodRGBDImages()
 87    color_raw = o3d.io.read_image(redwood_data.color_paths[0])
 88    depth_raw = o3d.io.read_image(redwood_data.depth_paths[0])
 89    rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
 90        color_raw, depth_raw, convert_rgb_to_intensity=False)
 91
 92    print("Displaying Redwood color and depth images and pointcloud ...")
 93    visualize_rgbd(rgbd_image)
 94
 95
 96def sun_dataset():
 97    print("Read SUN dataset")
 98    sun_data = o3d.data.SampleSUNRGBDImage()
 99    color_raw = o3d.io.read_image(sun_data.color_path)
100    depth_raw = o3d.io.read_image(sun_data.depth_path)
101    rgbd_image = o3d.geometry.RGBDImage.create_from_sun_format(
102        color_raw, depth_raw, convert_rgb_to_intensity=False)
103
104    print("Displaying SUN color and depth images and pointcloud ...")
105    visualize_rgbd(rgbd_image)
106
107
108def tum_dataset():
109    print("Read TUM dataset")
110    tum_data = o3d.data.SampleTUMRGBDImage()
111    color_raw = o3d.io.read_image(tum_data.color_path)
112    depth_raw = o3d.io.read_image(tum_data.depth_path)
113    rgbd_image = o3d.geometry.RGBDImage.create_from_tum_format(
114        color_raw, depth_raw, convert_rgb_to_intensity=False)
115
116    print("Displaying TUM color and depth images and pointcloud ...")
117    visualize_rgbd(rgbd_image)
118
119
120if __name__ == "__main__":
121    nyu_dataset()
122    redwood_dataset()
123    sun_dataset()
124    tum_dataset()