• python opencv把yuv格式转bgr


    1、yuv格式简介

    yuv格式不同于bgr。
    YUV ,指的是 YCbCr,其中Y是指亮度分量,Cb指蓝色色度分量,而Cr指红色色度分量。
    根据采样方式以及排列方式分了好多种细致的格式,常用的有yuyv422等。
    https://zhuanlan.zhihu.com/p/384455058

    2、yuyv422转jpg

    转格式之前必须只有yuyv的长和宽,以5120960举例
    ffmpeg
    ffmpeg -loglevel error -y -s 5120
    960 -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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    下列链接有更本质的方法,但是太慢了不推荐。
    https://blog.csdn.net/qq_36917144/article/details/120505174

    3、yuv bgr888编码转jpg

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4、另一种实现

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    数据库扫描工具scuba
    23种设计模式之---单例模式
    Java测试题(核心基础)
    【多线程 - 09、线程同步 Lock】
    leecode#只出现一次数字#环形链表
    好用的第三方免费API接口汇总
    视频怎么转音频?推荐使用这几种方法
    Linux 常见问题
    jquery-picture-cut 任意文件上传 (CVE-2018-9208)
    模板 主席树查询区间k小
  • 原文地址:https://blog.csdn.net/yang_daxia/article/details/127670134