import time
from ctypes import *
import cv2
import numpy as np
def yuv2bgr(file_name, height, width, start_frame):
fp = open(file_name, 'rb')
fp.seek(0, 2) # 设置文件指针到文件流的尾部 + 偏移 0
fp_end = fp.tell() # 获取文件尾指针位置
frame_size = height * width * 3 // 2 # 一帧图像所含的像素个数
num_frame = fp_end // frame_size # 计算 YUV 文件包含图像数
print("This yuv file has {} frame imgs!".format(num_frame))
fp.seek(frame_size * start_frame, 0) # 设置文件指针到文件流的起始位置 + 偏移 frame_size * startframe
print("Extract imgs start frame is {}!".format(start_frame + 1))
start=time.time()
yyyy_uv = np.zeros(shape=frame_size, dtype='uint8', order='C')
for j in range(frame_size):
yyyy_uv[j] = ord(fp.read(1)) # 读取 YUV 数据,并转换为 unicode
img = yyyy_uv.reshape((height * 3 // 2, width)).astype('uint8') # NV12 的存储格式为:YYYY UV
yuv转bgr
最新推荐文章于 2024-08-05 16:52:39 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)