-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoint_cloud.py
102 lines (72 loc) · 2.42 KB
/
point_cloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sys
import os
from PIL import Image
import numpy as np
from open3d import read_point_cloud, draw_geometries
# Instructions on how to control the view:
# http://www.open3d.org/docs/release/tutorial/Basic/visualization.html
"""
-- Mouse view control --
Left button + drag : Rotate.
Ctrl + left button + drag : Translate.
Wheel button + drag : Translate.
Shift + left button + drag : Roll.
Wheel : Zoom in/out.
-- Keyboard view control --
[/] : Increase/decrease field of view.
R : Reset view point.
Ctrl/Cmd + C : Copy current view status into the clipboard.
Ctrl/Cmd + V : Paste view status from clipboard.
-- General control --
Q, Esc : Exit window.
H : Print help message.
P, PrtScn : Take a screen capture.
D : Take a depth capture.
O : Take a capture of current rendering settings.
Ctrl + c : Save view point as JSON to clipboard
Ctrl + v : Load view point from clipboard
"""
focalLength = 525.0
centerX = 319.5
centerY = 239.5
scalingFactor = 5000.0
# Creates the PLY file
def generate_point_cloud(rgb_file, depth_file):
rgb = Image.open(rgb_file)
depth = Image.open(depth_file)
if rgb.size != depth.size:
raise Exception("Color and depth image do not match in resolution")
points = []
for v in range(rgb.size[1]):
for u in range(rgb.size[0]):
color = rgb.getpixel((u,v))
Z = None
if type(depth.getpixel((u,v))) == tuple:
Z = depth.getpixel((u,v))[0] / scalingFactor
else:
Z = depth.getpixel((u,v)) / scalingFactor
if Z == 0:
continue
X = (u - centerX) * Z / focalLength
Y = (v - centerY) * Z / focalLength
points.append("%f %f %f %d %d %d 0\n"%(X, Y, Z, color[0], color[1], color[2]))
file = open("./temp/pc.ply","w")
# PLY file header
file.write('''ply
format ascii 1.0
element vertex %d
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
property uchar alpha
end_header
%s
'''%(len(points),"".join(points)))
file.close()
# Opens 3D point cloud viewing window on host machine
def display_point_cloud():
pcd = read_point_cloud("./temp/pc.ply")
draw_geometries([pcd])