Python 读取 YUV(NV12) 视频文件实例

yipeiwu_com7年前Python基础

一、YUV 简介

YUV:是一种颜色编码方法,常使用在各个视频处理组件中

Y'UV, YCbCr, YPbPr等专有名词都可以称为 YUV,彼此有重叠

Y表示明亮度(单取此通道即可得灰度图),U和V则是色度、浓度

主流的采样方式有三种,YUV4:4:4,YUV4:2:2,YUV4:2:0

可以根据其采样格式来从码流中还原每个像素点的 YUV 值,进而通过 YUV 与 RGB 的转换公式提取出每个像素点的 RGB 值,然后显示出来

YUV4:2:0 数据在内存中的长度是 3 / 2 * heigth * width,是 RGB24(heigth * width * 3) 格式视频数据内存的一半

二、YUV420(NV12、NV21、I420、YV12)

# NV12、NV21 的存储格式为 Y 平面,UV 打包,即:Y 信息存储在一个数组中,UV 信息存储在一个矩阵中。
# 不同点在于 UV 的排列顺序
NV12: YYYYYYYY UVUV  => YUV420SP
NV21: YYYYYYYY VUVU  => YUV420SP

# I420、YV12 三个分量均为平面格式,即:分别存放在三个 Byte 型数组中
I420: YYYYYYYY UU VV => YUV420P
YV12: YYYYYYYY VV UU => YUV420P

假设一个分辨率为8X4的 YUV 图像,它们的格式如下图:

三、读取 YUV(NV12) 视频文件并保存

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np


def yuv2bgr(filename, height, width, startfrm):
 """
 :param filename: 待处理 YUV 视频的名字
 :param height: YUV 视频中图像的高
 :param width: YUV 视频中图像的宽
 :param startfrm: 起始帧
 :return: None
 """
 fp = open(filename, 'rb')

 framesize = height * width * 3 // 2 # 一帧图像所含的像素个数
 h_h = height // 2
 h_w = width // 2

 fp.seek(0, 2) # 设置文件指针到文件流的尾部
 ps = fp.tell() # 当前文件指针位置
 numfrm = ps // framesize # 计算输出帧数
 fp.seek(framesize * startfrm, 0)

 for i in range(numfrm - startfrm):
  Yt = np.zeros(shape=(height, width), dtype='uint8', order='C')
  Ut = np.zeros(shape=(h_h, h_w), dtype='uint8', order='C')
  Vt = np.zeros(shape=(h_h, h_w), dtype='uint8', order='C')

  for m in range(height):
   for n in range(width):
    Yt[m, n] = ord(fp.read(1))
  for m in range(h_h):
   for n in range(h_w):
    Ut[m, n] = ord(fp.read(1))
  for m in range(h_h):
   for n in range(h_w):
    Vt[m, n] = ord(fp.read(1))

  img = np.concatenate((Yt.reshape(-1), Ut.reshape(-1), Vt.reshape(-1)))
  img = img.reshape((height * 3 // 2, width)).astype('uint8') # YUV 的存储格式为:NV12(YYYY UV)

  # 由于 opencv 不能直接读取 YUV 格式的文件, 所以要转换一下格式
  bgr_img = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12) # 注意 YUV 的存储格式
  cv2.imwrite('yuv2bgr/%d.jpg' % (i + 1), bgr_img)
  print("Extract frame %d " % (i + 1))

 fp.close()
 print("job done!")
 return None


if __name__ == '__main__':
 _ = yuv2bgr(filename='xxx.yuv', height=1080, width=1920, startfrm=0)

以上这篇Python 读取 YUV(NV12) 视频文件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python之文字转图片方法

Python之文字转图片方法

Pygame模块一览表: 引入pygame模块 ,若本机没有请自行pip install pygame #载入必要的模块 import pygame #pygame初始化 pyga...

如何运行Python程序的方法

如何运行Python程序的方法

安装完python之后,我们可以做两件事情, 1.将安装目录中的Doc目录下的python331.chm使用手册复制到桌面上,方便学习和查阅 2.将Python安装路径我的是C:\Pyt...

pytorch + visdom CNN处理自建图片数据集的方法

pytorch + visdom CNN处理自建图片数据集的方法

环境 系统:win10 cpu:i7-6700HQ gpu:gtx965m python : 3.6 pytorch :0.3 数据下载 来源自Sasank Chilamkurthy 的...

python实现通过flask和前端进行数据收发

python代码: # -*- coding: utf-8 -*- from flask import Flask,jsonify,render_template,request i...

python目录操作之python遍历文件夹后将结果存储为xml

Linux服务器有CentOS、Fedora等,都预先安装了Python,版本从2.4到2.5不等,而Windows类型的服务器也多数安装了Python,因此只要在本机写好一个脚本,上传...