python(小程序系列2)yuv视频的读取(8bit、10bit)

一、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    
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值