简体   繁体   中英

how to write convert yuv array to rgb array?

I have original data which is yuv420p frame data bytes. I want to convert it to rgb data using numpy and scipy. 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.预期产出

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. That is the reason why you are seeing four 'little' images overlayed on top of your y component. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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