0

I cannot fetch a correct disparity map from a couple simple images, as shown below:

LEFT

LEFT

RIGHT

RIGHT

Disparity

disparity

The codes:

import cv2
import numpy as np

# frames buffer
frames = []
# image categories
cat_selected = 0
cat_list     = ['open']
cat_calib    = [np.load('LUMIA_CALIB.npy')]

# load images
def im_load(image, calib):
    frame = cv2.imread(image,0)
    if calib is not None:
        frame = cv2.undistort(cv2.resize(frame, (640, 480)), *calib[0])
        x, y, w, h = calib[1]
        frame = frame[y : y + h, x : x + w]
    return frame

for idx, im in enumerate(['left', 'right']):
    frames.append(im_load('images/%s/%s.jpg' %(cat_list[cat_selected], im), cat_calib[cat_selected]))
    cv2.namedWindow(im, cv2.cv.CV_WINDOW_NORMAL)
    cv2.imshow(im, frames[idx])
    cv2.imwrite('%s.jpg' %im, frames[idx])

stereo = cv2.StereoBM(1, 16, 15)
disparity = stereo.compute(frames[0], frames[1])
cv2.namedWindow('map', 0)
cv2.imshow('map', cv2.convertScaleAbs(disparity))
cv2.imwrite('disparity.jpg', disparity)

cv2.waitKey(0)
cv2.destroyAllWindows()

Questions

  1. What is wrong with the code and how can I fix it?
  2. What are the effects of the distance between cameras while computing depth?
  3. What is the unit of the members of the disparity matrix's values?

P.S

  1. The codes computes the disparity map for Tsukuba set of images, alright though.
  2. I don't know if this is relevant or not but the distance between two cameras is 14.85 cm.
Community
  • 1
  • 1
dariush
  • 3,191
  • 3
  • 24
  • 43

1 Answers1

0

Question 1

You seem to have forgotten to rectify your images from a stereo calibration procedure.

Question 2

The distance between two cameras is called Baseline. In general, the bigger the baseline, the better the accuracy at a distance is, with a sacrifice on field of view. The opposite is true too.

Question 3

The disparity is probably in pixel if you're using Python (I'm no expert in the python API of OpenCV). In general, if you want the distance values from the triangulation (in real world unit), you'll want to use reprojectImageTo3D or its equivalent in Python. You will need to first calibrate your stereo rig using a chessboard (and knowing the size of the squares).

Cedric
  • 889
  • 1
  • 8
  • 18
  • Would you please review [this](http://stackoverflow.com/questions/38635283/how-can-i-compute-the-depth-from-stereo-camera) question? -- is related to current one! – dariush Jul 28 '16 at 12:13
  • Hi! I can't seem to find your question... was it deleted? – Cedric Jul 28 '16 at 13:25
  • Sorry, I haven't thought the posted question through back then, so I have deleted it, a [new question](http://stackoverflow.com/questions/38653354/straightforward-solution-on-how-to-stereo-calibration-and-rectifications-opencv) have been asked. – dariush Jul 29 '16 at 07:54