-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstereo_depth_cpu_hsv.py
141 lines (112 loc) · 3.94 KB
/
stereo_depth_cpu_hsv.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
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
cam1 = jetCam.jetsonCam()
cam2 = jetCam.jetsonCam()
#left camera
cam1.open(sensor_id=1,
sensor_mode=3,
flip_method=0,
display_height=540,
display_width=960,
)
#right camera
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()
#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)
# 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)
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)
# Convert images to grayscale
gray_left = cv2.cvtColor(rectified_left, cv2.COLOR_BGR2GRAY)
gray_right = cv2.cvtColor(rectified_right, cv2.COLOR_BGR2GRAY)
# Compute disparity map
disparity = stereo.compute(gray_left, gray_right)
# 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)
#cv2.imshow('Depth colour map',colormap_image )
com_img= cv2.hconcat([rectified_left,rectified_right])
com_img=draw_lines(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(33)
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()