注:本文所指的YUV均为YUV420中的I420格式(最常见的一种),其他格式不能用以下的代码。

位深为8bit时,每个像素占用1字节,对应文件指针的fp.read(1);

位深为10bit时,每个像素占用2字节,对应文件指针的fp.read(2);

然后使用 int.from_bytes() 方法将二进制转换为int型数字。

以下程序可以读8bit或10bit位深的YUV,需要指定从第几帧开始读、一共读多少帧。

它返回三个数组,其shape分别为:Y [frame,W,H]   U [frame,W/2,H/2]   V [frame,W/2,H/2]

当只读1帧时它返回:Y [W,H]   U [W/2,H/2]   V [W/2,H/2]

# -*- coding: utf-8 -*-import math
from functools import partial
import numpy as np
import matplotlib.pyplot as pltdef readyuv420(filename, bitdepth, W, H, startframe, totalframe, show=False):# 从第startframe(含)开始读(0-based),共读totalframe帧uv_H = H // 2uv_W = W // 2if bitdepth == 8:Y = np.zeros((totalframe, H, W), np.uint8)U = np.zeros((totalframe, uv_H, uv_W), np.uint8)V = np.zeros((totalframe, uv_H, uv_W), np.uint8)elif bitdepth == 10:Y = np.zeros((totalframe, H, W), np.uint16)U = np.zeros((totalframe, uv_H, uv_W), np.uint16)V = np.zeros((totalframe, uv_H, uv_W), np.uint16)plt.ion()bytes2num = partial(int.from_bytes, byteorder='little', signed=False)bytesPerPixel = math.ceil(bitdepth / 8)seekPixels = startframe * H * W * 3 // 2fp = open(filename, 'rb')fp.seek(bytesPerPixel * seekPixels)for i in range(totalframe):for m in range(H):for n in range(W):if bitdepth == 8:pel = bytes2num(fp.read(1))Y[i, m, n] = np.uint8(pel)elif bitdepth == 10:pel = bytes2num(fp.read(2))Y[i, 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))U[i, m, n] = np.uint8(pel)elif bitdepth == 10:pel = bytes2num(fp.read(2))U[i, 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))V[i, m, n] = np.uint8(pel)elif bitdepth == 10:pel = bytes2num(fp.read(2))V[i, m, n] = np.uint16(pel)if show:print(i)plt.subplot(131)plt.imshow(Y[i, :, :], cmap='gray')plt.subplot(132)plt.imshow(U[i, :, :], cmap='gray')plt.subplot(133)plt.imshow(V[i, :, :], cmap='gray')plt.show()plt.pause(1)#plt.pause(0.001)if totalframe==1:return Y[0], U[0], V[0]else:return Y,U,Vif __name__ == '__main__':#y, u, v = readyuv420(r'F:\_commondata\video\176x144 qcif\football_qcif.yuv', 8, 176, 144, 1, 5, True)y, u, v = readyuv420(r'F:\_commondata\video\1920x1080 B\RitualDance_1920x1080_60fps_10bit_420.yuv', 10, 1920, 1080, 0, 5, True)print(y.shape,u.shape,v.shape)

以下程序将YUV转为RGB(只能读8bit位深的YUV),返回1个数组,其shape为: [frame,W,H,3]

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as pltdef yuv2rgb(yuvfilename, W, H, startframe, totalframe, show=False, out=False):# 从第startframe(含)开始读(0-based),共读totalframe帧arr = np.zeros((totalframe,H,W,3), np.uint8)plt.ion()with open(yuvfilename, 'rb') as fp:seekPixels = startframe * H * W * 3 // 2fp.seek(8 * seekPixels) #跳过前startframe帧for i in range(totalframe):print(i)oneframe_I420 = np.zeros((H*3//2,W),np.uint8)for j in range(H*3//2):for k in range(W):oneframe_I420[j,k] = int.from_bytes(fp.read(1), byteorder='little', signed=False)oneframe_RGB = cv2.cvtColor(oneframe_I420,cv2.COLOR_YUV2RGB_I420)if show:plt.imshow(oneframe_RGB)plt.show()plt.pause(0.001)if out:outname = yuvfilename[:-4]+'_'+str(startframe+i)+'.png'cv2.imwrite(outname,oneframe_RGB[:,:,::-1])arr[i] = oneframe_RGBreturn arrif __name__ == '__main__':video = yuv2rgb(r'D:\_workspace\akiyo_qcif.yuv', 176, 144, 0, 10, False, True)

用ffmpeg也可以,比如你需要将yuv的第8帧输出成一个png:

ffmpeg -s 176x144 -i akiyo_qcif.yuv -filter:v select="between(n\,8\,8)" out.png

用python读取YUV文件 转RGB 8bit/10bit通用相关推荐

  1. python 16bit转8bit的工具_利用python读取YUV文件 转RGB 8bit/10bit通用

    注:本文所指的YUV均为YUV420中的I420格式(最常见的一种),其他格式不能用以下的代码. 位深为8bit时,每个像素占用1字节,对应文件指针的fp.read(1): 位深为10bit时,每个像 ...

  2. python 读取jpg文件是yuv_Python读取YUV文件,并显示的方法

    今天小编就为大家分享一篇Python读取YUV文件,并显示的方法,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 Python读取YUV格式文件,并使用opencv显示的方法 open ...

  3. 医学影像中用 python 读取 nrrd 文件、nrrd转nii、nrrd转h5

    用 python 读取 nrrd 文件一般使用 pynrrd 这个库 1 安装pynrrd pip 安装 pip install pynrrd 源码安装 pip install git+https:/ ...

  4. python 读取excel文件 效率 时间 格式_python读取Excel文件中的时间数据

    在使用python读取Excel文件中的时间格式,碰到的时间格式转换问题: 读取这样的表格: 输出这样的数据结果: 然而这样的结果却不是我们想要的,我们需要的是这样的结果: 1.安装python官方库 ...

  5. python读取一个文件夹/子文件夹下的所有文件名字

    python读取一个文件夹/子文件夹下的所有文件名字 示例代码: import osfile_path = './images/' all_file_name = os.listdir(file_pa ...

  6. python删除重复值所在的行数_使用python读取txt文件的内容,并删除重复的行数方法...

    注意,本文代码是使用在txt文档上,同时txt文档中的内容每一行代表的是图片的名字. #coding:utf-8 import shutil readDir = "原文件绝对路经" ...

  7. python读取txt文件并画图

    1,使用python读取txt文件 已知txt文件内容如下: 0 01 12 43 94 165 256 36 请以第一列为x轴,第二列为y轴画图 步骤如下: 1)使用readlines读取文件 2) ...

  8. MATLAB和Python读取wave文件的波形对比

       用python读取.wav文件的波形后再用MATLAB读取文件波形进行验证. 1.MORSE 1.1 Python 程序见之前的博客. 波形如图1所示: 图1 1.2 MATLAB 读取波形程序 ...

  9. 用Python读取整个文件

    本文翻译自:Reading entire file in Python If you read an entire file with content = open('Path/to/file', ' ...

  10. python怎么读xlsx_用python读取xlsx文件

    一 准备工作: 二 xlrd库读取 首先安装xlrd库,安装方法: pip install xlrd import xlrd #打开excel wb = xlrd.open_workbook('tes ...

最新文章

  1. 代码优化实战:我又优化了一百个if else!
  2. kettle创建mysql资源库
  3. 微信公众号关注用户的信息拉取
  4. JavaWeb14-HTML篇笔记(一)
  5. 5.3 Date类型
  6. tab页签切换----bootstrap
  7. Oracle shared_pool_reserved_size参数设置说明
  8. [2018.05.05 T2] 互质2
  9. 为了在 Windows 11 上启用 IE ,我撸了个修复工具
  10. 前端测试框架Jest系列教程 -- Matchers(匹配器)
  11. 计算机桌面壁纸希望,电脑励志的图片桌面壁纸
  12. Tera Term 工具的使用
  13. 微信小程序免300元认证费的方法,无需续费年检!
  14. apscheduler报错maximum number of running instances
  15. linux系统读移动硬盘,在linux系统上识别与挂载移动硬盘数据
  16. 23王道考研数据操作目录一览
  17. 复现I3D遇到的问题
  18. Node.js文件系统-文件操作(一)
  19. 安卓逆向013之DDMS去广告(车来了)
  20. 使用css将彩色图片转换为黑白图片

热门文章

  1. NLTK2:词性标注
  2. 存量用户时代,方兴未艾的客户服务SaaS
  3. EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT
  4. “少年派”生命里的上帝与野兽
  5. 更改ubuntu引导界面_UBuntu修改开机启动界面
  6. 2010年计算语言学分词作业——采用二元语法模型与viterbi算法分词
  7. 服务器iis建站维护,云服务器iis建站教程
  8. 计算机编码骂人,问吧骂人专用代码
  9. word分栏对齐方法
  10. python解析pdf中文乱码_Python解决中文乱码.pdf