-
Notifications
You must be signed in to change notification settings - Fork 2
/
stereo_depth_cuda_open3d.py
236 lines (179 loc) · 6.14 KB
/
stereo_depth_cuda_open3d.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import open3d as o3d
import cv2
import numpy as np
from matplotlib import pyplot as plt
from StereoCameraCalibrate.Stereo_Calib_Camera import stereoCalibrateCamera
from StereoCameraCalibrate.Stereo_Calib_Camera import getStereoCameraParameters, getStereoSingleCameraParameters
import Camera.jetsonCam as jetCam
def draw_lines(img):
for i in range(0,img.shape[0],30):
cv2.line(img,(0,i),(img.shape[1],i),(255,0,0),1)
return img
def getPoints(points,img_disp,img):
points[:,2]=img_disp.flatten()
img=img/255
colours = np.stack(((img[:,:,2]).flatten(),(img[:,:,1]).flatten(),(img[:,:,0]).flatten()),1)
return points, colours
cam1 = jetCam.jetsonCam()
cam2 = jetCam.jetsonCam()
cam1.open(sensor_id=1,
sensor_mode=3,
flip_method=0,
display_height=540,
display_width=960,
)
cam2.open(sensor_id=0,
sensor_mode=3,
flip_method=0,
display_height=540,
display_width=960,
)
cam1.start()
cam2.start()
#stereoCalibrateCamera(cam1,cam2,'jetson_stereo_8MP',24)
lod_data = getStereoCameraParameters('jetson_stereo_8MP.npz')
lod_datac1 = getStereoSingleCameraParameters('jetson_stereo_8MPc1.npz')
lod_datac2 = getStereoSingleCameraParameters('jetson_stereo_8MPc2.npz')
print(lod_datac1[0])
print(lod_datac2[0])
print(lod_datac1[1])
print(lod_datac2[1])
camera_matrix_left = lod_data[0]
dist_coeffs_left = lod_data[1]
camera_matrix_right = lod_data[2]
dist_coeffs_right = lod_data[3]
R = lod_data[4]
T = lod_data[5]
#print camera matrix
print("RAW camera matrix")
print(camera_matrix_right)
print(camera_matrix_left)
ret,img_s = cam1.read()
if ret:
image_size = (img_s.shape[1],img_s.shape[0])
print(image_size)
else:
cam1.stop()
cam2.stop()
cam1.release()
cam2.release()
exit()
print('here4')
#stereo rectify
R1,R2,P1,P2,Q,roi1,roi2= cv2.stereoRectify(camera_matrix_left,dist_coeffs_left, camera_matrix_right, dist_coeffs_right, image_size, R, T)
block_s = 5
num_disp= 16
# Create a StereoBM object
#stereo = cv2.StereoBM_create(numDisparities=num_disp, blockSize=block_s)
stereo = cv2.cuda.createStereoBM(numDisparities=num_disp, blockSize=block_s)
# Load rectification maps
map1_left, map2_left = cv2.initUndistortRectifyMap( camera_matrix_left, dist_coeffs_left, R1, P1, image_size, cv2.CV_16SC2)
map1_right, map2_right = cv2.initUndistortRectifyMap(camera_matrix_right, dist_coeffs_right, R2, P2, image_size, cv2.CV_16SC2)
# Initialize GPU capture object
gpu_mat_l = cv2.cuda_GpuMat()
gpu_mat_r = cv2.cuda_GpuMat()
# Create output disparity map
gpu_disparity = cv2.cuda_GpuMat()
# Create CUDA stream
stream = cv2.cuda_Stream()
h,w=image_size[:2]
total_pix=w*h
points = np.ones((total_pix,3)) *255
colours= np.ones((total_pix,3))
#points = np.random.rand(300, 3)
count = 0
for i in range(0,w,1):
for j in range(0,h,1):
if count ==total_pix:
break
points[count]= [i,j,0 ]
count+=1
# Step 2: Convert the NumPy array to an Open3D PointCloud object
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(points)
#point_cloud.colors = o3d.utility.Vector3dVector(colours)
# Step 3: Visualize the PointCloud using Open3D's visualization tools
#o3d.visualization.draw_geometries([point_cloud])
import time
vis = o3d.visualization.Visualizer()
if vis is None:
print('vis is none')
cam1.stop()
cam2.stop()
cam1.release()
cam2.release()
exit()
vis.create_window(
window_name='Stereo depth',
width=960,
height=540,
left=480,
top=270)
vis.get_render_option().background_color = [0.05, 0.05, 0.05]
vis.get_render_option().point_size = 1
vis.get_render_option().show_coordinate_frame = True
vis.add_geometry(point_cloud)
frame = 0
while True:
# Read stereo images
ret,image_left = cam1.read()
ret, image_right = cam2.read()
# Remap the images using rectification maps
rectified_left = cv2.remap(image_left, map1_left, map2_left, cv2.INTER_LINEAR)
rectified_right = cv2.remap(image_right, map1_right, map2_right, cv2.INTER_LINEAR)
gpu_mat_l.upload(rectified_left)
gpu_mat_r.upload(rectified_right)
# Convert images to grayscale
gray_left = cv2.cuda.cvtColor(gpu_mat_l, cv2.COLOR_BGR2GRAY)
gray_right = cv2.cuda.cvtColor(gpu_mat_r, cv2.COLOR_BGR2GRAY)
# Compute disparity map
gpu_disparity = stereo.compute(gray_left, gray_right, stream=stream)
# Download disparity map from GPU to CPU memory
disparity = gpu_disparity.download()
# Normalize the disparity map to the range [0, 1]
normalized_disparity_map = cv2.normalize(disparity, None, 0.0, 1.0, cv2.NORM_MINMAX,cv2.CV_32F)
colormap_image = cv2.applyColorMap(np.uint8(normalized_disparity_map * 255), cv2.COLORMAP_JET)
#print(time.time(),end=' ')
points, colours= getPoints(points,normalized_disparity_map*255, colormap_image)
#print(time.time(),)
#points = np.random.rand(300, 3)
point_cloud.points = o3d.utility.Vector3dVector(points)
point_cloud.colors = o3d.utility.Vector3dVector(colours)
vis.update_geometry(point_cloud)
vis.poll_events()
vis.update_renderer()
# This can fix Open3D jittering issues:
#time.sleep(0.005)
#cv2.imshow('Depth colour map',colormap_image )
com_img= cv2.hconcat([rectified_left,rectified_right])
com_img=draw_lines(com_img)
#cv2.imshow('img_tog',com_img)
cv2.imshow('img left - img right - depth map',cv2.hconcat([cv2.resize(com_img, (0,0), fx=0.6, fy=0.6),cv2.resize(colormap_image, (0,0), fx=0.6, fy=0.6)]))
k=cv2.waitKey(1)
if k == ord('x'):
break
elif k == ord('q'):
#increase block size
block_s +=2
print("block_size:"+str(block_s))
stereo.setBlockSize(block_s)
elif k == ord('a'):
#decrease block size
block_s =max(block_s-2,5)
print("block_size:"+str(block_s))
stereo.setBlockSize(block_s)
elif k == ord('w'):
#increase disparity
num_disp +=16
print("disparity:"+str(num_disp))
stereo.setNumDisparities(num_disp)
elif k == ord('s'):
#decrease disparity
num_disp =max(16, num_disp-16)
print("disparity:"+str(num_disp))
stereo.setNumDisparities(num_disp)
cv2.destroyAllWindows()
cam1.stop()
cam2.stop()
cam1.release()
cam2.release()