Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How do I convert uint8 to CV_8UC1 in OpenCV 3.2 Python?

The code is the following:

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

cv2.namedWindow( "Left", cv2.WINDOW_AUTOSIZE );
cv2.namedWindow( "Right", cv2.WINDOW_AUTOSIZE );

cam1 = cv2.VideoCapture(1)
cam1.set(cv2.CAP_PROP_FRAME_WIDTH,384)
cam1.set(cv2.CAP_PROP_FRAME_HEIGHT,216)

cam2 = cv2.VideoCapture(2)
cam2.set(cv2.CAP_PROP_FRAME_WIDTH,384)
cam2.set(cv2.CAP_PROP_FRAME_HEIGHT,216)

stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)


while(True):

    # Capture frame-by-frame

    ret, frame1 = cam1.read()

    ret, frame2 = cam2.read()


    # Display the resulting frame
    cv2.imshow("Left",frame1)  
    cv2.imshow("Right",frame2)  

    disparity = stereo.compute(frame1,frame2) # kaput as it wants CV_8UC1 matrix
    plt.imshow(disparity,'gray')
    plt.show()

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cam1.release()
cam2.release()
cv2.destroyAllWindows()

Computing disparity is kaput as the image frame appears to be uint8 while the disparity computation wants CV_8UC1. Examples I have found to do the conversion are either in C++ or not in OpenCV 3.2 Pythonesque.

What is the conversion approach for uint8 to CV_8UC1 in OpenCV 3.2 Python please?

Cheers, A