The Wayback Machine - https://web.archive.org/web/20231101022049/https://www.geeksforgeeks.org/python-opencv-depth-map-from-stereo-images/
Open In App
Related Articles

Python OpenCV – Depth map from Stereo Images

Improve Article
Improve
Save Article
Save
Like Article
Like

OpenCV is the huge open-source library for the computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems.
Note: For more information, refer to Introduction to OpenCV

Depth Map : A depth map is a picture where every pixel has depth information(rather than RGB) and it normally represented as a grayscale picture. Depth information means the distance of surface of scene objects from a viewpoint. An example of pixel value depth map can be found here : Pixel Value Depth Map using Histograms

Stereo Images : Two images with slight offset. For example, take a picture of an object from the center. Move your camera to your right by 6cms while keeping the object at the center of the image. Look for the same thing in both pictures and infer depth from the difference in position. This is called stereo matching. To have best results, avoid distortions.

Approach 
 

  • Collect or take stereo images.
  • Import OpenCV and matplotlib libraries.
  • Read both left and right images.
  • Calculate disparity using stereo.compute.

Example :
Sample Images:
 

Left

 

Right

 

Python3




# import OpenCV and pyplot 
import cv2 as cv
from matplotlib import pyplot as plt
  
# read left and right images
imgR = cv.imread('right.png', 0)
imgL = cv.imread('left.png', 0)
  
# creates StereoBm object 
stereo = cv.StereoBM_create(numDisparities = 16,
                            blockSize = 15)
  
# computes disparity
disparity = stereo.compute(imgL, imgR)
  
# displays image as grayscale and plotted
plt.imshow(disparity, 'gray')
plt.show()


Output:
 

Disparity Map Output

 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials