17

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!

Community
  • 1
  • 1
jwdink
  • 4,824
  • 5
  • 18
  • 20
  • just for the record, the tutorial is from the future (opencv3.0), but yes, it's outdated. for current 3.0, you'd have to use cv2.StereoBM_create() or cv2.StereoSGBM_create(). – berak Jan 01 '15 at 08:17
  • cv2.StereoBM seems to exist, but using cv2.StereoBM_create() or cv2.StereoSGBM_create() gives an error (attribute error). – jwdink Jan 01 '15 at 08:44
  • again, you're obviously using opencv2.4, not 3.0 – berak Jan 01 '15 at 08:46
  • maybe play with the [stereo_matching sample](https://github.com/Itseez/opencv/blob/2.4/samples/python2/stereo_match.py), which also shows usage of StereoSGBM ? – berak Jan 01 '15 at 08:55
  • Nice find! I'll check that example out. Sorry for the version confusion. Installing OpenCV with homebrew was already a nightmare, so I dare not try and upgrade for now. – jwdink Jan 01 '15 at 21:47
  • Are you sure you have the images the right way around? From just looking at them it looks like it should be the other way around from what you have. – will Mar 08 '15 at 10:35

3 Answers3

26

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

will
  • 10,260
  • 6
  • 46
  • 69
1

It is possible that you need to keep adjusting the parameters of the block matching algorithm.

have a look at this blog article:https://erget.wordpress.com/2014/03/13/building-an-interactive-gui-with-opencv/

The article's author has composed a set of classes to make the process of calibrating the cameras more streamlined than the opencv tutorial. These classes are available as pypi package: https://github.com/erget/StereoVision

Hope this helps :)

samkhan13
  • 3,315
  • 2
  • 33
  • 54
-3

The camera is translated vertically instead of horizontally. Rotate the images 90 degrees, then try. (Prove it to yourself by rotating the screen. I just picked up my laptop and turned it on its edge.)

You mention different software; perhaps a row-major/column-major kind of thing between the original and pyOpenCV.

haruka
  • 1
  • If I'm understanding your suggestion correctly, I should try rotating the source images 90 degrees then run the same code? I just tried this-- it does not produce better results. Did you get this to work? If so, could you post the result? – jwdink Jan 01 '15 at 08:42
  • I think this is just the formatting of the question... The images are from two perspectives separated horizontally. – will Mar 08 '15 at 10:33
  • Additionally, if this were the case, then it would not matter, you would just have to put the *bottom* image as the left on (rotate the frame of reference). Rotating each of the images individually would actually stop it working. – will Mar 08 '15 at 12:01