Python读取YUV文件,并显示的方法-创新互联
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)
文章标题:Python读取YUV文件,并显示的方法-创新互联
文章地址:http://tyjierui.cn/article/dcgosj.html