Depth-map creation from stereo images implemented using python

What is depth map?

depth map is an image that contains information about the distance between the surface of objects from a given viewpoint generally from the camera by which the photo has been taken. A depth map is generated from a greyscaled source images. When merged with the source images, a three-dimensional image is created. It can be seen as a depth buffer that stores the distance of the pixel form camera. In an image, there are 2D pixel array consists of pixel color data, unlike depth map which only stores depth i.e. Z-axis data.

A depth map can be easily calculated from a stereo image with OpenCV in python. There is a library function to calculate disparity between two images taking a block of pixel once. the disparity is itself is depth map as derived from the concept of parallax.

The following python program can be used to calculate disparities between two images.

import numpy as np
import cv2
#reading images from a stereo snap i.e. left and right snap seperately
#reading as a greyscaled image to calcualte disparity
#the images must be of exaclty same size i.e. dimension
left_image = cv2.imread('left.jpg',0)
right_image = cv2.imread('right.jpg',0)
#setting stereo options numDisparities=16 and blockSize=15
#numDisparities must be multiple of 16
#blockSize must be an odd number between 5 and 255 (inclusive)
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
#calculating disparities between two images namely left_image and right_image
img_disparity = stereo.compute(left_image,right_image)
#disparity as output

here left, right images and output disparities are as given below

left image
left image
right image
right image

Output: disparity as the depth map

depth map

more to be added very soon. we are working on it.

Share on Social Media
Mukuldeep Maiti

Mukuldeep Maiti

An unrecognized crazy being with random thoughts lost in unknown

You may also like...