yuv格式不同于bgr。
YUV ,指的是 YCbCr,其中Y是指亮度分量,Cb指蓝色色度分量,而Cr指红色色度分量。
根据采样方式以及排列方式分了好多种细致的格式,常用的有yuyv422等。
https://zhuanlan.zhihu.com/p/384455058
转格式之前必须只有yuyv的长和宽,以5120960举例
ffmpeg
ffmpeg -loglevel error -y -s 5120960 -pix_fmt yuyv422 -i 4366.yuv -frames:v 1 4366.jpg
opencv
def convert_yuyv422(yuv_file, yuv_shape=(1920, 720)):
h, w = yuv_shape
yuv_file = open(yuv_file, 'rb')
frame_len = h * w * 2
shape = (w, h, 2) # 用于yuyv2长度为w*h*2
raw = yuv_file.read(int(frame_len))
yuv = np.frombuffer(raw, dtype=np.uint8)
yuv = yuv.reshape(shape)
bgr = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR_YUYV)
cv2.imwrite('cv2.jpg', bgr)
下列链接有更本质的方法,但是太慢了不推荐。
https://blog.csdn.net/qq_36917144/article/details/120505174
yuv本质上没有bgr888的格式,所以这种只是把bgr888编码的存为了yuv而已
ffmpeg
ffmpeg -s 1920x720 -pix_fmt bgr24 -i 4399.yuv -frames:v 1 4399.jpg
python
def yuvbgr888_to_bgr(yuv_file, yuv_shape=(1920, 720)):
h, w = yuv_shape
yuv_file = open(yuv_file, 'rb')
frame_len = h * w * 3
shape = (w, h, 3) # 用于yuv转bgr,对于yuyv格式,需要2通道
raw = yuv_file.read(int(frame_len))
yuv = np.frombuffer(raw, dtype=np.uint8)
yuv = yuv.reshape(shape)
cv2.imwrite('cv2.jpg', yuv)
def yuvbgr888(yuv_file, yuv_shape=(1920, 720)):
img = np.fromfile(yuv_file, dtype='uint8')
h, w = yuv_shape
img = img.reshape(w, h, 3)
cv2.imwrite('cv2.jpg', img)
def yuyv422_to_bgr2(yuv_file, yuv_shape=(1920, 720)):
h, w = yuv_shape
shape = (w, h, 2) # 用于yuv转bgr,对于yuyv格式,需要2通道
yuv = np.fromfile(yuv_file, dtype='uint8')
yuv = yuv.reshape(shape)
bgr = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR_YUYV)
cv2.imwrite('cv2.jpg', bgr)