图像处理之CSC性能优化(源码)

1 CSC基本实现

       根据前一篇CSC转换的文档了解到,RGB与YUV的变换公式如下:

   

        YCbCr(256 级别) 可以从8位 RGB 直接计算,计算公式如下:

        Y = 0.299 R + 0.587 G + 0.114 B

        Cb = - 0.1687 R - 0.3313 G + 0.5 B + 128

        Cr = 0.5 R - 0.4187 G - 0.0813 B + 128

        反过来,RGB 也可以直接从YUV (256级别) 计算:

        R = Y + 1.402 (Cr-128)

        G = Y - 0.34414 (Cb-128) - 0.71414 (Cr-128)

        B = Y + 1.772 (Cb-128)

        YUV格式比较多,下面以YV12格式为例,说明YV12格式转化成RGB24格式的方法。其基本思路:按照RGB与YUV的变换公式进行逐像素的计算,但具体实现过程中,优化方法和技巧影响最终的转换效率。说明:为了方便查看转换后的结果,在实现过程中,是BGR24格式代替RGB24格式,其转换过程是不变。具体实现如下:

 1 bool YV12ToBGR24_Native(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
 2 {
 3     if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
 4         return false;
 5     const long len = width * height;
 6     unsigned char* yData = pYUV;
 7     unsigned char* vData = &yData[len];
 8     unsigned char* uData = &vData[len >> 2];
 9 
10     int bgr[3];
11     int yIdx,uIdx,vIdx,idx;
12     for (int i = 0;i < height;i++){
13         for (int j = 0;j < width;j++){
14             yIdx = i * width + j;
15             vIdx = (i/2) * (width/2) + (j/2);
16             uIdx = vIdx;
17         
18             bgr[0] = (int)(yData[yIdx] + 1.732446 * (uData[vIdx] - 128));                                    // b分量
19             bgr[1] = (int)(yData[yIdx] - 0.698001 * (uData[uIdx] - 128) - 0.703125 * (vData[vIdx] - 128));    // g分量
20             bgr[2] = (int)(yData[yIdx] + 1.370705 * (vData[uIdx] - 128));                                    // r分量
21 
22             for (int k = 0;k < 3;k++){
23                 idx = (i * width + j) * 3 + k;
24                 if(bgr[k] >= 0 && bgr[k] <= 255)
25                     pBGR24[idx] = bgr[k];
26                 else
27                     pBGR24[idx] = (bgr[k] < 0)?0:255;
28             }
29         }
30     }
31     return true;
32 }

2 基于查表法优化

       逐一访问像素,进行浮点运算,比较耗时,因而利用空间换时间思路,以查找表来替代转换过程中的一些计算。具体实现如下:

 1 static int Table_fv1[256] = { -180, -179, -177, -176, -174, -173, -172, -170, -169, -167, -166, -165, -163, -162, -160, -159, -158, -156, -155, -153, -152, -151, -149, -148, -146, -145, -144, -142, -141, -139, -138, -137,  -135, -134, -132, -131, -130, -128, -127, -125, -124, -123, -121, -120, -118, -117, -115, -114, -113, -111, -110, -108, -107, -106, -104, -103, -101, -100, -99, -97, -96, -94, -93, -92, -90,  -89, -87, -86, -85, -83, -82, -80, -79, -78, -76, -75, -73, -72, -71, -69, -68, -66, -65, -64,-62, -61, -59, -58, -57, -55, -54, -52, -51, -50, -48, -47, -45, -44, -43, -41, -40, -38, -37,  -36, -34, -33, -31, -30, -29, -27, -26, -24, -23, -22, -20, -19, -17, -16, -15, -13, -12, -10, -9, -8, -6, -5, -3, -2, 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 30, 32, 33, 35, 36, 37, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 72, 74, 75, 77, 78, 79, 81, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 124, 126, 127, 129, 130, 131, 133, 134, 136, 137, 138, 140, 141, 143, 144, 145, 147, 148,  150, 151, 152, 154, 155, 157, 158, 159, 161, 162, 164, 165, 166, 168, 169, 171, 172, 173, 175, 176, 178 };
 2 static int Table_fv2[256] = { -92, -91, -91, -90, -89, -88, -88, -87, -86, -86, -85, -84, -83, -83, -82, -81, -81, -80, -79, -78, -78, -77, -76, -76, -75, -74, -73, -73, -72, -71, -71, -70, -69, -68, -68, -67, -66, -66, -65, -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, -54, -53, -53, -52, -51, -51, -50, -49, -48, -48, -47, -46, -46, -45, -44, -43, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35, -34, -33, -33, -32, -31, -31, -30, -29, -28, -28, -27, -26, -26, -25, -24, -23, -23, -22, -21, -21, -20, -19, -18, -18, -17, -16, -16, -15, -14, -13, -13, -12, -11, -11, -10, -9, -8, -8, -7, -6, -6, -5, -4, -3, -3, -2, -1, 0, 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 42, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50, 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62, 62, 63, 64, 65, 65, 66, 67, 67, 68, 69, 70, 70, 71, 72, 72, 73, 74, 75, 75, 76, 77, 77, 78, 79, 80, 80, 81, 82, 82, 83, 84, 85, 85, 86, 87, 87, 88, 89, 90, 90 };
 3 static int Table_fu1[256] = { -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39, -38, -38, -38, -37, -37, -37, -36, -36, -36, -35, -35, -35, -34, -34, -33, -33, -33, -32, -32, -32, -31, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26, -26, -25, -25, -25, -24, -24, -24, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19, -19, -18, -18, -18, -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -11, -11, -11, -10, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -6, -5, -5, -5, -4, -4, -4, -3, -3, -3, -2, -2, -2, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 43, 43 };
 4 static int Table_fu2[256] = { -227, -226, -224, -222, -220, -219, -217, -215, -213, -212, -210, -208, -206, -204, -203, -201, -199, -197, -196, -194, -192, -190, -188, -187, -185, -183, -181, -180, -178, -176, -174, -173, -171, -169, -167, -165, -164, -162, -160, -158, -157, -155, -153, -151, -149, -148, -146, -144, -142, -141, -139, -137, -135, -134, -132, -130, -128, -126, -125, -123, -121, -119, -118, -116, -114, -112, -110, -109, -107, -105, -103, -102, -100, -98, -96, -94, -93, -91, -89, -87, -86, -84, -82, -80, -79, -77, -75, -73, -71, -70, -68, -66, -64, -63, -61, -59, -57, -55, -54, -52, -50, -48, -47, -45, -43, -41, -40, -38, -36, -34, -32, -31, -29, -27, -25, -24, -22, -20, -18, -16, -15, -13, -11, -9, -8, -6, -4, -2, 0, 1, 3, 5, 7, 8, 10, 12, 14, 15, 17, 19, 21, 23, 24, 26, 28, 30, 31, 33, 35, 37, 39, 40, 42, 44, 46, 47, 49, 51, 53, 54, 56, 58, 60, 62, 63, 65, 67, 69, 70, 72, 74, 76, 78, 79, 81, 83, 85, 86, 88, 90, 92, 93, 95, 97, 99, 101, 102, 104, 106, 108, 109, 111, 113, 115, 117, 118, 120, 122, 124, 125, 127, 129, 131, 133, 134, 136, 138, 140, 141, 143, 145, 147, 148, 150, 152, 154, 156, 157, 159, 161, 163, 164, 166, 168, 170, 172, 173, 175, 177, 179, 180, 182, 184, 186, 187, 189, 191, 193, 195, 196, 198, 200, 202, 203, 205, 207, 209, 211, 212, 214, 216, 218, 219, 221, 223, 225 };
 5 
 6 bool YV12ToBGR24_Table(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
 7 {
 8     if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
 9         return false;
10     const long len = width * height;
11     unsigned char* yData = pYUV;
12     unsigned char* vData = &yData[len];
13     unsigned char* uData = &vData[len >> 2];
14 
15     int bgr[3];
16     int yIdx,uIdx,vIdx,idx;
17     int rdif,invgdif,bdif;
18     for (int i = 0;i < height;i++){
19         for (int j = 0;j < width;j++){
20             yIdx = i * width + j;
21             vIdx = (i/2) * (width/2) + (j/2);
22             uIdx = vIdx;
23             
24             rdif = Table_fv1[vData[vIdx]];
25             invgdif = Table_fu1[uData[uIdx]] + Table_fv2[vData[vIdx]];
26             bdif = Table_fu2[uData[uIdx]];
27 
28             bgr[0] = yData[yIdx] + bdif;    
29             bgr[1] = yData[yIdx] - invgdif;
30             bgr[2] = yData[yIdx] + rdif;
31 
32             for (int k = 0;k < 3;k++){
33                 idx = (i * width + j) * 3 + k;
34                 if(bgr[k] >= 0 && bgr[k] <= 255)
35                     pBGR24[idx] = bgr[k];
36                 else
37                     pBGR24[idx] = (bgr[k] < 0)?0:255;
38             }
39         }
40     }
41     return true;
42 }

3 基于opencv优化

       利用OpenCV提供的转换函数实现YUV到RGB的转换,实现简单方便。实现过程,只需要合理构造包含YUV数据的Mat,具体实现方法如下:

1 bool YV12ToBGR24_OpenCV(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
2 {
3     if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
4         return false;
5     Mat dst(height,width,CV_8UC3,pBGR24);
6     Mat src(height + height/2,width,CV_8UC1,pYUV);
7     cvtColor(src,dst,CV_YUV2BGR_YV12);
8     return true;
9 }

4 基于FFmpeg优化

       利用FFmpeg中swscale实现YUV到RGB的转换,实现过程中,需要构造AVPicture结构,具体实现方法如下:

 1 bool YV12ToBGR24_FFmpeg(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
 2 {
 3     if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
 4         return false;
 5     //int srcNumBytes,dstNumBytes;
 6     //uint8_t *pSrc,*pDst;
 7     AVPicture pFrameYUV,pFrameBGR;
 8     
 9     //pFrameYUV = avpicture_alloc();
10     //srcNumBytes = avpicture_get_size(PIX_FMT_YUV420P,width,height);
11     //pSrc = (uint8_t *)malloc(sizeof(uint8_t) * srcNumBytes);
12     avpicture_fill(&pFrameYUV,pYUV,PIX_FMT_YUV420P,width,height);
13 
14     //U,V互换
15     uint8_t * ptmp=pFrameYUV.data[1];
16     pFrameYUV.data[1]=pFrameYUV.data[2];
17     pFrameYUV.data [2]=ptmp;
18 
19     //pFrameBGR = avcodec_alloc_frame();
20     //dstNumBytes = avpicture_get_size(PIX_FMT_BGR24,width,height);
21     //pDst = (uint8_t *)malloc(sizeof(uint8_t) * dstNumBytes);
22     avpicture_fill(&pFrameBGR,pBGR24,PIX_FMT_BGR24,width,height);
23 
24     struct SwsContext* imgCtx = NULL;
25     imgCtx = sws_getContext(width,height,PIX_FMT_YUV420P,width,height,PIX_FMT_BGR24,SWS_BILINEAR,0,0,0);
26 
27     if (imgCtx != NULL){
28         sws_scale(imgCtx,pFrameYUV.data,pFrameYUV.linesize,0,height,pFrameBGR.data,pFrameBGR.linesize);
29         if(imgCtx){
30             sws_freeContext(imgCtx);
31             imgCtx = NULL;
32         }
33         return true;
34     }
35     else{
36         sws_freeContext(imgCtx);
37         imgCtx = NULL;
38         return false;
39     }
40 }

5 基于Pinknoise实现

       下载上述网站提供的yuv2rgb代码,利用yuv420_2_rgb888函数即可实现YUV到RGB的转换

 1 bool YV12ToBGR24_Pinknoise(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
 2 {
 3     if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
 4         return false;
 5     unsigned char *yData = pYUV;
 6     unsigned char *vData = &pYUV[width * height];
 7     unsigned char *uData = &vData[width * height >> 2];
 8     yuv420_2_rgb888(pBGR24,yData,uData,vData,width,height,width,width>>1,width*3,yuv2rgb565_table,0);
 9     return true;
10 }

6 基于移位操作实现

       在牺牲一定精度前提下,RGB2YUV和YUV2RGB的转换如下:

      RGB转YUV:

Y = ((R << 6) + (R << 3) + (R << 2) + R + (G << 7) + (G << 4) + (G << 2) + (G << 1) + (B << 4) + (B << 3) + (B << 2) + B) >> 8;

U = (-((R << 5) + (R << 2) + (R << 1)) - ((G << 6) + (G << 3) + (G << 1)) + ((B << 6) + (B << 5) + (B << 4))) >> 8;

V = ((R << 7) + (R << 4) + (R << 3) + (R << 2) + (R << 1) - ((G << 7) + (G << 2)) - ((B << 4) + (B << 3) + (B << 1))) >> 8;

      YUV转RGB:

       R   = ((Y << 8) + ((V << 8) + (V << 5) + (V << 2))) >> 8;
G = ((Y << 8) - ((U << 6) + (U << 5) + (U << 2)) - ((V << 7) + (V << 4) + (V << 2) + V)) >> 8;  

B = ((Y << 8) + (U << 9) + (U << 3)) >> 8;

根据上述公式,具体实现如下:

static void RGBToYUV(int Red, int Green, int Blue, int* Y,int* U,int* V)
{
    *Y = ((Red << 6) + (Red << 3) + (Red << 2) + Red + (Green << 7) + (Green << 4) + (Green << 2) + (Green << 1) + (Blue << 4) + (Blue << 3) + (Blue << 2) + Blue) >> 8;
    *U = (-((Red << 5) + (Red << 2) + (Red << 1)) - ((Green << 6) + (Green << 3) + (Green << 1)) + ((Blue << 6) + (Blue << 5) + (Blue << 4))) >> 8;
    *V = ((Red << 7) + (Red << 4) + (Red << 3) + (Red << 2) + (Red << 1) - ((Green << 7) + (Green << 2)) - ((Blue << 4) + (Blue << 3) + (Blue << 1))) >> 8;
};
static void YUVToRGB(int Y, int U, int V, int* Red, int* Green, int* Blue)
{
    *Red   = ((Y << 8) + ((V << 8) + (V << 5) + (V << 2))) >> 8;
    *Green = ((Y << 8) - ((U << 6) + (U << 5) + (U << 2)) - ((V << 7) + (V << 4) + (V << 2) + V)) >> 8;  
    *Blue = ((Y << 8) + (U << 9) + (U << 3)) >> 8;
};

7 各版本性能分析

      测试序列:1920*1080(基于移位操作实现不参与对比)

      测试环境:OpenCV2.4.8, FFmpeg2.0, YUV2RGB v0.03

 

       由上述表格可以看出,基于FFmpeg的格式转换效率最高,利用查找表法可以提高转换效率,但基于查找表法的转换效率有待进一步优化提升。

后续应关注于FFmpeg其格式转换的算法实现,抽取其实现代码,使格式转换不依赖于FFmpeg库。

9 参考链接

       (1) http://www.cnblogs.com/dwdxdy/p/3713990.html

       (2) http://www.cnblogs.com/qiqibaby/p/8608893.html

       (3) http://blog.csdn.net/Trent1985/article/details/52053397

posted @ 2018-03-20 15:18  淇淇宝贝  阅读(2287)  评论(0编辑  收藏  举报