Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/OpenCV: Computing a depth map from stereo images

I have two stereo images that I'd like to use to compute a depth map. While I unfortunately do not know C/C++, I do know python-- so when I found this tutorial, I was optimistic.

Unfortunately, the tutorial appears to be somewhat out of date. It not only needs to be tweaked to run at all (renaming 'createStereoBM' to 'StereoBM') but when it does run, it doesn't give a good result, even on the example stereo-images that were used in the tutorial itself.

Here's an example:

image-leftimage-right

import numpy as np
import cv2
from matplotlib import pyplot as plt

imgL = cv2.imread('Yeuna9x.png',0)
imgR = cv2.imread('SuXT483.png',0)

stereo = cv2.StereoBM(1, 16, 15)
disparity = stereo.compute(imgL, imgR)

plt.imshow(disparity,'gray')
plt.show()

The result:

the result

This looks very different from what the author of the tutorial achieves:

good result
(source: opencv.org)

Tweaking the parameters does not improve matters. All documentation I've been able to find is for the original C-version of openCV code, not the python-library-equivalent. I unfortunately haven't been able to use this to improve things.

Any help would be appreciated!

like image 787
jwdink Avatar asked Dec 31 '14 21:12

jwdink


People also ask

What is depth of image in OpenCV?

In OpenCV you typically have those types: 8UC3 : 8 bit unsigned and 3 channels => 24 bit per pixel in total. 8UC1 : 8 bit unsigned with a single channel. 32S : 32 bit integer type => int. 32F : 32 bit floating point => float.


1 Answers

You have the images the wrong way around.

Look at the images, the tin behind the lamp lets you work out the camera locations of the two images,

Just change this:

#  v
imgR = cv2.imread('Yeuna9x.png',0)
imgL = cv2.imread('SuXT483.png',0)
#  ^

If you look at the image in the tutorial which they say is the left frame, it the same as your right one.

Here's my result after the change.

enter image description here

like image 104
will Avatar answered Sep 21 '22 05:09

will