1# ---------------------------------------------------------------------------- 2# - Open3D: www.open3d.org - 3# ---------------------------------------------------------------------------- 4# Copyright (c) 2018-2024 www.open3d.org 5# SPDX-License-Identifier: MIT 6# ---------------------------------------------------------------------------- 7 8importopen3daso3d 9importnumpyasnp 10importmatplotlib.imageasmpimg 11importre 12 13 14defvisualize_rgbd(rgbd_image): 15print(rgbd_image) 16 17o3d.visualization.draw_geometries([rgbd_image]) 18 19pcd=o3d.geometry.PointCloud.create_from_rgbd_image( 20rgbd_image, 21o3d.camera.PinholeCameraIntrinsic( 22o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault)) 23# Flip it, otherwise the pointcloud will be upside down. 24pcd.transform([[1,0,0,0],[0,-1,0,0],[0,0,-1,0],[0,0,0,1]]) 25o3d.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. 30defread_nyu_pgm(filename,byteorder='>'): 31withopen(filename,'rb')asf: 32buffer=f.read() 33try: 34header,width,height,maxval=re.search( 35b"(^P5\s(?:\s*#.*[\r\n])*" 36b"(\d+)\s(?:\s*#.*[\r\n])*" 37b"(\d+)\s(?:\s*#.*[\r\n])*" 38b"(\d+)\s(?:\s*#.*[\r\n]\s)*)",buffer).groups() 39exceptAttributeError: 40raiseValueError("Not a raw PGM file: '%s'"%filename) 41img=np.frombuffer(buffer, 42dtype=byteorder+'u2', 43count=int(width)*int(height), 44offset=len(header)).reshape((int(height),int(width))) 45img_out=img.astype('u2') 46returnimg_out 47 48 49defnyu_dataset(): 50print("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. 53nyu_data=o3d.data.SampleNYURGBDImage() 54color_raw=mpimg.imread(nyu_data.color_path) 55depth_raw=read_nyu_pgm(nyu_data.depth_path) 56color=o3d.geometry.Image(color_raw) 57depth=o3d.geometry.Image(depth_raw) 58rgbd_image=o3d.geometry.RGBDImage.create_from_nyu_format( 59color,depth,convert_rgb_to_intensity=False) 60 61print("Displaying NYU color and depth images and pointcloud ...") 62visualize_rgbd(rgbd_image) 63 64 65defredwood_dataset(): 66print("Read Redwood dataset") 67redwood_data=o3d.data.SampleRedwoodRGBDImages() 68color_raw=o3d.io.read_image(redwood_data.color_paths[0]) 69depth_raw=o3d.io.read_image(redwood_data.depth_paths[0]) 70rgbd_image=o3d.geometry.RGBDImage.create_from_color_and_depth( 71color_raw,depth_raw,convert_rgb_to_intensity=False) 72 73print("Displaying Redwood color and depth images and pointcloud ...") 74visualize_rgbd(rgbd_image) 75 76 77defsun_dataset(): 78print("Read SUN dataset") 79sun_data=o3d.data.SampleSUNRGBDImage() 80color_raw=o3d.io.read_image(sun_data.color_path) 81depth_raw=o3d.io.read_image(sun_data.depth_path) 82rgbd_image=o3d.geometry.RGBDImage.create_from_sun_format( 83color_raw,depth_raw,convert_rgb_to_intensity=False) 84 85print("Displaying SUN color and depth images and pointcloud ...") 86visualize_rgbd(rgbd_image) 87 88 89deftum_dataset(): 90print("Read TUM dataset") 91tum_data=o3d.data.SampleTUMRGBDImage() 92color_raw=o3d.io.read_image(tum_data.color_path) 93depth_raw=o3d.io.read_image(tum_data.depth_path) 94rgbd_image=o3d.geometry.RGBDImage.create_from_tum_format( 95color_raw,depth_raw,convert_rgb_to_intensity=False) 96 97print("Displaying TUM color and depth images and pointcloud ...") 98visualize_rgbd(rgbd_image) 99100101if__name__=="__main__":102nyu_dataset()103redwood_dataset()104sun_dataset()105tum_dataset()