Python3 OpenCV将原始YUYV422从WebCam转换为RGB

时间:2018-02-28 19:08:17

标签: python python-3.x opencv v4l2 opencv-python

我正在尝试将尺寸为640x480的RAW YUYY422帧转换为Python3 OpenCV中的RGB。 我已经查看过OpenCV文档和谷歌各种解决方案,但似乎无法让它工作。

我不断得到的错误是:

的变体
Before 614400 <class 'bytes'>
After 614400 <class 'numpy.ndarray'>
(614400,)
Traceback (most recent call last):
File "./capture.py", line 111, in <module>
cv2.cvtColor(dataBuffer, dataBuffer, cv2.COLOR_YUV2RGB_UYVY);
TypeError: only size-1 arrays can be converted to Python scalars

以下是我尝试转换的代码:

    buf = v4l2_buffer()
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
    buf.memory = V4L2_MEMORY_MMAP
    fcntl.ioctl(vd, VIDIOC_DQBUF, buf)  # get image from the driver queue
    mm = buffers[buf.index]

    dataBuffer = mm.read()
    print("Before",len(dataBuffer),type(dataBuffer))
    dataBuffer = np.fromstring(dataBuffer, np.uint8)
    print("After",len(dataBuffer),type(dataBuffer))
    #dataBuffer = dataBuffer.reshape( 640, 480 )
    print(dataBuffer.shape)

    cv2.cvtColor(dataBuffer, dataBuffer, cv2.COLOR_YUV2RGB_UYVY);

    dbusClient.run(dataBuffer)
    mm.seek(0)
    fcntl.ioctl(vd, VIDIOC_QBUF, buf)  # requeue the buffer

如何完成此转化?感谢。

更新:每条评论:

    for _ in range(50):  # capture 50 frames
    buf = v4l2_buffer()
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
    buf.memory = V4L2_MEMORY_MMAP
    fcntl.ioctl(vd, VIDIOC_DQBUF, buf)  # get image from the driver queue
    mm = buffers[buf.index]

    dataBuffer = mm.read()
    dataBuffer = np.fromstring(dataBuffer, dtype=np.uint8)
    dataBuffer2 = cv2.cvtColor(dataBuffer, cv2.COLOR_BGR2YUV)

    dbusClient.run(dataBuffer)
    mm.seek(0)
    fcntl.ioctl(vd, VIDIOC_QBUF, buf)  # requeue the buffer

给出错误:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /io/opencv/modules/imgproc/src/color.cpp, line 11144
Traceback (most recent call last):
File "./capture.py", line 106, in <module>
dataBuffer2 = cv2.cvtColor(dataBuffer, cv2.COLOR_BGR2YUV)
cv2.error: /io/opencv/modules/imgproc/src/color.cpp:11144: error: (-215) scn == 3 || scn == 4 in function cvtColor

0 个答案:

没有答案