-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputeDisparityMap.py
More file actions
34 lines (25 loc) · 1.09 KB
/
computeDisparityMap.py
File metadata and controls
34 lines (25 loc) · 1.09 KB
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
import cv2 as cv
from utils import imshow
import constants
def computeDisparityMap(imgL,imgR,frameShape,numDisparities,blockSize,interval,cutted_frame):
# Set Disparity map algotithm's parameters
stereoMatcher = cv.StereoBM_create()
stereoMatcher.setNumDisparities(numDisparities)
stereoMatcher.setBlockSize(blockSize)
# Disparity map computing
disparity = stereoMatcher.compute(imgL, imgR)
# Calculate center frame
center = frameShape
centerY = int(center[0]/2)
centerX = int(center[1]/2)
halfsize = int(interval/2)
# cut disparity to the center frame
disparity = disparity[centerY-halfsize:centerY+halfsize, centerX-halfsize:centerX+halfsize]
# main disparity computing excluding negative values where disparity could not be computed
dMain = (disparity[disparity >= 0]/16).mean()
# depth computing
z = (constants.FOCAL_LENGHT * constants.BASELINE)/dMain
# printing disparity map
if cutted_frame:
imshow("disparity",'numDisparity {}, blockSize {}'.format(numDisparities,blockSize),disparity)
return z,dMain