简体   繁体   English

如何编写将 yuv 数组转换为 rgb 数组?

[英]how to write convert yuv array to rgb array?

I have original data which is yuv420p frame data bytes.我有原始数据,即 yuv420p 帧数据字节。 I want to convert it to rgb data using numpy and scipy.我想使用 numpy 和 scipy 将其转换为 rgb 数据。 Here is my code:这是我的代码:

    yuv = np.frombuffer(data, dtype='uint8')
    y = yuv[:1920*1080].reshape(1080, 1920)
    v = yuv[1920*1080::2].reshape(540, 960)
    u = yuv[1920*1080+1::2].reshape(540, 960)

    u = ndimage.zoom(u, 2, order=0)
    v = ndimage.zoom(v, 2, order=0)

    y = y.reshape((y.shape[0], y.shape[1], 1))
    u = u.reshape((u.shape[0], u.shape[1], 1))
    v = v.reshape((v.shape[0], v.shape[1], 1))

    yuv = np.concatenate((y, u, v), axis=2)

    yuv[:, :, 0] = yuv[:, :, 0].clip(16, 235).astype(yuv.dtype) - 16
    yuv[:, :, 1:] = yuv[:, :, 1:].clip(16, 240).astype(yuv.dtype) - 128

    A = np.array([[1.164, 0.000, 1.793],
                  [1.164, -0.213, -0.533],
                  [1.164, 2.112, 0.000]])

    rgb = np.dot(yuv, A.T).clip(0, 255).astype('uint8')

I used PIL to open this output rgb array and the image wasn't the way I expected it to be.我使用 PIL 打开这个输出 rgb 数组,图像不是我预期的那样。预期产出

Is there anying wrong with my code??我的代码有问题吗?? Or is it wrong with my data?还是我的数据有问题?

The problem is in the format of the original video.问题出在原始视频的格式上。 yuv420 means that the chroma components (u and v) have one forth of the spacial resolution od the y component. yuv420 意味着色度分量(u 和 v)具有 y 分量的空间分辨率的四分之一。 That is the reason why you are seeing four 'little' images overlayed on top of your y component.这就是为什么您会看到四个“小”图像叠加在 y 分量之上的原因。 Therefore, you have to upsample the u and v components by a factor of two, both in the horizontal and vertical directions, so that each component can match the spatial resolution of the y component.因此,您必须在水平和垂直方向上以因子 2 对 u 和 v 分量进行上采样,以便每个分量都可以匹配 y 分量的空间分辨率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM