OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Depth Map from Stereo Images

Goal

In this session,

Basics

In last session, we saw basic concepts like epipolar constraints and other related terms. We also saw that 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 proves that intuition. (Image Courtesy :

stereo_depth.jpg
image

The above diagram contains equivalent triangles. Writing their equivalent equations will yield us following result:

\[disparity = x - x' = \frac{Bf}{Z}\]

\(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, 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.

Code

Below code snippet shows a simple procedure to create disparity map.

1 import numpy as np
2 import cv2
3 from matplotlib import pyplot as plt
4 
5 imgL = cv2.imread('tsukuba_l.png',0)
6 imgR = cv2.imread('tsukuba_r.png',0)
7 
8 stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
9 disparity = stereo.compute(imgL,imgR)
10 plt.imshow(disparity,'gray')
11 plt.show()

Below image contains the original image (left) and its disparity map (right). As you can see, result is contaminated with high degree of noise. By adjusting the values of numDisparities and blockSize, you can get a better result.

disparity_map.jpg
image
Note
More details to be added

Additional Resources

Exercises

  1. OpenCV samples contain an example of generating disparity map and its 3D reconstruction. Check stereo_match.py in OpenCV-Python samples.