我有原始数据,即 yuv420p 帧数据字节。我想使用 numpy 和 scipy 将其转换为 rgb 数据。这是我的代码:
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')
我使用 PIL 打开这个输出 rgb 数组,但图像不是我预期的那样。
我的代码有什么问题吗??还是我的数据有问题?