方法一:

import os
import cv2 as cv
import numpy as np
 
# 读取yuv420p的一帧文件,并转化为png图片
if __name__ == '__main__':
  filepath = 'one_frame_of_highway.yuv'
  binfile = open(filepath, 'rb')
  size = os.path.getsize(filepath)
  image_width = 352
  image_hight = 288
  image_y = [[0] * image_width for i in range(image_hight)]
  image_u = [[0] * image_width for i in range(image_hight)]
  image_v = [[0] * image_width for i in range(image_hight)]
  for r in range(image_hight):
    for c in range(image_width):
      image_y[r][c] = binfile.read(1)[0]
  Image_Y = np.array(image_y)
 
  for r in range(int(image_hight / 2)):
    for c in range(int(image_width / 2)):
      pixel = binfile.read(1)[0]
      image_u[2 * r + 0][2 * c + 0] = pixel
      image_u[2 * r + 1][2 * c + 0] = pixel
      image_u[2 * r + 0][2 * c + 1] = pixel
      image_u[2 * r + 1][2 * c + 1] = pixel
  Image_U = np.array(image_u)
 
  for r in range(int(image_hight / 2)):
    for c in range(int(image_width / 2)):
      pixel = binfile.read(1)[0]
      image_v[2 * r + 0][2 * c + 0] = pixel
      image_v[2 * r + 0][2 * c + 1] = pixel
      image_v[2 * r + 1][2 * c + 0] = pixel
      image_v[2 * r + 1][2 * c + 1] = pixel
  Image_V = np.array(image_v)
  binfile.close()
  compose = np.array([Image_Y, Image_V, Image_U]).transpose([1, 2, 0]).astype(np.uint8)
  Image = cv.cvtColor(compose, cv.COLOR_YUV2RGB)
  cv.imwrite("one_frame_of_highway.yuv.png", Image)

方法二:

ffmpeg -s 352x288 -i one_frame_of_highway.yuv one_frame_of_highway.png

posted on 2022-06-13 14:14  司徒轩宇  阅读(83)  评论(0编辑  收藏  举报