深度估计是计算机视觉中的一个主要问题,特别是对于与增强现实、机器人甚至自动驾驶汽车相关的应用。
传统的 3D 传感器通常使用立体视觉、运动或结构光的投影。然而,这些传感器依赖于环境(阳光、纹理)或需要多个外围设备(相机、投影仪),这导致系统非常庞大。
最近,已经提出了几种基于深度学习的深度估计方法。这些方法使用单个视点(单个图像)并且通常优化参考深度图上的回归。
1.图片下载https://github.com/PacktPublishing/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition/blob/master/images/color1_small.jpg
https://github.com/PacktPublishing/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition/blob/master/images/color2_small.jpg
2.我們為立體算法的幾個參數定義初始值
import numpy as np
import cv2
minDisparity = 16
numDisparities = 192 - minDisparity
blockSize = 5
uniquenessRatio = 1
speckleWindowSize = 3
speckleRange = 3
disp12MaxDiff = 200
P1 = 600
P2 = 2400
3.使用這些參數,我們創建了 OpenCV 的 cv2.StereoSGBM 類的實例。
SGBM 代表半全局塊匹配,這是一種用於計算視差圖的算法。
stereo = cv2.StereoSGBM_create(
minDisparity = minDisparity,
numDisparities = numDisparities,
blockSize = blockSize,
uniquenessRatio = uniquenessRatio,
speckleRange = speckleRange,
speckleWindowSize = speckleWindowSize,
disp12MaxDiff = disp12MaxDiff,
P1 = P1,
P2 = P2
)
4.读取图片
imgl = cv2.imread('img/color1_small.jpeg')
imgr = cv2.imread('img/color2_small.jpeg')
imgl
5.我們希望提供幾個滑塊,使用戶能夠以交互方式調整計算視差圖的算法的參數。 每當用戶調整任何滑塊時,我們都會通過設置 StereoSGBM 實例的屬性來更新立體算法的參數,並通過調用 StereoSGBM 實例的計算方法重新計算視差圖。
def update(slidevalue =0):
stereo.setBlockSize(
cv2.getTrackbarPos('blockSize','Display')
)
stereo.setUniquenessRatio(
cv2.getTrackbarPos(
'uniquenessRatio', 'Disparity'
)
)
stereo.setSpeckleWindowSize(
cv2.getTrackbarPos('speckleWindowSize', 'Disparity')
)
stereo.setSpeckleRange(
cv2.getTrackbarPos('speckleRange', 'Disparity')
)
stereo.setDisp12MaxDiff(
cv2.getTrackbarPos('disp12MaxDiff', 'Disparity')
)
disparity = stereo.compute(
imgl, imgr).astype(np.float32) / 16.0
cv2.imshow('left',imgl)
cv2.imshow('right',imgr)
cv2.imshow('Disparity',
(disparity - minDisparity) / numDisparities)
6.创建窗口和滑块
cv2.namedWindow('Disparity')
cv2.createTrackbar('blockSize', 'Disparity', blockSize, 21,update)
cv2.createTrackbar('uniquenessRatio', 'Disparity',uniquenessRatio, 50, update)
cv2.createTrackbar('speckleWindowSize', 'Disparity',speckleWindowSize, 200, update)
cv2.createTrackbar('speckleRange', 'Disparity',speckleRange, 50, update)
cv2.createTrackbar('disp12MaxDiff', 'Disparity',
disp12MaxDiff, 250, update)
7.运行查看
update()
#wait for the user to press any key
#meanwhile update() will be called anytime the user moves a slider
cv2.waitKey()
import numpy as np
import cv2
minDisparity = 16
numDisparities = 192 - minDisparity
blockSize = 5
uniquenessRatio = 1
speckleWindowSize = 3
speckleRange = 3
disp12MaxDiff = 200
P1 = 600
P2 = 2400
stereo = cv2.StereoSGBM_create(
minDisparity = minDisparity,
numDisparities = numDisparities,
blockSize = blockSize,
uniquenessRatio = uniquenessRatio,
speckleRange = speckleRange,
speckleWindowSize = speckleWindowSize,
disp12MaxDiff = disp12MaxDiff,
P1 = P1,
P2 = P2
)
imgl = cv2.imread('img/color1_small.jpeg')
imgr = cv2.imread('img/color2_small.jpeg')
def update(slidevalue =0):
stereo.setBlockSize(
cv2.getTrackbarPos('blockSize','Display')
)
stereo.setUniquenessRatio(
cv2.getTrackbarPos(
'uniquenessRatio', 'Disparity'
)
)
stereo.setSpeckleWindowSize(
cv2.getTrackbarPos('speckleWindowSize', 'Disparity')
)
stereo.setSpeckleRange(
cv2.getTrackbarPos('speckleRange', 'Disparity')
)
stereo.setDisp12MaxDiff(
cv2.getTrackbarPos('disp12MaxDiff', 'Disparity')
)
disparity = stereo.compute(
imgl, imgr).astype(np.float32) / 16.0
cv2.imshow('left',imgl)
cv2.imshow('right',imgr)
cv2.imshow('Disparity',
(disparity - minDisparity) / numDisparities)
cv2.namedWindow('Disparity')
cv2.createTrackbar('blockSize', 'Disparity', blockSize, 21,update)
cv2.createTrackbar('uniquenessRatio', 'Disparity',uniquenessRatio, 50, update)
cv2.createTrackbar('speckleWindowSize', 'Disparity',speckleWindowSize, 200, update)
cv2.createTrackbar('speckleRange', 'Disparity',speckleRange, 50, update)
cv2.createTrackbar('disp12MaxDiff', 'Disparity',
disp12MaxDiff, 250, update)
update()
#wait for the user to press any key
#meanwhile update() will be called anytime the user moves a slider
cv2.waitKey()
《Learning OpenCV 4 Computer Vision with Python 3 - Third Edition》