If we have two images of same scene, we can get depth information from that in an intuitive way.
Below is an image and some simple mathematical formulas which prove that intuition.
The above diagram contains equivalent triangles. Writing their equivalent equations will yield us following result:
x and x′ are the distance between points in image plane corresponding to the scene point 3D and their camera center.
B is the distance between two cameras (which we know) and f is the focal length of camera (already known).
So in short, the above equation says that the depth of a point in a scene is inversely proportional to the difference in distance of corresponding image points and their camera centers.
So with this information, we can derive the depth of all pixels in an image.
So it finds corresponding matches between two images. We have already seen how epiline constraint make this operation faster and accurate. Once it finds matches, it finds the disparity. Let's see how we can do it with OpenCV.
####################################################################################################
# 立体图像深度图(Depth Map from Stereo Images)
def lmc_cv_depth_map():
"""
函数功能: 立体图像深度图(Depth Map from Stereo Images).
"""
image1 = lmc_cv.imread('D:/99-Research/TestData/cv/stereo/uncalib/board_l.png',
lmc_cv.IMREAD_GRAYSCALE)
image2 = lmc_cv.imread('D:/99-Research/TestData/cv/stereo/uncalib/board_r.png',
lmc_cv.IMREAD_GRAYSCALE)
# create a disparity map
# numDisparities: 最大视差值与最小视差值之差, 窗口大小必须是16的整数倍, int 型.
# blockSize: 匹配的块大小. 它必须是 >=1的奇数. 通常情况下, 它应该在5-11的范围内. 这里设置为大于11也可以, 但必须为奇数.
stereo = lmc_cv.StereoBM_create(numDisparities=16, blockSize=17)
disparity = stereo.compute(image1, image2)
stacking_images = []
# stacking images side-by-side
stacking_image = np.hstack((image1, image2, disparity))
stacking_images.append(stacking_image)
# 显示图像
for i in range(len(stacking_images)):
pyplot.figure('Depth Map from Stereo Images %d' % (i + 1), figsize=(16, 9))
# pyplot.subplot(1, 1, 1)
pyplot.imshow(stacking_images[i], 'gray')
pyplot.title('Depth Map from Stereo Images')
pyplot.xticks([])
pyplot.yticks([])
pyplot.savefig('%02d.png' % (i + 1))
pyplot.show()
Below image contains the original image (left) and its disparity map (right). As you can see, the result is contaminated with high degree of noise.
By adjusting the values of numDisparities and blockSize, you can get a better result.
There are some parameters when you get familiar with StereoBM, and you may need to fine tune the parameters to get better and smooth results.
Parameters: