import cv2
import numpy as np
def update():
#定义compute计算视差图函数
stereo.setBlockSize(cv2.getTrackbarPos('window_size','disparity'))
stereo.setUniquenessRatio(cv2.getTrackbarPos('uniquenessRatio','disparity'))
stereo.setSpeckleWindowSize(cv2.getTrackbarPos('speckleWindowSize','disparity'))
stereo.setSpeckleRange(cv2.getTrackbarPos('speckleRange','disparity'))
stereo.setDisp12MaxDiff(cv2.getTrackbarPos('disp12MaxDiff','disparity'))
print('computing disparity...')
disp=stereo.compute(imgL, imgR).astype(np.float32)/16.0
cv2.imshow('left',imgL)
cv2.imshow('right',imgR)
cv2.imshow('disp',disp)
cv2.imshow('disp',(disp-min_disp)/num_disp)
cv2.imshow('disparity',(disp-min_disp)/num_disp)
if __name__=="__main__":
window_size=3
min_disp=16
num_disp=192-min_disp
blockSize=window_size
uniquenessRatio=1
speckleRange=12
speckleWindowSize=3
disp12MaxDiff=200
P1=600
P2=2400
imgL=cv2.imread('C:/Users/xpp/Desktop/Lena.png')#原始图像
imgR=cv2.imread('C:/Users/xpp/Desktop/Lena.png')#原始图像
imgL=cv2.resize(imgL,(800,450))
imgR=cv2.resize(imgR,(800,450))
print(imgL.shape)
print(imgR.shape)
cv2.namedWindow('disparity')
cv2.createTrackbar('speckleRange','disparity',speckleRange,50,update)
cv2.createTrackbar('window_size','disparity',window_size,10,update)
cv2.createTrackbar('speckleWindowSize','disparity',speckleWindowSize,200,update)
cv2.createTrackbar('uniquenessRatio','disparity',uniquenessRatio,50,update)
cv2.createTrackbar('disp12MaxDiff','disparity',disp12MaxDiff,250,update)
stereo=cv2.StereoSGBM_create(
minDisparity=min_disp,
numDisparities=num_disp,
blockSize=window_size,
uniquenessRatio=uniquenessRatio,
speckleRange=speckleRange,
speckleWindowSize=speckleWindowSize,
disp12MaxDiff=disp12MaxDiff,
P1=P1,
P2=P2
)
update()
cv2.waitKey(0)
cv2.destroyAllWindows()
(450, 800, 3) (450, 800, 3) computing disparity...
算法:StereoSGBM图像分割是使用极几何来计算所谓的视差图,是对图像中检测到的不同深度的基本表示,提取出一张图片中的前景部分而抛弃其余部分。
本文分享自 图像处理与模式识别研究所 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!