Ask Your Question
0

Problem with imshow() to get live disparity map of 2 videos

asked 2014-05-04 15:43:02 -0600

thePhi gravatar image

updated 2014-05-05 01:33:50 -0600

berak gravatar image

I'm following the tutorial here, to get a disparity map from 2 pictures:

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

imgL = cv2.imread('tsukuba_l.png',0)
imgR = cv2.imread('tsukuba_r.png',0)

stereo = cv2.createStereoBM(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgL,imgR)
plt.imshow(disparity,'gray')
plt.show()

Now, I would like to do the same thing for live video. Unfortunately, cv2.imshow is not working : No error message but only a grey window. code used :

import numpy as np
import cv2

cap_left = cv2.VideoCapture(1)
cap_left = cv2.VideoCapture(2)


# create windows
cv2.namedWindow('left_Webcam', cv2.WINDOW_NORMAL)
cv2.namedWindow('right_Webcam', cv2.WINDOW_NORMAL)
cv2.namedWindow('disparity', cv2.WINDOW_NORMAL)

while(cv2.waitKey(1) & 0xFF != ord('q')):
    ret1, frame_left = cap_left.read()
    ret2, frame_right = cap_left.read()
    # our operations on the frame come here
    gray_left = cv2.cvtColor(frame_left, cv2.COLOR_BGR2GRAY)
    gray_left = cv2.cvtColor(frame_right, cv2.COLOR_BGR2GRAY)
    cv2.imshow('left_Webcam', gray_left)
    cv2.imshow('right_Webcam', gray_right)
    stereo = cv2.createStereoBM(numDisparities=16, blockSize=15)
    disparity = stereo.compute(gray_left, gray_right)
    cv2.imshow('disparity', disparity)
# When everything done, release the capture
cap1.release()
cap2.release()
cv2.destroyAllWindows()

So how can I get a live disparity map? By the way, if I used a fixed image from my webcams, using matplotlib as in the example of the tutorial, it's working (but it's a fixed image so...)

edit retag flag offensive close merge delete

Comments

it needs cv2.waitKey(10) after cv2.imshow()

(waitKey does the window-messaging, so no image without that)

berak gravatar imageberak ( 2014-05-05 01:32:59 -0600 )edit

I found a solution by modifying the format of the file returned by my function. Since imshow() can only have input of CV_8u file, I needed to convert to a compatible format. I can't yet answer to myself, but I post a solution on this question : http://answers.opencv.org/question/22102/windows-imshow-sometimes-showing-gray-image/

thePhi gravatar imagethePhi ( 2014-05-05 05:09:20 -0600 )edit

yes, i've seen that.

"imshow() can only have input of CV_8u file" - that's not correct, it can show integer and float images as well.

you got a nice image in the other example by specifying float output from stereo.compute, and scaling that properly. (it's still a float image you're showing there)

berak gravatar imageberak ( 2014-05-05 05:18:40 -0600 )edit

Yes I corrected for the "imshow() can only show cv_8u file". Okay, I'm going on on this subject : so I' ve seen that we can do :

res = cv2.convertScaleAbs(disparity) and if you do : print(disparity.dtype) it shows : int16 and print(res.dtype) it shows : uint8. so the type of the image has changed.

thePhi gravatar imagethePhi ( 2014-05-05 06:55:59 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-05-06 22:36:31 -0600

jwatte gravatar image

First of all, you are passing the arguments in different order in the two calls to imshow().

Second, I found that my disparity map looked faint until I re-mapped it before showing. Here's the code I'm using:

disp = cv2.normalize(sgbm.compute(ri_l, ri_r), alpha=0, beta=255, \
    norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-05-04 15:43:02 -0600

Seen: 5,172 times

Last updated: May 06 '14