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

Python OpenCV – Depth map from Stereo Images

Last Updated : 03 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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

 


Previous Article
Next Article

Similar Reads

Splitting stereo audio to mono with PyDub
Splitting a stereo audio file into multiple mono audio files is very useful if you are trying to process or transcribe the stereo audio files. This is because stereo audio has 2 audio sources on different channels which makes it very hard to process the file. Splitting the stereo audio file into mono audio files makes this job easier. For this arti
2 min read
Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
Addition and Blending of images using OpenCV in Python
When we talk about images, we know its all about the matrix either binary image(0, 1), gray scale image(0-255) or RGB image(255 255 255). So additions of the image is adding the numbers of two matrices. In OpenCV, we have a command cv2.add() to add the images. Below is code for Addition of two images using OpenCV : # Python program for adding # ima
2 min read
Python | Denoising of colored images using opencv
Denoising of an image refers to the process of reconstruction of a signal from noisy images. Denoising is done to remove unwanted noise from image to analyze it in better form. It refers to one of the major pre-processing steps. There are four functions in opencv which is used for denoising of different images. Syntax: cv2.fastNlMeansDenoisingColor
1 min read
Python | Create video using multiple images using OpenCV
As we know OpenCV is a widely used library for image processing. It provides a wide sense of image processing. Let's see how to create video using multiple images using OpenCV. Install the following libraries: PIL cv2 Also, check the path before running the code otherwise you will be full with errors. How it works ? Using PIL library we are opening
3 min read
How to Detect Shapes in Images in Python using OpenCV?
Prerequisites: OpenCV OpenCV is an open source library used mainly for processing images and videos to identify shapes, objects, text etc. It is mostly used with python. In this article we are going to see how to detect shapes in image. For this we need cv2.findContours() function of OpenCV, and also we are going to use cv2.drawContours() function
3 min read
How to subtract two images using Python-OpenCV ?
In this article, we are going to see how to subtract two images using OpenCV in Python. Now, before getting into the topic we shall discuss some of the use cases of Arithmetic operations. Arithmetic operations like addition and subtraction can help us to make images brighter or darker. Specifically, subtraction of two images has a lot of purposes i
3 min read
Drawing with Mouse on Images using Python-OpenCV
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we will try to draw on images with the help of the mo
3 min read
Measure similarity between images using Python-OpenCV
Prerequisites: Python OpenCVSuppose we have two data images and a test image. Let's find out which data image is more similar to the test image using python and OpenCV library in Python.Let's first load the image and find out the histogram of images.Importing library import cv2 Importing image data image = cv2.imread('test.jpg') Converting to gray
2 min read
How to Display Multiple Images in One Window using OpenCV Python?
Prerequisites: Opencv In this article, we will show how to display Multiple Images In One window using OpenCV in Python. Approach Import moduleLoad the Multiple images using cv2.imread()Concatenate the images using concatenate(), with axis value provided as per orientation requirementDisplay all the images using cv2.imshow()Wait for keyboard button
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg