Python读取YUV文件,并显示的方法
作者:TiRan_Yang 时间:2023-03-19 11:44:13
Python读取YUV格式文件,并使用opencv显示的方法
opencv可以读取的图片类型比较多,但大多是比较常见的类型,比如".jpg"和".png",但它不能直接读取YUV格式的文件,需要通过python读取YUV文件,并进行相应的转换后,才能被opencv读取,并进行后续相应的处理.
话不多说,直接上程序。
import cv2
from numpy import *
import Image
screenLevels = 255.0
def yuv_import(filename,dims,numfrm,startfrm):
fp=open(filename,'rb')
blk_size = prod(dims) *3/2
fp.seek(blk_size*startfrm,0)
Y=[]
U=[]
V=[]
print dims[0]
print dims[1]
d00=dims[0]//2
d01=dims[1]//2
print d00
print d01
Yt=zeros((dims[0],dims[1]),uint8,'C')
Ut=zeros((d00,d01),uint8,'C')
Vt=zeros((d00,d01),uint8,'C')
for i in range(numfrm):
for m in range(dims[0]):
for n in range(dims[1]):
#print m,n
Yt[m,n]=ord(fp.read(1))
for m in range(d00):
for n in range(d01):
Ut[m,n]=ord(fp.read(1))
for m in range(d00):
for n in range(d01):
Vt[m,n]=ord(fp.read(1))
Y=Y+[Yt]
U=U+[Ut]
V=V+[Vt]
fp.close()
return (Y,U,V)
if __name__ == '__main__':
width=1280
height=720
data=yuv_import('test.yuv',(height,width),1,0)
YY=data[0][0]
cv2.imshow("sohow",YY)
cv2.waitKey(0)
如果YUV文件需要转换为PIL的image格式,那么只需在main函数中,进行稍微的处理。
YY=data[0][0]
im=Image.frombytes('L',(720,1280),YY.tostring())
另外,如果PIL的image想转换为opencv的mat格式,只需将PIL转换为矩阵的形式。
im_array = np.array(im)
# 也可以用 np.asarray(im) 区别是 np.array() 是深拷贝,np.asarray() 是浅拷贝
来源:https://blog.csdn.net/lovelyaiq/article/details/76067085
标签:Python,YUV
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
DES加密解密算法之python实现版(图文并茂)
2023-08-15 01:55:11
![](https://img.aspxhome.com/file/2023/8/77758_0s.png)
z-index在IE中的迷惑
2007-05-11 16:50:00
php简单防盗链验证实现方法
2023-09-12 04:55:15
python脚本实现统计日志文件中的ip访问次数代码分享
2021-03-17 08:40:08
小白入门篇使用Python搭建点击率预估模型
2021-02-09 20:49:04
![](https://img.aspxhome.com/file/2023/6/128496_0s.png)
Numpy数组转置的两种实现方法
2023-01-22 16:36:54
兼容主流浏览器,纯CSS下拉菜单
2010-09-05 20:30:00
mysql 5.7.14 安装配置方法图文教程
2024-01-12 14:01:18
![](https://img.aspxhome.com/file/2023/7/96497_0s.jpg)
PHP小白必须要知道的php基础知识(超实用)
2024-05-13 09:56:42
![](https://img.aspxhome.com/file/2023/4/126224_0s.png)
Python中使用socket发送HTTP请求数据接收不完整问题解决方法
2021-06-29 11:43:50
Python Django框架中表单的用法详解
2021-11-06 04:24:41
![](https://img.aspxhome.com/file/2023/4/80534_0s.png)
hmac模块生成加入了密钥的消息摘要详解
2023-05-17 09:26:46
![](https://img.aspxhome.com/file/2023/3/70893_0s.png)
Python处理JSON时的值报错及编码报错的两则解决实录
2023-11-10 07:12:07
MySQL子查询详细教程
2024-01-22 07:42:04
![](https://img.aspxhome.com/file/2023/1/96511_0s.png)
Python读取图片EXIF信息类库介绍和使用实例
2023-05-27 00:49:47
python_tkinter事件类型详情
2021-11-29 10:09:30
![](https://img.aspxhome.com/file/2023/2/133662_0s.png)
MySQL下载安装、配置与使用教程详细版(win7x64)
2024-01-22 16:44:50
![](https://img.aspxhome.com/file/2023/2/88492_0s.jpg)
Python/JS实现常见加密算法的示例代码
2023-01-25 08:17:29
![](https://img.aspxhome.com/file/2023/2/97072_0s.png)
Python使用for实现无限循环的多种方式汇总
2023-04-30 15:15:11
SpringCloud Config连接git与数据库流程分析讲解
2024-01-14 01:29:29
![](https://img.aspxhome.com/file/2023/7/79907_0s.png)