YUV与RGB格式转换

YUV与RGB格式转换

转载 2015年11月26日 12:24:43

原文:http://www.cnblogs.com/dwdxdy/p/3713990.html


YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式。

因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式。

RGB与YUV的变换公式如下:  

 

 

 

  

 

 

YUV(256 级别) 可以从8位 RGB 直接计算:

Y = 0.299 R + 0.587 G + 0.114 B

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

V = 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. 基本实现

按照YUV与RGB的变换公式,逐像素访问Y、U、V分量的值,并转换成RGB。

复制代码
bool YV12ToBGR24_Native(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
{
    if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
        return false;
    const long len = width * height;
    unsigned char* yData = pYUV;
    unsigned char* vData = &yData[len];
    unsigned char* uData = &vData[len >> 2];

    int bgr[3];
    int yIdx,uIdx,vIdx,idx;
    for (int i = 0;i < height;i++){
        for (int j = 0;j < width;j++){
            yIdx = i * width + j;
            vIdx = (i/2) * (width/2) + (j/2);
            uIdx = vIdx;

            bgr[0] = (int)(yData[yIdx] + 1.732446 * (uData[vIdx] - 128));                                    // b分量
            bgr[1] = (int)(yData[yIdx] - 0.698001 * (uData[uIdx] - 128) - 0.703125 * (vData[vIdx] - 128));    // g分量
            bgr[2] = (int)(yData[yIdx] + 1.370705 * (vData[uIdx] - 128));                                    // r分量

            for (int k = 0;k < 3;k++){
                idx = (i * width + j) * 3 + k;
                if(bgr[k] >= 0 && bgr[k] <= 255)
                    pBGR24[idx] = bgr[k];
                else
                    pBGR24[idx] = (bgr[k] < 0)?0:255;
            }
        }
    }
    return true;
}
复制代码

2. 基于查表法的实现 

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

优化过程可参考:http://blog.csdn.net/colorant/article/details/1913162/ 

复制代码
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 };
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 };
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 };
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 };

bool YV12ToBGR24_Table(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
{
    if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
        return false;
    const long len = width * height;
    unsigned char* yData = pYUV;
    unsigned char* vData = &yData[len];
    unsigned char* uData = &vData[len >> 2];

    int bgr[3];
    int yIdx,uIdx,vIdx,idx;
    int rdif,invgdif,bdif;
    for (int i = 0;i < height;i++){
        for (int j = 0;j < width;j++){
            yIdx = i * width + j;
            vIdx = (i/2) * (width/2) + (j/2);
            uIdx = vIdx;

            rdif = Table_fv1[vData[vIdx]];
            invgdif = Table_fu1[uData[uIdx]] + Table_fv2[vData[vIdx]];
            bdif = Table_fu2[uData[uIdx]];

            bgr[0] = yData[yIdx] + bdif;    
            bgr[1] = yData[yIdx] - invgdif;
            bgr[2] = yData[yIdx] + rdif;

            for (int k = 0;k < 3;k++){
                idx = (i * width + j) * 3 + k;
                if(bgr[k] >= 0 && bgr[k] <= 255)
                    pBGR24[idx] = bgr[k];
                else
                    pBGR24[idx] = (bgr[k] < 0)?0:255;
            }
        }
    }
    return true;
}
复制代码

3. 基于OpenCV的实现

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

复制代码
bool YV12ToBGR24_OpenCV(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
{
    if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
        return false;
    Mat dst(height,width,CV_8UC3,pBGR24);
    Mat src(height + height/2,width,CV_8UC1,pYUV);
    cvtColor(src,dst,CV_YUV2BGR_YV12);
    return true;
}
复制代码

4. 基于FFmpeg的实现 

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

复制代码
bool YV12ToBGR24_FFmpeg(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
{
    if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
        return false;
    //int srcNumBytes,dstNumBytes;
    //uint8_t *pSrc,*pDst;
    AVPicture pFrameYUV,pFrameBGR;

    //pFrameYUV = avpicture_alloc();
    //srcNumBytes = avpicture_get_size(PIX_FMT_YUV420P,width,height);
    //pSrc = (uint8_t *)malloc(sizeof(uint8_t) * srcNumBytes);
    avpicture_fill(&pFrameYUV,pYUV,PIX_FMT_YUV420P,width,height);

    //U,V互换
    uint8_t * ptmp=pFrameYUV.data[1];
    pFrameYUV.data[1]=pFrameYUV.data[2];
    pFrameYUV.data [2]=ptmp;

    //pFrameBGR = avcodec_alloc_frame();
    //dstNumBytes = avpicture_get_size(PIX_FMT_BGR24,width,height);
    //pDst = (uint8_t *)malloc(sizeof(uint8_t) * dstNumBytes);
    avpicture_fill(&pFrameBGR,pBGR24,PIX_FMT_BGR24,width,height);

    struct SwsContext* imgCtx = NULL;
    imgCtx = sws_getContext(width,height,PIX_FMT_YUV420P,width,height,PIX_FMT_BGR24,SWS_BILINEAR,0,0,0);

    if (imgCtx != NULL){
        sws_scale(imgCtx,pFrameYUV.data,pFrameYUV.linesize,0,height,pFrameBGR.data,pFrameBGR.linesize);
        if(imgCtx){
            sws_freeContext(imgCtx);
            imgCtx = NULL;
        }
        return true;
    }
    else{
        sws_freeContext(imgCtx);
        imgCtx = NULL;
        return false;
    }
}
复制代码

5. 基于Pinknoise的实现 

参考资料:http://wss.co.uk/pinknoise/yuv2rgb/

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

复制代码
bool YV12ToBGR24_Pinknoise(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
{
    if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
        return false;
    unsigned char *yData = pYUV;
    unsigned char *vData = &pYUV[width * height];
    unsigned char *uData = &vData[width * height >> 2];
    yuv420_2_rgb888(pBGR24,yData,uData,vData,width,height,width,width>>1,width*3,yuv2rgb565_table,0);
    return true;
}
复制代码

6. 转换效率分析 

测试序列:1920*1080

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

Method

Time(ms)

YV12ToBGR24_Native

83.7263

YV12ToBGR24_Table

54.2376

YV12ToBGR24_OpenCV

26.0529

YV12ToBGR24_FFmpeg

3.41499

YV12ToBGR24_Pinknoise

14.1215

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

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

参考资料:

https://code.google.com/p/libyuv/

                </div>
</article>
  <div class="readall_box csdn-tracking-statistics tracking-click readall_box_nobg" data-mod="popu_376" style="display: none;">
      <div class="read_more_mask"></div>
      <a class="btn btn-large btn-gray-fred read_more_btn" target="_self">阅读全文</a>
  </div>
  <div class="article_copyright">
          </div>
  <ul class="article_collect clearfix csdn-tracking-statistics tracking-click" data-mod="popu_378" style="display: none;">
      <li class="tit">本文已收录于以下专栏:</li>









  <div class="more_comment" style="display: none;">
      <div id="comment_bar" class="trackgin-ad" data-mod="popu_385"></div>
  </div>

  <h3 class="recommend_tit" id="related">相关文章推荐</h3>
  <div class="recommend_list clearfix" id="rasss">
                                                                                  <dl class="clearfix csdn-tracking-statistics" data-mod="popu_387" data-poputype="feed" data-feed-show="false" data-dsm="post">
                  <dd>
                      <h2><a href="http://blog.csdn.net/u011258240/article/details/54601603" target="_blank" strategy="BlogCommendFromBaidu_0">YUV与RGB互转各种公式 (YUV与RGB的转换公式有很多种,请注意区别!!!)</a></h2>
                      <div class="summary">
                          公式:基于BT.601-6

1.小数形式,full range
2.整数形式(减少计算量)full range
3. 量化后的公式( Y~(16,235) U/V ~(0,240) ) tv …







图像算法研究—一种简单的YUV转RGB的优化算法



本文介绍了一种YUV与RGB相互转换的优化算法公式,跟大家分享一下!





(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('
');
(window.slotbydup=window.slotbydup || []).push({
id: '4765209',
container: s,
size: '808,120',
display: 'inlay-fix'
});
})();



YUV 格式与 RGB 格式的相互转换公式及C++ 代码



YUV 格式与 RGB 格式的相互转换公式最近在用的一个工业相机,输出的图像格式是 YUY2 格式。而在电脑上显示时需要 RGB 格式,所以就花了些时间在网上查了些相关的资料。说实话,网上关于 YUV…






RGB与YUV转换



1 前言
        自然界的颜色千变万化,为了给颜色一个量化的衡量标准,就需要建立色彩空间模型来描述各种各样的颜色,由于人对色彩的感知是一个复杂的生理和心理联合作用 的过程,所以在不同的应用领…






经典算法,yuv与rgb互转,查表法,让你的软件飞起来



代码的运算速度取决于以下几个方面

1、 算法本身的复杂度,比如MPEG比JPEG复杂,JPEG比BMP图片的编码复杂。

2、 CPU自身的速度和设计架构

3、 CPU的总线带宽

4、 您自己代…







(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('
');
(window.slotbydup=window.slotbydup || []).push({
id: '4983339',
container: s,
size: '808,120',
display: 'inlay-fix'
});
})();



最简单的基于FFmpeg的libswscale的示例(YUV转RGB)



本文记录一个基于FFmpeg的libswscale的示例。Libswscale里面实现了各种图像像素格式的转换,例如YUV与RGB之间的转换;以及图像大小缩放(例如640x360拉伸为1280x720…






YUV / RGB 格式及快速转换算法



RGB TO YUV转换原理及代码示例[转]

RGB TO YUV转换原理及代码示例
                                      
RGB TO …







YUV格式学习:YUV444转换RGB24



YUV格式有很多种,按其采样方式,有444、422、420,还有411(但不常见)。针对数据的排序,又有平面格式和打包格式,还有“踢啊”特有的半平面格式——这些排列组合,就显得YUV格式多种多样,初看…


  • subfate
  • subfate
  • 2015年08月05日 22:01
  • 2173





YUV4:2:2转换成RGB的代码



int convert_yuv_to_rgb_pixel(int y, int u, int v)
{
uint pixel32 = 0;
uchar pixel = (uchar )&pix…


  • chyxwzn
  • chyxwzn
  • 2012年12月27日 16:15
  • 4023





常用YUV转RGB代码



常用YUV转RGB java代码

public class YuvToRGB {
private static int R = 0;
private static int G = 1;







neon指令进行yuv420到rgb24转换效率



从网上找到了一个用neon指令优化yuv420转换成rgb24的代码, 在cortex-A8架构、主频1G的cpu下进行对一帧qcif(176x144)数据测试,另外用网上很流行的用C写的算法做比较,…


  • alien75
  • alien75
  • 2012年03月21日 11:21
  • 7632





从零开始学习音视频编程技术(十五) YUV420P转RGB32



原文地址: http://blog.yundiantech.com/?log=blog&id=19

上一节讲解了YUV420P格式的内容。

我说过,我们不是为了做研究。 平白无故讲了YUV420P的…







YUV422与RGB互相转换



转自:  http://m.blog.csdn.net/blog/guo8113/25333837#

 

前一段时间在DM8168中进行颜色空间的转换,在网上找了些程序,自己也根据网上的改了下,由于…







RGB与YUV之间的转换



一.RGB模型与YUV模型

1.RGB模型
我们知道物理三基色分别是红(Red)、绿(Green)、蓝(Blue)。现代的显示器技术就是通过组合不同强度的红绿蓝三原色,来达成几乎任何一种可见光的颜色…







BMP 转 YUV (BMP2YUV)+ YUV 转RGB



http://blog.csdn.net/leixiaohua1020/article/details/13506099

可以下载工程直接查看,可以修改部分效率;…







rgb to yuv 互相转化



说明:下面的代码用C\C++执行都可以,用C的时候请把#include 删除。

RGB to YUV420 原代码:  RGB2YUV.CPP文件

[cpp]
view plain…



  • yangzm
  • yangzm
  • 2014年09月18日 10:27
  • 1338







Delphi7高级应用开发随书源码




  • 2003年04月30日 00:00
  • 676KB
  • 下载







FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)



FFMPEG中的swscale提供了视频原始数据(YUV420,YUV422,YUV444,RGB24…)之间的转换,分辨率变换等操作,使用起来十分方便,在这里记录一下它的用法。

swscale…







YUV格式详解及 YUV与RGB转换



转自: http://msdn.microsoft.com/zh-cn/library/ms867704.aspx

Microsoft Digital Media Division

适用于:

Mi…







常用YUV转RGB代码



常用YUV转RGB [java] view plaincopyprint?public class YuvToRGB {      private static int R = 0;      pri…


  • mao0514
  • mao0514
  • 2016年01月19日 19:14
  • 1736





  • 4
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值