nv12, yuv420, yuv444转换关系

nv12, yuv420, yuv444转换关系

对于宽高为(w,h)的图像

  • yuv444

    yuv444的排列顺序:[y, y, y, y, …, y, u, u,u,u,…u, v,v,v,v,…v], shape:(h, w, 3).
    乘以一个3*3矩阵可以转换为RGB, opencv已经提供了这种函数:cv2.cvtColor(yuv444, cv2.COLORYUV2RGB).

  • yuv420

    排列顺序: [y, y, y, y, …, y, u, u, u, …u, v, v, v, …, v], shape:(h*3/2, w),
    其中前[:h, :]为y, [h : h * 3/2, :]为u和v,先u,再v. cvtColor转换flag:cv2.COLORYUV2RGB_I420.

  • nv12

    排列顺序:[y,y,y,y,…,y, u,v,u,v,…], shape和yuv420一样:(h*3/2, w),
    前[:h, :]为y, 剩下部分u,v间隔排列。与yuv420唯一区别是uv顺序, cv2.COLORYUV2RGB_NV12.

    nv12toyuv转换方式:

    def convert_nv12_to_yuv444_naive(width, height, nv12_data):
        yuv444 = np.zeros((height, width,  3), nv12_data.dtype)
        yuv444[:, :, 0] = nv12_data[:width * height].reshape(height, width)
        uv_start = width * height
        import math
        for j in range(height):
            for i in range(width):
                uv_idx = uv_start + (width * math.floor(j / 2)) + (math.floor(i / 2))*2
                u = nv12_data[uv_idx]
                v = nv12_data[uv_idx+1]
                yuv444[j, i , 1:] = u, v
        return yuv444
    

    yuv444不够的uv要从nv12的uv上重复采样。

参考:

http://paulbourke.net/dataformats/nv12/

你可能感兴趣的:(python)