一、YUV封装格式
格式一:YUV依次存放,每帧依次存放
格式二:。。。
二、YUV读取
以下代码可以从yuv文件中提取某一帧的YUV3个分量的对应像素值信息(YUV420格式)
适用于8bit,10bit
//头文件
import numpy as np
from functools import partial
def get_YUVdata(fdata,width,height,BitDepth,startfrm):
fp = open(fdata, 'rb')
Bd= 1 if BitDepth==8 else 2
bytes2num = partial(int.from_bytes, byteorder='little', signed=False)
framesize = height * width * 3 // 2 # 一帧8bit图像所含的像素个数
fp.seek(Bd*(framesize * startfrm), 0) #文件指针移到块起始的位置
uv_h=height//2
uv_w=width//2
if BitDepth==8:
Yt = np.zeros(shape=(1,height,width), dtype='uint8', order='C')
Ut = np.zeros(shape=(1,uv_h,uv_w), dtype='uint8', order='C')
Vt = np.zeros(shape=(1,uv_h,uv_w), dtype='uint8', order='C')
else:
Yt = np.zeros(shape=(1,height,width), dtype='uint16', order='C')
Ut = np.zeros(shape=(1,uv_h,uv_w), dtype='uint16', order='C')
Vt = np.zeros(shape=(1,uv_h,uv_w), dtype='uint16', order='C')
for m in range(height):
for n in range(width):
if BitDepth==8:
pel=bytes2num(fp.read(1))
Yt[0,m,n] = np.uint8(pel)
elif BitDepth==10:
pel=bytes2num(fp.read(2))
Yt[0,m,n] = np.uint16(pel)
for m in range(uv_h):
for n in range(uv_w):
if BitDepth==8:
pel=bytes2num(fp.read(1))
Ut[0,m,n] = np.uint8(pel)
elif BitDepth==10:
pel=bytes2num(fp.read(2))
Ut[0,m,n] = np.uint16(pel)
for m in range(uv_h):
for n in range(uv_w):
if BitDepth==8:
pel=bytes2num(fp.read(1))
Vt[0,m,n] = np.uint8(pel)
elif BitDepth==10:
pel=bytes2num(fp.read(2))
Vt[0,m,n] = np.uint16(pel)
fp.close()
return Yt,Ut,Vt