├── README.md └── python_depth_to_point_cloud.py /README.md: -------------------------------------------------------------------------------- 1 | # python_depth_to_point_cloud 2 | Transform depth and RGB image pairs into a .ply file and show it. 3 | 4 | ## Getting Started 5 | Download or copy the .py file to utilize the full funtionality. 6 | 7 | ### Prerequisites 8 | Python3.7 and some several Python libraries. 9 | 10 | ### Installing 11 | Install all related libraries in terminal, 12 | ``` 13 | pip install Pillow pandas numpy open3d-python 14 | ``` 15 | Note: open3d-python might have some problems in version, but you can still get the .ply file via other libs and choose other point cloud lib to show the point cloud directly. 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /python_depth_to_point_cloud.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import pandas as pd 3 | import numpy as np 4 | from open3d import read_point_cloud, draw_geometries 5 | import time 6 | 7 | 8 | class point_cloud_generator(): 9 | 10 | def __init__(self, rgb_file, depth_file, pc_file, focal_length, scalingfactor): 11 | self.rgb_file = rgb_file 12 | self.depth_file = depth_file 13 | self.pc_file = pc_file 14 | self.focal_length = focal_length 15 | self.scalingfactor = scalingfactor 16 | self.rgb = Image.open(rgb_file) 17 | self.depth = Image.open(depth_file).convert('I') 18 | self.width = self.rgb.size[0] 19 | self.height = self.rgb.size[1] 20 | 21 | def calculate(self): 22 | t1=time.time() 23 | depth = np.asarray(self.depth).T 24 | self.Z = depth / self.scalingfactor 25 | X = np.zeros((self.width, self.height)) 26 | Y = np.zeros((self.width, self.height)) 27 | for i in range(self.width): 28 | X[i, :] = np.full(X.shape[1], i) 29 | 30 | self.X = ((X - self.width / 2) * self.Z) / self.focal_length 31 | for i in range(self.height): 32 | Y[:, i] = np.full(Y.shape[0], i) 33 | self.Y = ((Y - self.height / 2) * self.Z) / self.focal_length 34 | 35 | df=np.zeros((6,self.width*self.height)) 36 | df[0] = self.X.T.reshape(-1) 37 | df[1] = -self.Y.T.reshape(-1) 38 | df[2] = -self.Z.T.reshape(-1) 39 | img = np.array(self.rgb) 40 | df[3] = img[:, :, 0:1].reshape(-1) 41 | df[4] = img[:, :, 1:2].reshape(-1) 42 | df[5] = img[:, :, 2:3].reshape(-1) 43 | self.df=df 44 | t2=time.time() 45 | print('calcualte 3d point cloud Done.',t2-t1) 46 | 47 | def write_ply(self): 48 | t1=time.time() 49 | float_formatter = lambda x: "%.4f" % x 50 | points =[] 51 | for i in self.df.T: 52 | points.append("{} {} {} {} {} {} 0\n".format 53 | (float_formatter(i[0]), float_formatter(i[1]), float_formatter(i[2]), 54 | int(i[3]), int(i[4]), int(i[5]))) 55 | 56 | file = open(self.pc_file, "w") 57 | file.write('''ply 58 | format ascii 1.0 59 | element vertex %d 60 | property float x 61 | property float y 62 | property float z 63 | property uchar red 64 | property uchar green 65 | property uchar blue 66 | property uchar alpha 67 | end_header 68 | %s 69 | ''' % (len(points), "".join(points))) 70 | file.close() 71 | 72 | t2=time.time() 73 | print("Write into .ply file Done.",t2-t1) 74 | 75 | def show_point_cloud(self): 76 | pcd = read_point_cloud(self.pc_file) 77 | draw_geometries([pcd]) 78 | 79 | a = point_cloud_generator('p.png', 'd.png', 'pc1.ply', 80 | focal_length=50, scalingfactor=1000) 81 | a.calculate() 82 | a.write_ply() 83 | a.show_point_cloud() 84 | df = a.df 85 | np.save('pc.npy',df) 86 | --------------------------------------------------------------------------------