-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path深度图.py
50 lines (41 loc) · 1.85 KB
/
深度图.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
import cv2
import numpy as np
def depth_map(imgL, imgR, sigma=1.3):
""" Depth map calculation. Works with SGBM and WLS. Need rectified images, returns depth map ( left to right disparity ) """
# SGBM Parameters -----------------
# wsize default 3; 5; 7 for SGBM reduced size image; 15 for SGBM full size image (1300px and above); 5 Works nicely
window_size = 3
left_matcher = cv2.StereoSGBM_create(
minDisparity=-1,
numDisparities=5*16, # max_disp has to be dividable by 16 f. E. HH 192, 256
blockSize=window_size,
P1=8 * 3 * window_size,
# wsize default 3; 5; 7 for SGBM reduced size image; 15 for SGBM full size image (1300px and above); 5 Works nicely
P2=32 * 3 * window_size,
disp12MaxDiff=12,
uniquenessRatio=10,
speckleWindowSize=50,
speckleRange=32,
preFilterCap=63,
mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY
)
right_matcher = cv2.ximgproc.createRightMatcher(
left_matcher) # Block matching disparity map calculation
# FILTER Parameters
lmbda = 80000
visual_multiplier = 6
wls_filter = cv2.ximgproc.createDisparityWLSFilter(
matcher_left=left_matcher)
wls_filter.setLambda(lmbda)
wls_filter.setSigmaColor(sigma)
displ = left_matcher.compute(imgL, imgR) # .astype(np.float32)/16
dispr = right_matcher.compute(imgR, imgL) # .astype(np.float32)/16
displ = np.int16(displ)
dispr = np.int16(dispr)
filtered_disp = wls_filter.filter(displ, imgL, None, dispr) # 对视差图进行滤波处理
filtered_disp = cv2.normalize(
src=filtered_disp, dst=None, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX)
filtered_disp = np.uint8(filtered_disp)
# 保存深度图
cv2.imwrite('depth_map.png', filtered_disp)
return filtered_disp