yuv420编码后像素值改变_使用ffmpeg库进行YUV420到RGB的转化

本文介绍了在视频分析项目中,如何使用ffmpeg将解码后的YUV420格式转换为RGB。详细阐述了四种转换方法,包括逐像素替换、矩阵运算、OpenCV的cvtColor函数以及ffmpeg的sws_scale函数,强调了sws_scale函数的高效率。
摘要由CSDN通过智能技术生成

背景

最近使用ffmpeg库在进行一个视频分析的相关项目。因为这个行业内原始视频的编码原因(从历史到硬件到存储空间,各种原因),使用ffmpeg在decode H264的视频的时候,一般都会decode为YUV420格式的raw data。这就需要进一步转换到RGB颜色空间,方便各种python的库或者是神经网络框架做分析。

YUV420格式的raw data在哪里呢?

经过ffmpeg相关的API进行解码后,YUV420的data就存放在AVFrame结构体里的data成员中,data成员是个指针数组,一共有8个元素(也就是8个指针),其中,每个指针指向的都是一个unsigned char* 的buffer。

当decode为YUV420格式的时候,data只有前3个unsigned char* 指针被使用了,分别存放的是Y、U、V的raw data,其中U、V的raw data长度是Y的四分之一;

当decode为RGB或者BGR的时候,data只有第一个unsigned char* 指针被使用了,存放的就是RGB或者BGR的raw data。

转换方式一:逐像素替换

因为YUV和RGB的相关性是可以通过简单的线性公式计算出来的(很容易搜索到相关的介绍)。相关的代码如下所示:

int buffer_len = pFrame->width * pFrame->height;
img_width = pFrame->width;
img_height = pFrame->height;
buffer = malloc(sizeof(uint8_t)* buffer_len * 3);
int channels = 3;
int index = 0;
for (int y = 0; y < img_height; y++){
    for (int x = 0; x < img_width; x++){
        int indexY = y * img_width + x;
        int indexU = y / 2 * img_width / 2 + x / 2;
        int indexV = y / 2 * img_width / 2 + x / 2;
        uint8_t Y = pFrame->data[0][indexY];
        uint8_t U = pFrame->data[1][indexU];
        uint8_t V = pFrame->data[2][indexV];
        
        int R = Y + 1.402 * (V - 128);
        int G = Y - 0.34413 * (U - 128) - 0.71414*(V - 128);
        int B = Y + 1.772*(U - 128);
        R = (R < 0) ? 0 : R;
        G = (G < 0) ? 0 : G;
        B = (B < 0) ? 0 : B;
        R = (R > 255) ? 255 : R;
        G = (G > 255) ? 255 : G;
        B = (B > 255) ? 255 : B;
        buffer[(y*img_width + x)*channels + 2] = (uint8_t)R;
        buffer[(y*img_width + x)*channels + 1] = (uint8_t)G;
        buffer[(y*img_width + x)*channels + 0] = (uint8_t)B;
    }
}

这种方式优点是看起来简单,缺点是慢!想想看,1080p的图片还不得循环百万次以上!还有,鲁棒性差!

转换方式二:使用矩阵

这种方式使用矩阵运算来加快运行速度,这种方式速度可以快点,但鲁棒性和上面的一样:

def ConvertYUVtoRGB(yuv_planes)
    plane_y  = yuv_planes[0]
    plane_u  = yuv_planes[1]
    plane_v  = yuv_planes[2]
     
    height = plane_y.shape[0]
    width  = plane_y.shape[1]
     
    # upsample if YV12
    plane_u = ndimage.zoom(plane_u, 2, order=0)
    plane_v = ndimage.zoom(plane_v, 2, order=0)
    # alternativelly can perform upsampling with numpy.repeat()
     
    # reshape
    plane_y  = plane_y.reshape((plane_y.shape[0], plane_y.shape[1], 1))
    plane_u  = plane_u.reshape((plane_u.shape[0], plane_u.shape[1], 1))
    plane_v  = plane_v.reshape((plane_v.shape[0], plane_v.shape[1], 1))
     
    # make YUV of shape [height, width, color_plane]
    yuv = np.concatenate((plane_y, plane_u, plane_v), axis=2)
     
    # according to ITU-R BT.709
    yuv[:,:, 0] = yuv[:,:, 0].clip(16, 235).astype(yuv.dtype) - 16
    yuv[:,:,1:] = yuv[:,:,1:].clip(16, 240).astype(yuv.dtype) - 128
     
    A = np.array([[1.164,  0.000,  1.793],
                  [1.164, -0.213, -0.533],
                  [1.164,  2.112,  0.000]])
     
    # our result
    rgb = np.dot(yuv, A.T).clip(0, 255).astype('uint8')
     
    return rgb

转换方式三:使用OpenCV

使用opencv的cvtColor函数进行颜色空间的转换:

cv2.cvtColor(crs,dst, cv2.COLOR_YUV2BGR)

转换方式四:ffmpeg自身的sws_scale

使用sws_scale函数进行转换的效率是极高的。

#include <libswscale/swscale.h>
SwsContext* swsContext = swsContext = sws_getContext(pFrame->width, pFrame->height, AV_PIX_FMT_YUV420P,pFrame->width, pFrame->height, AV_PIX_FMT_BGR24,
                          NULL, NULL, NULL, NULL);

int linesize[8] = {pFrame->linesize[0] * 3};
int num_bytes = av_image_get_buffer_size(AV_PIX_FMT_BGR24, pFrame->width, pFrame->height, 1);
p_global_bgr_buffer = (uint8_t*) malloc(num_bytes * sizeof(uint8_t));
uint8_t* bgr_buffer[8] = {p_global_bgr_buffer};

sws_scale(swsContext, pFrame->data, pFrame->linesize, 0, pFrame->height, bgr_buffer, linesize);
//bgr_buffer[0] is the BGR raw data

庆祝

有了RGB或者BGR颜色空间的raw data后,我们就可以为所欲为了。可以送给CV的Mat结构体,可以使用numpy进行构造,也可以啥都不干。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值