convert 8bit/10bit RGB444,YUV444,NV12,NV21 to PNG
生活随笔
收集整理的這篇文章主要介紹了
convert 8bit/10bit RGB444,YUV444,NV12,NV21 to PNG
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
convert 8bit/10bit RGB444,YUV444,NV12,NV21 to PNG
下面的代碼寫的粗糙(汗~),將就著看,僅做參考。。。
def yuv2bgr(filename,height,width,bit_depth,format_,output_name):path=os.path.dirname(filename)base=os.path.basename(filename)if bit_depth=="8bits":bpp=1; dtype='uint8'else:bpp=2; dtype='uint16'if format_=="nv21":cv_format=cv2.COLOR_YUV2BGR_NV21if format_ == "nv12":cv_format = cv2.COLOR_YUV2BGR_NV12with open(filename,'rb') as fp:frame_len=height*width*bpp*3//2fp.seek(0,2)ps=fp.tell()numfrm=ps//frame_lenfp.seek(0,0)for i in range(numfrm):# for i in range(1):raw = fp.read(frame_len)yuv = np.frombuffer(raw, dtype=dtype)yuv = yuv.reshape((height*3//2, width))if bpp==1:bgr_img = cv2.cvtColor(yuv, cv_format)else:yuv_tmp = yuv * 255.0 / 1023 # scale data from 10bit to 8bit: max value is 1023 for 10bit# Sn=a1*(1-q^n)/(1-q) a1:1st vaule(1); q:2 n =10yuv_uint8=yuv_tmp.astype(np.uint8)bgr_img = cv2.cvtColor(yuv_uint8, cv_format)cv2.imwrite(output_name , bgr_img)def convert_to_png(path):files=os.listdir(path)for file in files:fmts=parse_pix_fmts(file)width, height = parse_resolution(file)if fmts=="8bit_NV12":yuv2bgr(os.path.join(path,file), height, width,"8bits", "nv12",os.path.join(path,file.replace(".nv12",".png")))if fmts=="8bit_NV21":yuv2bgr(os.path.join(path,file), height, width,"8bits", "nv21",os.path.join(path,file.replace(".nv21",".png")))if fmts=="8bit_RGB444":with open(os.path.join(path,file), "rb") as fp:size_channel = height * widthimgs = np.fromfile(fp, np.dtype('u1'))b=imgs[0:size_channel];g=imgs[size_channel:size_channel*2];r=imgs[size_channel*2:]r=r.reshape(height,width);g=g.reshape(height,width);b=b.reshape(height,width);img_rgb=np.dstack([r,g,b])save_name=os.path.join(path,file).replace(".rgb",".png")cv2.imwrite(save_name,img_rgb)if fmts=="8bit_YUV444":cmd="ffmpeg.exe -y -s %dx%d -pix_fmt yuv444p -i %s %s"%(width,height,os.path.join(path,file),os.path.join(path,file.replace(".yuv",".png")))os.system(cmd)if fmts=="10bit_NV12":yuv2bgr(os.path.join(path,file), height, width,"10bits", "nv12",os.path.join(path,file.replace(".nv12",".png")))if fmts=="10bit_NV21":yuv2bgr(os.path.join(path,file), height, width,"10bits", "nv21",os.path.join(path,file.replace(".nv21",".png")))if fmts=="10bit_RGB444":with open(os.path.join(path,file), "rb") as fp:size_channel = height * widthimgs = np.fromfile(fp, np.dtype('u2'))b=imgs[0:size_channel];g=imgs[size_channel:size_channel*2];r=imgs[size_channel*2:]r=r.reshape(height,width);g=g.reshape(height,width);b=b.reshape(height,width);img_rgb=np.dstack([r,g,b])save_name=os.path.join(path,file).replace(".rgb",".png")cv2.imwrite(save_name,img_rgb)if fmts=="10bit_YUV444":cmd="ffmpeg.exe -y -s %dx%d -pix_fmt yuv444p10le -i %s %s"%(width,height,os.path.join(path,file),os.path.join(path,file.replace(".yuv",".png")))os.system(cmd)?
總結
以上是生活随笔為你收集整理的convert 8bit/10bit RGB444,YUV444,NV12,NV21 to PNG的全部內容,希望文章能夠幫你解決所遇到的問題。