Python/OpenCV: Computing a depth map from stereo images

jwdink picture jwdink · Dec 31, 2014 · Viewed 31.1k times · Source

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-left image-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!

Answer

will picture will · Mar 8, 2015

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