国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Python > 正文

利用python讀取YUV文件 轉RGB 8bit/10bit通用

2024-09-09 19:03:11
字體:
來源:轉載
供稿:網友

注:本文所指的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 mathfrom functools import partialimport numpy as npimport matplotlib.pyplot as plt  def readyuv420(filename, bitdepth, W, H, startframe, totalframe, show=False):  # 從第startframe(含)開始讀(0-based),共讀totalframe幀   uv_H = H // 2  uv_W = W // 2   if 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 // 2  fp = 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,V  if __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 cv2import numpy as npimport matplotlib.pyplot as plt  def 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 // 2    fp.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_RGB  return arr if __name__ == '__main__':  video = yuv2rgb(r'D:/_workspace/akiyo_qcif.yuv', 176, 144, 0, 10, False, True)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 承德县| 海宁市| 池州市| 宁国市| 南郑县| 建水县| 珲春市| 涡阳县| 木兰县| 太和县| 鹿邑县| 西丰县| 凤山市| 綦江县| 岳西县| 阳原县| 合川市| 新建县| 淮阳县| 科尔| 海原县| 常州市| 沁阳市| 渭源县| 昌宁县| 前郭尔| 凭祥市| 金沙县| 县级市| 塔城市| 南陵县| 门头沟区| 牡丹江市| 尚志市| 合山市| 昌平区| 四平市| 新绛县| 丰镇市| 榆树市| 花垣县|