-
Notifications
You must be signed in to change notification settings - Fork 0
/
zed_depth_point_cloud.py
178 lines (146 loc) · 5.34 KB
/
zed_depth_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import sys
import numpy as np
import pyzed.sl as sl
import cv2
help_string = "[s] Save side by side image [d] Save Depth, [n] Change Depth format, [p] Save Point Cloud, [m] Change Point Cloud format, [q] Quit"
prefix_point_cloud = "Cloud_"
prefix_depth = "Depth_"
path = "./"
count_save = 0
mode_point_cloud = 0
mode_depth = 0
point_cloud_format_ext = ".ply"
depth_format_ext = ".png"
def point_cloud_format_name():
global mode_point_cloud
if mode_point_cloud > 3:
mode_point_cloud = 0
switcher = {
0: ".xyz",
1: ".pcd",
2: ".ply",
3: ".vtk",
}
return switcher.get(mode_point_cloud, "nothing")
def depth_format_name():
global mode_depth
if mode_depth > 2:
mode_depth = 0
switcher = {
0: ".png",
1: ".pfm",
2: ".pgm",
}
return switcher.get(mode_depth, "nothing")
def save_point_cloud(zed, filename) :
print("Saving Point Cloud...")
tmp = sl.Mat()
zed.retrieve_measure(tmp, sl.MEASURE.XYZRGBA)
saved = (tmp.write(filename + point_cloud_format_ext) == sl.ERROR_CODE.SUCCESS)
if saved :
print("Done")
else :
print("Failed... Please check that you have permissions to write on disk")
def save_depth(zed, filename) :
print("Saving Depth Map...")
tmp = sl.Mat()
zed.retrieve_measure(tmp, sl.MEASURE.DEPTH)
saved = (tmp.write(filename + depth_format_ext) == sl.ERROR_CODE.SUCCESS)
if saved :
print("Done")
else :
print("Failed... Please check that you have permissions to write on disk")
def save_sbs_image(zed, filename) :
image_sl_left = sl.Mat()
zed.retrieve_image(image_sl_left, sl.VIEW.LEFT)
image_cv_left = image_sl_left.get_data()
image_sl_right = sl.Mat()
zed.retrieve_image(image_sl_right, sl.VIEW.RIGHT)
image_cv_right = image_sl_right.get_data()
sbs_image = np.concatenate((image_cv_left, image_cv_right), axis=1)
cv2.imwrite(filename, sbs_image)
def process_key_event(zed, key) :
global mode_depth
global mode_point_cloud
global count_save
global depth_format_ext
global point_cloud_format_ext
if key == 100 or key == 68:
save_depth(zed, path + prefix_depth + str(count_save))
count_save += 1
elif key == 110 or key == 78:
mode_depth += 1
depth_format_ext = depth_format_name()
print("Depth format: ", depth_format_ext)
elif key == 112 or key == 80:
save_point_cloud(zed, path + prefix_point_cloud + str(count_save))
count_save += 1
elif key == 109 or key == 77:
mode_point_cloud += 1
point_cloud_format_ext = point_cloud_format_name()
print("Point Cloud format: ", point_cloud_format_ext)
elif key == 104 or key == 72:
print(help_string)
elif key == 115:
save_sbs_image(zed, "ZED_image" + str(count_save) + ".png")
count_save += 1
else:
a = 0
def print_help() :
print(" Press 's' to save Side by side images")
print(" Press 'p' to save Point Cloud")
print(" Press 'd' to save Depth image")
print(" Press 'm' to switch Point Cloud format")
print(" Press 'n' to switch Depth format")
def main() :
# Create a ZED camera object
zed = sl.Camera()
# Set configuration parameters
input_type = sl.InputType()
if len(sys.argv) >= 2 :
input_type.set_from_svo_file(sys.argv[1])
init = sl.InitParameters(input_t=input_type)
init.camera_resolution = sl.RESOLUTION.HD1080
init.depth_mode = sl.DEPTH_MODE.PERFORMANCE
init.coordinate_units = sl.UNIT.MILLIMETER
# Open the camera
err = zed.open(init)
if err != sl.ERROR_CODE.SUCCESS :
print(repr(err))
zed.close()
exit(1)
# Display help in console
print_help()
# Set runtime parameters after opening the camera
runtime = sl.RuntimeParameters()
runtime.sensing_mode = sl.SENSING_MODE.STANDARD
# Prepare new image size to retrieve half-resolution images
image_size = zed.get_camera_information().camera_resolution
image_size.width = image_size.width /2
image_size.height = image_size.height /2
# Declare your sl.Mat matrices
image_zed = sl.Mat(image_size.width, image_size.height, sl.MAT_TYPE.U8_C4)
depth_image_zed = sl.Mat(image_size.width, image_size.height, sl.MAT_TYPE.U8_C4)
point_cloud = sl.Mat()
key = ' '
while key != 113 :
err = zed.grab(runtime)
if err == sl.ERROR_CODE.SUCCESS :
# Retrieve the left image, depth image in the half-resolution
zed.retrieve_image(image_zed, sl.VIEW.LEFT, sl.MEM.CPU, image_size)
zed.retrieve_image(depth_image_zed, sl.VIEW.DEPTH, sl.MEM.CPU, image_size)
# Retrieve the RGBA point cloud in half resolution
zed.retrieve_measure(point_cloud, sl.MEASURE.XYZRGBA, sl.MEM.CPU, image_size)
# To recover data from sl.Mat to use it with opencv, use the get_data() method
# It returns a numpy array that can be used as a matrix with opencv
image_ocv = image_zed.get_data()
depth_image_ocv = depth_image_zed.get_data()
cv2.imshow("Image", image_ocv)
cv2.imshow("Depth", depth_image_ocv)
key = cv2.waitKey(10)
process_key_event(zed, key)
cv2.destroyAllWindows()
zed.close()
print("\nFINISH")
if __name__ == "__main__":
main()