Problem with imshow() to get live disparity map of 2 videos
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...)
it needs cv2.waitKey(10) after cv2.imshow()
(waitKey does the window-messaging, so no image without that)
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/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)
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
andprint(res.dtype)
it shows :uint8
. so the type of the image has changed.