YUV420P转换为RGB32格式

作者:masikkk

转自:http://blog.csdn.net/masibuaa/article/details/10004161


从网络摄像机中获取的帧数据是YUV420P格式的,而我们处理图像需要RGB格式,在网上找了一段将YUV420P格式的帧转换为RGB的代码。


<方法一> 直接计算,效率低

  1. // 转换 YV12 到 RGB24  
  2. // pYUV 的大小 (3 * iWidth * iHeight / 2)  
  3. // pRGB 的大小 (3 * iWidth * iHeight)  
  4. // 如果成功返回 true, 否则 false  
  5. bool YV12_to_RGB24(unsigned char* pYV12, unsigned char* pRGB24, int iWidth, int iHeight)  
  6. {  
  7. if(!pYV12 || !pRGB24)  
  8. return false;  
  9.   
  10. const long nYLen = long(iHeight * iWidth);  
  11. const int nHfWidth = (iWidth>>1);  
  12.   
  13. if(nYLen<1 || nHfWidth<1)   
  14. return false;  
  15.   
  16. // yv12数据格式,其中Y分量长度为width * height, U和V分量长度都为width * height / 4  
  17. // |WIDTH |  
  18. // y......y--------  
  19. // y......y HEIGHT  
  20. // y......y  
  21. // y......y--------  
  22. // v..v  
  23. // v..v  
  24. // u..u  
  25. // u..u  
  26. unsigned char* yData = pYV12;  
  27. unsigned char* vData = &yData[nYLen];  
  28. unsigned char* uData = &vData[nYLen>>2];  
  29.   
  30. if(!uData || !vData)  
  31. return false;  
  32.   
  33. // Convert YV12 to RGB24  
  34. //   
  35. // formula  
  36. // [1 1 1 ]  
  37. // [r g b] = [y u-128 v-128] [0 0.34375 0 ]  
  38. // [1.375 0.703125 1.734375]  
  39. // another formula  
  40. // [1 1 1 ]  
  41. // [r g b] = [y u-128 v-128] [0 0.698001 0 ]  
  42. // [1.370705 0.703125 1.732446]  
  43. int rgb[3];  
  44. int i, j, m, n, x, y;  
  45. m = -iWidth;  
  46. n = -nHfWidth;  
  47. for(y=0; y < iHeight; y++)  
  48. {  
  49. m += iWidth;  
  50. if(!(y % 2))  
  51. n += nHfWidth;  
  52. for(x=0; x < iWidth; x++)  
  53. {  
  54. i = m + x;  
  55. j = n + (x>>1);  
  56. rgb[2] = int(yData[i] + 1.370705 * (vData[j] - 128)); // r分量值  
  57. rgb[1] = int(yData[i] - 0.698001 * (uData[j] - 128) - 0.703125 * (vData[j] - 128)); // g分量值  
  58. rgb[0] = int(yData[i] + 1.732446 * (uData[j] - 128)); // b分量值  
  59.   
  60. j = nYLen - iWidth - m + x;  
  61. i = (j<<1) + j;  
  62. for(j=0; j<3; j++)  
  63. {  
  64. if(rgb[j]>=0 && rgb[j]<=255)  
  65. pRGB24[i + j] = rgb[j];  
  66. else  
  67. pRGB24[i + j] = (rgb[j] < 0) ? 0 : 255;  
  68. }  
  69. }  
  70. }  
  71. return true;  
  72. }  

在利用OpenCV进行处理时,如果直接将转换好的数据赋给IplImage->imageData,会出现红蓝通道对调的情况,因为OpenCV中默认使用的是BGR的排列方式。

将代码中的给rgb复制的一段稍微改一下就行,改为:

  1. rgb[0] = int(yData[i] + 1.370705 * (vData[j] - 128)); // r分量值  
  2. rgb[1] = int(yData[i] - 0.698001 * (uData[j] - 128) - 0.703125 * (vData[j] - 128)); // g分量值  
  3. rgb[2] = int(yData[i] + 1.732446 * (uData[j] - 128)); // b分量值  


<方法二>查表,效率高

  1. #include   <string>   
  2. #include   <string.h>   
  3. using   namespace   std;   
  4.   
  5. static   long   int   crv_tab[256];   
  6. static   long   int   cbu_tab[256];   
  7. static   long   int   cgu_tab[256];   
  8. static   long   int   cgv_tab[256];   
  9. static   long   int   tab_76309[256];   
  10. static   unsigned   char   clp[1024]; //for   clip   in   CCIR601   
  11.   
  12.   
  13. void   InitConvtTbl();   
  14. void   YUV2RGB420(unsigned   char   *src,   unsigned   char   *dst_ori,   
  15.     int   width,int   height);   
  16.   
  17. /****************************************************/   
  18. /* Sum   the   input */   
  19. /* Input:   input,   len */   
  20. /* Output:   input */   
  21. /* Algorithm:   add */   
  22. /****************************************************/   
  23. void   InitConvtTbl()   
  24. {   
  25.     long   int   crv,cbu,cgu,cgv;   
  26.     int   i,ind;         
  27.   
  28.     crv   =   104597;   cbu   =   132201;     /*   fra   matrise   i   global.h   */   
  29.     cgu   =   25675;     cgv   =   53279;   
  30.   
  31.     for   (i   =   0;   i   <   256;   i++)   {   
  32.         crv_tab[i]   =   (i-128)   *   crv;   
  33.         cbu_tab[i]   =   (i-128)   *   cbu;   
  34.         cgu_tab[i]   =   (i-128)   *   cgu;   
  35.         cgv_tab[i]   =   (i-128)   *   cgv;   
  36.         tab_76309[i]   =   76309*(i-16);   
  37.     }   
  38.   
  39.     for   (i=0;   i <384;   i++)   
  40.         clp[i]   =0;   
  41.     ind=384;   
  42.     for   (i=0;i <256;   i++)   
  43.         clp[ind++]=i;   
  44.     ind=640;   
  45.     for   (i=0;i <384;i++)   
  46.         clp[ind++]=255;   
  47. }  
  48.   
  49. void   YUV2RGB420(unsigned   char   *src,   unsigned   char   *dst_ori,   
  50.     int   width,int   height)   
  51. {   
  52.     unsigned   char   *src0;   
  53.     unsigned   char   *src1;   
  54.     unsigned   char   *src2;   
  55.     int   y1,y2,u,v;     
  56.     unsigned   char   *py1,*py2;   
  57.     int   i,j,   c1,   c2,   c3,   c4;   
  58.     unsigned   char   *d1,   *d2,   *d3;   
  59.   
  60.     //Initialization   
  61.     src0=src;   
  62.     src1=src+width*height;   
  63.     src2=src+width*height+width*height/4;   
  64.   
  65.     py1=src0;   
  66.     py2=py1+width;   
  67.     d1=dst_ori + 3 * width * (height -1);   
  68.     d2=d1-3*width;   
  69.     for   (j   =   0;   j   <   height;   j   +=   2)   {     
  70.         for   (i   =   0;   i   <   width;   i   +=   2)   {   
  71.   
  72.             u   =   *src1++;   
  73.             v   =   *src2++;   
  74.   
  75.             c1   =   crv_tab[v];   
  76.             c2   =   cgu_tab[u];   
  77.             c3   =   cgv_tab[v];   
  78.             c4   =   cbu_tab[u];   
  79.   
  80.             //up-left   
  81.             y1   =   tab_76309[*py1++];   
  82.             *d1++   =   clp[384+((y1   +   c4)>> 16)];       
  83.             *d1++   =   clp[384+((y1   -   c2   -   c3)>> 16)];   
  84.             *d1++   =   clp[384+((y1   +   c1)>> 16)];   
  85.   
  86.             //down-left   
  87.             y2   =   tab_76309[*py2++];   
  88.             *d2++   =   clp[384+((y2   +   c4)>> 16)];       
  89.             *d2++   =   clp[384+((y2   -   c2   -   c3)>> 16)];   
  90.             *d2++   =   clp[384+((y2   +   c1)>> 16)];   
  91.   
  92.             //up-right   
  93.             y1   =   tab_76309[*py1++];   
  94.             *d1++   =   clp[384+((y1   +   c4)>> 16)];       
  95.             *d1++   =   clp[384+((y1   -   c2   -   c3)>> 16)];   
  96.             *d1++   =   clp[384+((y1   +   c1)>> 16)];   
  97.   
  98.             //down-right   
  99.             y2   =   tab_76309[*py2++];   
  100.             *d2++   =   clp[384+((y2   +   c4)>> 16)];       
  101.             *d2++   =   clp[384+((y2   -   c2   -   c3)>> 16)];   
  102.             *d2++   =   clp[384+((y2   +   c1)>> 16)];   
  103.         }   
  104.         d1   -=   3*width * 3;   
  105.         d2   -=   3*width * 3;   
  106.         py1+=       width;   
  107.         py2+=       width;   
  108.     }                 
  109.   
  110.   
  111. }   








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值