7.4. Depth map of stereoscopic images

Target:

In this chapter you need to learn the following:

  • We will learn how to create depth maps from stereo images.

Base

In the previous section we learned the basic concepts and related terminology of epipolar constraints. If there are two images of the same scene, we can intuitively obtain the depth information of the image. The picture below and the mathematical formula in it prove that our intuition is correct. (Image courtesy image courtesy)
image9

The above figure contains equivalent triangles. Writing the equivalent equation yields the following results:

d i s p a r i t y = x − x ′ = B f Z disparity = x - x' = \frac{Bf}{Z} disparity=xx=ZBf

x and x' are the distances from a point in the image to a point in 3D space and to the center of the camera, respectively. B is the distance between these two cameras and f is the focal length of the cameras. The above equation tells us that the depth of a point is inversely proportional to the difference between x and x'. So according to this equation we can get the depth map of all points in the image.

In this way, matching points in the two images can be found. We have seen earlier that epipolar constraints can make this operation faster and more accurate. Once a match is found, the disparity can be calculated. Let's see how to do it in OpenCV.

Code

The code snippet below shows the simple process of creating a disparity map.

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

imgL = cv.imread('tsukuba_l.png',0)
imgR = cv.imread('tsukuba_r.png',0)

stereo = cv.StereoBM_create(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgL,imgR)
plt.imshow(disparity,'gray')
plt.show()

The image below contains the original image (left) and its disparity map (right). As can be seen, the results are contaminated by a high degree of noise. By adjusting the values ​​of numDisparities and blockSize, you can get better results.

image10

Guess you like

Origin blog.csdn.net/qq_33319476/article/details/130435786