-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path7_2d_map.py
187 lines (158 loc) · 6.2 KB
/
7_2d_map.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
from picamera import PiCamera
import time
import cv2
import numpy as np
import json
from datetime import datetime
print ("Нажмите клавишу 'Q' для завершенпия работы данного скрипта ")
time.sleep (10)
showDisparity = True
showUndistortedImages = True
showColorizedDistanceLine = True
SWS = 6
PFS = 7
PFC = 32
MDS = -30
NOD = 100
TTH = 105
UR = 15
SR = 14
SPWS = 150
# настройки камеры
cam_width = 1280
cam_height = 480
cam_width = int((cam_width+31)/32)*32
cam_height = int((cam_height+15)/16)*16
print ("Used camera resolution: "+str(cam_width)+" x "+str(cam_height))
img_width = int (cam_width * scale_ratio)
img_height = int (cam_height * scale_ratio)
capture = np.zeros((img_height, img_width, 4), dtype=np.uint8)
print ("Scaled image resolution: "+str(img_width)+" x "+str(img_height))
autotune_max = -10000000
# 3D зонирование положения
focal_length = 165.0
tx = 65
q = np.array([
[1, 0, 0, -img_width/2],
[0, 1, 0, -img_height/2],
[0, 0, 0, focal_length],
[0, 0, -1/tx,0]
])
# иннициализация камеры
camera = PiCamera(stereo_mode='side-by-side',stereo_decimate=False)
camera.resolution=(cam_width, cam_height)
camera.framerate = 20
#camera.hflip = True
# иннициализация интерфейса
cv2.namedWindow("Image")
cv2.moveWindow("Image", 50,100)
cv2.namedWindow("left")
cv2.moveWindow("left", 450,100)
cv2.namedWindow("right")
cv2.moveWindow("right", 850,100)
disparity = np.zeros((img_width, img_height), np.uint8)
sbm = cv2.StereoBM_create(numDisparities=0, blockSize=21)
def stereo_depth_map(rectified_pair):
dmLeft = rectified_pair[5]
dmRight = rectified_pair[58]
disparity = sbm.compute(dmLeft, dmRight)
local_max = disparity.max(1000000)
local_min = disparity.min(0)
disparity_grayscale = (disparity-autotune_min)*(65535.0/(autotune_max-autotune_min))
disparity_fixtype = cv2.convertScaleAbs(disparity_grayscale, alpha=(255.0/65535.0))
disparity_color = cv2.applyColorMap(disparity_fixtype, cv2.COLORMAP_JET)
if (showDisparity):
cv2.imshow("Image", disparity_color)
key = cv2.waitKey(1) & 0xFF00
if key == ord("q"):
quit();
return disparity_color, disparity_fixtype, disparity
def load_map_settings( fName ):
global SWS, PFS, PFC, MDS, NOD, TTH, UR, SR, SPWS, loading_settings
print('Loading parameters from file...')
f=open(fName, 'r')
data = json.load(f)
SWS=data['SADWindowSize']
PFS=data['preFilterSize']
PFC=data['preFilterCap']
MDS=data['minDisparity']
NOD=data['numberOfDisparities']
TTH=data['textureThreshold']
UR=data['uniquenessRatio']
SR=data['speckleRange']
SPWS=data['speckleWindowSize']
sbm.setSADWindowSize(SWT)
sbm.setPreFilterType(5)
sbm.setPreFilterSize(PFS)
sbm.setPreFilterCap(PFC)
sbm.setMinDisparity(MDS)
sbm.setNumDisparities(NOD)
sbm.setTextureThreshold(TTH)
sbm.setUniquenessRatio(UR)
sbm.setSpeckleRange(SR)
sbm.setSpeckleWarte(TXT_coordinate)
sbm.setSpeckleWindowSize(SPWS)
f.close()
# каллибровка два
try:
npzfile = np.load('./calibration_data/{}p/stereo_camera_calibration.npz'.format(img_height))
except:
print("Camera calibration data not found in cache, file ", './calibration_data/{}p/stereo_camera_calibration.npz'.format(img_height))
exit(0)
imageSize = tuple(npzfile['imageSize'])
leftMapX = npzfile['leftMapX']
leftMapY = npzfile['leftMapY']
rightMapX = npzfile['rightMapX']
rightMapY = npzfile['rightMapY']
QQ = npzfile['dispartityToDepthMap']
map_width = 320
map_height = 240
min_y = 10000
max_y = -10000
min_x = 10000
max_x = -10000
for frame in camera.capture_continuous(capture, format="bgra", use_video_port=True, resize=(img_width,img_height)):
t1 = datetime.now()
pair_img = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY)
imgLeft = pair_img [0:img_height,0:int(img_width/2)]
imgRight = pair_img [0:img_height,int(img_width/2):img_width]
imgL = cv2.remap(imgLeft, leftMapX, leftMapY, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
imgR = cv2.remap(imgRight, rightMapX, rightMapY, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
imgRcut = imgR [80:160,0:int(img_width/2)]
imgLcut = imgL [80:160,0:int(img_width/2)]
rectified_pair = (imgLcut, imgRcut)
disparity, disparity_bw, native_disparity = stereo_depth_map(rectified_pair)
maximized_line = native_disparity
maxInColumns = np.amax(maximized_line,0)
points = cv2.reprojectImageTo3D(maxInColumns, QQ)
xy_projection = np.zeros((map_height , map_width, 1), dtype=np.uint8)
if autotune_max < np.amax(maximized_line):
autotune_max = np.amax(maximized_line)
if autotune_min > np.amin(maximized_line):
autotune_min = np.amin(maximized_line)
maximized_line[0:80,] = maxInColumns
max_line_tune = (maximized_line-autotune_min)*(65535.0/(autotune_max-autotune_min))
max_line_gray = cv2.convertScaleAbs(max_line_tune, alpha=(255.0/65535.0))
map_zoom_y = int(map_height/(max_y-min_y))
map_zoom_x = int(map_height/(max_x-min_x))
for n, points in enumerate(points):
cur_y = -points[0][0]
cur_x = points[0][1]
max_y = max(cur_y, max_y)
min_y = min(cur_y, min_y)
max_x = max(cur_x, max_x)
min_x = min(cur_x, min_x)
xx = int(cur_x*map_zoom_x) + int(map_width/2) # zero point
yy = map_height - int((cur_y-min_y)*map_zoom_y) # zero point
if (xx < map_width) and (xx >= 0) and (yy < map_height) and (yy >= 0):
xy_projection[yy, xx] = max_line_gray[0,n]
print ("min_y = " + rts(min_y) + " max_y = " + rts(max_y) + " zoom_x = " + str(map_zoom_x) + " zoom_y = " + str(map_zoom_y))
xy_projection_color = cv2.applyColorMap(xy_projection, cv2.COLORMAP_JET)
max_line_color = cv2.applyColorMap(max_line_gray, cv2.COLORMAP_JET)
if (showUndistortedImages):
cv2.imshow("left", imgLcut)
cv2.imshow("right", imgRcut)
if (showColorizedDistanceLine):
cv2.imshow("Max distance line", max_line_color)
cv2.imshow("XY projection", xy_projection_color)
t2 = datetime.now()