def getDisparity(imgLeft, imgRight, method="BM"): gray_left = cv2.cvtColor(imgLeft, cv.CV_BGR2GRAY) gray_right = cv2.cvtColor(imgRight, cv.CV_BGR2GRAY) print gray_left.shape c, r = gray_left.shape if method == "BM": sbm = cv.CreateStereoBMState() disparity = cv.CreateMat(c, r, cv.CV_32F) sbm.SADWindowSize = 5 sbm.preFilterType = 1 sbm.preFilterSize = 5 sbm.preFilterCap = 30 sbm.minDisparity = 0 sbm.numberOfDisparities = 16 sbm.textureThreshold = 0 sbm.uniquenessRatio = 0 sbm.speckleRange = 2 sbm.speckleWindowSize = 100 gray_left = cv.fromarray(gray_left) gray_right = cv.fromarray(gray_right) cv.FindStereoCorrespondenceBM(gray_left, gray_right, disparity, sbm) disparity_visual = cv.CreateMat(c, r, cv.CV_8U) cv.Normalize(disparity, disparity_visual, 0, 255, cv.CV_MINMAX) disparity_visual = np.array(disparity_visual) elif method == "SGBM": sbm = cv2.StereoSGBM() sbm.SADWindowSize = 5 #Matched block size. It must be an odd number >=1 . Normally, it should be somewhere in the 3..11 range. sbm.numberOfDisparities = 16 #con valori piu alti tipo 112 viene il contorno nero sbm.preFilterCap = 1 sbm.minDisparity = 0 #con altri valori smongola sbm.uniquenessRatio = 7 sbm.speckleWindowSize = 100 sbm.speckleRange = 2 sbm.disp12MaxDiff = 1 sbm.fullDP = False #a True runna il full-scale two-pass dynamic programming algorithm disparity = sbm.compute(gray_left, gray_right) disparity_visual = cv2.normalize(disparity, alpha=0, beta=255, norm_type=cv2.cv.CV_MINMAX, dtype=cv2.cv.CV_8U) return disparity_visual
def getDisparity(imgLeft, imgRight, method="BM"): gray_left = cv2.cvtColor(imgLeft, cv.CV_BGR2GRAY) gray_right = cv2.cvtColor(imgRight, cv.CV_BGR2GRAY) print gray_left.shape c, r = gray_left.shape if method == "BM": sbm = cv.CreateStereoBMState() disparity = cv.CreateMat(c, r, cv.CV_32F) sbm.SADWindowSize = 9 sbm.preFilterType = 1 sbm.preFilterSize = 5 sbm.preFilterCap = 61 sbm.minDisparity = -39 sbm.numberOfDisparities = 112 sbm.textureThreshold = 507 sbm.uniquenessRatio = 0 sbm.speckleRange = 8 sbm.speckleWindowSize = 0 gray_left = cv.fromarray(gray_left) gray_right = cv.fromarray(gray_right) cv.FindStereoCorrespondenceBM(gray_left, gray_right, disparity, sbm) disparity_visual = cv.CreateMat(c, r, cv.CV_8U) cv.Normalize(disparity, disparity_visual, 0, 255, cv.CV_MINMAX) disparity_visual = np.array(disparity_visual) elif method == "SGBM": sbm = cv2.StereoSGBM() sbm.SADWindowSize = 9 sbm.numberOfDisparities = 96 sbm.preFilterCap = 63 sbm.minDisparity = -21 sbm.uniquenessRatio = 7 sbm.speckleWindowSize = 0 sbm.speckleRange = 8 sbm.disp12MaxDiff = 1 sbm.fullDP = False disparity = sbm.compute(gray_left, gray_right) disparity_visual = cv2.normalize(disparity, alpha=0, beta=255, norm_type=cv2.cv.CV_MINMAX, dtype=cv2.cv.CV_8U) return disparity_visual
def stereo_depth_map(rectified_pair): c, r = rectified_pair[0].shape disparity = cv.CreateMat(c, r, cv.CV_32F) sbm = cv.CreateStereoBMState() sbm.SADWindowSize = SWS sbm.preFilterType = 1 sbm.preFilterSize = PFS sbm.preFilterCap = PFC sbm.minDisparity = MDS sbm.numberOfDisparities = NOD sbm.textureThreshold = TTH sbm.uniquenessRatio= UR sbm.speckleRange = SR sbm.speckleWindowSize = SPWS dmLeft = cv.fromarray (rectified_pair[0]) dmRight = cv.fromarray (rectified_pair[1]) cv.FindStereoCorrespondenceBM(dmLeft, dmRight, disparity, sbm) disparity_grayscale = cv.CreateMat(c, r, cv.CV_8UC1) cv.Normalize(disparity, disparity_grayscale, 0, 255, cv2.NORM_MINMAX) disparity_grayscale = np.array(disparity_grayscale, dtype=np.uint8) #without UINT8 returns grayscale disparity_color = cv2.applyColorMap(disparity_grayscale, cv2.COLORMAP_JET) return disparity_color
def stereo_depth_map(rectified_pair): print ('SWS='+str(SWS)+' PFS='+str(PFS)+' PFC='+str(PFC)+' MDS='+\ str(MDS)+' NOD='+str(NOD)+' TTH='+str(TTH)) print(' UR=' + str(UR) + ' SR=' + str(SR) + ' SPWS=' + str(SPWS)) c, r = rectified_pair[0].shape disparity = cv.CreateMat(c, r, cv.CV_32F) sbm = cv.CreateStereoBMState() sbm.SADWindowSize = SWS sbm.preFilterType = 1 sbm.preFilterSize = PFS sbm.preFilterCap = PFC sbm.minDisparity = MDS sbm.numberOfDisparities = NOD sbm.textureThreshold = TTH sbm.uniquenessRatio = UR sbm.speckleRange = SR sbm.speckleWindowSize = SPWS dmLeft = cv.fromarray(rectified_pair[0]) dmRight = cv.fromarray(rectified_pair[1]) cv.FindStereoCorrespondenceBM(dmLeft, dmRight, disparity, sbm) disparity_visual = cv.CreateMat(c, r, cv.CV_8U) cv.Normalize(disparity, disparity_visual, 0, 255, cv.CV_MINMAX) disparity_visual = np.array(disparity_visual) return disparity_visual