1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# Copyright (c) 2018-2023 www.open3d.org
5# SPDX-License-Identifier: MIT
6# ----------------------------------------------------------------------------
7
8import open3d as o3d
9import numpy as np
10import matplotlib.image as mpimg
11import re
12
13
14def visualize_rgbd(rgbd_image):
15 print(rgbd_image)
16
17 o3d.visualization.draw_geometries([rgbd_image])
18
19 pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
20 rgbd_image,
21 o3d.camera.PinholeCameraIntrinsic(
22 o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
23 # Flip it, otherwise the pointcloud will be upside down.
24 pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
25 o3d.visualization.draw_geometries([pcd])
26
27
28# This is special function used for reading NYU pgm format
29# as it is written in big endian byte order.
30def read_nyu_pgm(filename, byteorder='>'):
31 with open(filename, 'rb') as f:
32 buffer = f.read()
33 try:
34 header, width, height, maxval = re.search(
35 b"(^P5\s(?:\s*#.*[\r\n])*"
36 b"(\d+)\s(?:\s*#.*[\r\n])*"
37 b"(\d+)\s(?:\s*#.*[\r\n])*"
38 b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
39 except AttributeError:
40 raise ValueError("Not a raw PGM file: '%s'" % filename)
41 img = np.frombuffer(buffer,
42 dtype=byteorder + 'u2',
43 count=int(width) * int(height),
44 offset=len(header)).reshape((int(height), int(width)))
45 img_out = img.astype('u2')
46 return img_out
47
48
49def nyu_dataset():
50 print("Read NYU dataset")
51 # Open3D does not support ppm/pgm file yet. Not using o3d.io.read_image here.
52 # MathplotImage having some ISSUE with NYU pgm file. Not using imread for pgm.
53 nyu_data = o3d.data.SampleNYURGBDImage()
54 color_raw = mpimg.imread(nyu_data.color_path)
55 depth_raw = read_nyu_pgm(nyu_data.depth_path)
56 color = o3d.geometry.Image(color_raw)
57 depth = o3d.geometry.Image(depth_raw)
58 rgbd_image = o3d.geometry.RGBDImage.create_from_nyu_format(
59 color, depth, convert_rgb_to_intensity=False)
60
61 print("Displaying NYU color and depth images and pointcloud ...")
62 visualize_rgbd(rgbd_image)
63
64
65def redwood_dataset():
66 print("Read Redwood dataset")
67 redwood_data = o3d.data.SampleRedwoodRGBDImages()
68 color_raw = o3d.io.read_image(redwood_data.color_paths[0])
69 depth_raw = o3d.io.read_image(redwood_data.depth_paths[0])
70 rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
71 color_raw, depth_raw, convert_rgb_to_intensity=False)
72
73 print("Displaying Redwood color and depth images and pointcloud ...")
74 visualize_rgbd(rgbd_image)
75
76
77def sun_dataset():
78 print("Read SUN dataset")
79 sun_data = o3d.data.SampleSUNRGBDImage()
80 color_raw = o3d.io.read_image(sun_data.color_path)
81 depth_raw = o3d.io.read_image(sun_data.depth_path)
82 rgbd_image = o3d.geometry.RGBDImage.create_from_sun_format(
83 color_raw, depth_raw, convert_rgb_to_intensity=False)
84
85 print("Displaying SUN color and depth images and pointcloud ...")
86 visualize_rgbd(rgbd_image)
87
88
89def tum_dataset():
90 print("Read TUM dataset")
91 tum_data = o3d.data.SampleTUMRGBDImage()
92 color_raw = o3d.io.read_image(tum_data.color_path)
93 depth_raw = o3d.io.read_image(tum_data.depth_path)
94 rgbd_image = o3d.geometry.RGBDImage.create_from_tum_format(
95 color_raw, depth_raw, convert_rgb_to_intensity=False)
96
97 print("Displaying TUM color and depth images and pointcloud ...")
98 visualize_rgbd(rgbd_image)
99
100
101if __name__ == "__main__":
102 nyu_dataset()
103 redwood_dataset()
104 sun_dataset()
105 tum_dataset()