Depth Map from stereo images : OpenCV Python
In this tutorial, we shall see how to create a depth map from stereo images using the OpenCV library in Python. But before that, let us first understand the concept of stereo images and the depth of an image.
While walking or running, we notice that objects close to us seem to move faster than those far away. We call this underlying effect ‘parallax’. We can use this phenomenon to extract geometrical information from any spectacle. From numerous images of the same arena from various points of view, we can estimate a number of things; one of them being the interspace of the components. This distance is known as the depth of the image and the images are known as stereo images. Now, by pursuing the span of points amongst these depictions, we find the stretch of these spots from the camera.
So, this stereo-matching focuses on the detection of the correlated points and recover their displacement. This is for the remodeling of the configuration of the site as a stereo map or depth map. From the map, we can apply mathematical relations involving camera configurations to measure the depth. Now, this thing has multitude of applications: it is used in self-driving cars, 3D TVs, and so on.
Enough of theory; we should dive into the coding part now. We start by importing the libraries.
Importing the necessary libraries and modules
import numpy as np import cv2 from matplotlib import pyplot as plt
Loading the stereo images
Now let us load the stereo images. You can download the images from here or from below:
imgL = cv2.imread('input_images/L1.jpg',0) imgR = cv2.imread('input_images/R1.jpg',0)
Creating the depth map
To create the depth map, we shall use the StereoBM_create() function. We can amend the parameters accordingly. numDisparities take values up to 255 and divisible by 16 while blockSize takes values up to 25.
stereo = cv2.StereoBM_create(numDisparities=96, blockSize=15) disparity = stereo.compute(imgL,imgR) plt.figure(figsize = (20,10)) plt.imshow(disparity,'disparity') plt.xticks([]) plt.yticks([])
The output is:
From the depth map, we can measure the depth. This involves camera configurations and their relations with the image disparities. This does not come under the scope of this tutorial and hence we can not proceed further.
So this concludes the tutorial on how to generate a depth map from stereo images using OpenCV in Python. Much of the work is still on you as you need to decide the best parameters for the map analytically.
How get distance from one camera to one object in the scene?
why are your images so perfect for the stereo task? My images suffer from bad rotation and the effect is shown as random separated points with black, white, gray colors. Is there any advice for image processing?