在Python OpenCV中解码YUV图像 [英] Decoding a YUV image in Python OpenCV

查看:1805
本文介绍了在Python OpenCV中解码YUV图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个YUV420_SP_NV21图像表示为字节数组(无标头),取自Android预览框架,我需要将其解码为RGB图像. 之前,我已经在Android应用中使用Java和OpenCV4Android做到了这一点:

I have a YUV420_SP_NV21 image represented as a byte array (no headers), taken from an Android preview frame, and I need to decode it into a RGB image. I've done this in Android apps before, using Java and OpenCV4Android:

convert_mYuv = new Mat(height + height / 2, width, CvType.CV_8UC1);
convert_mYuv.put( 0, 0, data );
Imgproc.cvtColor( convert_mYuv, convert_mRgba, type, channels );

我尝试在Python中做同样的事情:

I've tried doing the same in Python:

nmp = np.array(byteArray, dtype=np.ubyte) RGBMatrix = cv2.cvtColor(nmp, cv2.COLOR_YUV420P2RGB)

nmp = np.array(byteArray, dtype=np.ubyte) RGBMatrix = cv2.cvtColor(nmp, cv2.COLOR_YUV420P2RGB)

,但RGBMatrix仍为None.

, but RGBMatrix remains None.

我对自己熟悉的公式非常了解,但我很高兴以OpenCV的方式进行此操作. 该怎么办?

I'm aware of the possibility to do this myself, I'm familiar with the formula, but would be realy happy to do this the OpenCV way. How can this be done?

我也尝试过cv2.imdecode(),但是它也失败了,可能是因为我错过了使用它的原因.

I've also tried cv2.imdecode(), but it failed too, possibly because of me miss-using it.

推荐答案

自从我解决了这个问题已经有一段时间了,但是我没有时间更新这个问题.

Its been a while since I solved this issue, but I hadn't the time to update this question.

我最终使用numpy将YUV420_SP_NV21字节数组提取为完整比例的YUV图像(在我的情况下为1280x720),然后使用OpenCV将其转换为RGB.

I ended up extracting the YUV420_SP_NV21 byte array to a full scaled YUV image (1280x720 in my case) using numpy, then converting it to RGB using OpenCV.

    import cv2 as cv2        # OpenCV import
    def YUVtoRGB(byteArray):

        e = 1280*720
        Y = byteArray[0:e]
        Y = np.reshape(Y, (720,1280))

        s = e
        V = byteArray[s::2]
        V = np.repeat(V, 2, 0)
        V = np.reshape(V, (360,1280))
        V = np.repeat(V, 2, 0)

        U = byteArray[s+1::2]
        U = np.repeat(U, 2, 0)
        U = np.reshape(U, (360,1280))
        U = np.repeat(U, 2, 0)

        RGBMatrix = (np.dstack([Y,U,V])).astype(np.uint8)
        RGBMatrix = cv2.cvtColor(RGBMatrix, cv2.COLOR_YUV2RGB, 3)

我的原始数据是YUV420_SP_NV21,其解码方式如下:YY ... YVUVUVU ... VU, 但是其他YUV格式将需要对代码进行细微更改.

My raw data was YUV420_SP_NV21, which is decode like this: YY...YVUVUVU...VU, but other YUV formats will require subtle changes to the code.

这篇关于在Python OpenCV中解码YUV图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆