YUV转IPLImage(RGB)

一个小的程序,在网上找了很久没有发现

自己搞了一个大家看看

第一个是很笨的办法:

yuv三个分量分别写在3个矩阵下,然后合并之后转换为rgb分量的图片格式就可以了;

代码如下:

 

[html] view plain copy
 
  1. IplImage*image,*rgbimg,*yimg,*uimg,*vimg,*uuimg,*vvimg;
  2. rgbimg=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,3);
  3. image=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,3);
  4. yimg=cvCreateImageHeader(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
  5. uimg=cvCreateImageHeader(cvSize(nWidth/2,nHeight/2),IPL_DEPTH_8U,1);
  6. vimg=cvCreateImageHeader(cvSize(nWidth/2,nHeight/2),IPL_DEPTH_8U,1);
  7. uuimg=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
  8. vvimg=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
  9. cvSetData(yimg,pBuf,nWidth);
  10. cvSetData(uimg,pBuf+nWidth*nHeight,nWidth/2);
  11. cvSetData(vimg,pBuf+long(nWidth*nHeight*1.25),nWidth/2);
  12. cvResize(uimg,uuimg,CV_INTER_LINEAR);
  13. cvResize(vimg,vvimg,CV_INTER_LINEAR);
  14. cvMerge(yimg,uuimg,vvimg,NULL,image);
  15. cvCvtColor(image,rgbimg,CV_YCrCb2BGR);
[html] view plain copy
 
  1. IplImage*image,*rgbimg,*yimg,*uimg,*vimg,*uuimg,*vvimg;
  2. rgbimg=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,3);
  3. image=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,3);
  4. yimg=cvCreateImageHeader(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
  5. uimg=cvCreateImageHeader(cvSize(nWidth/2,nHeight/2),IPL_DEPTH_8U,1);
  6. vimg=cvCreateImageHeader(cvSize(nWidth/2,nHeight/2),IPL_DEPTH_8U,1);
  7. uuimg=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
  8. vvimg=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
  9. cvSetData(yimg,pBuf,nWidth);
  10. cvSetData(uimg,pBuf+nWidth*nHeight,nWidth/2);
  11. cvSetData(vimg,pBuf+long(nWidth*nHeight*1.25),nWidth/2);
  12. cvResize(uimg,uuimg,CV_INTER_LINEAR);
  13. cvResize(vimg,vvimg,CV_INTER_LINEAR);
  14. cvMerge(yimg,uuimg,vvimg,NULL,image);
  15. cvCvtColor(image,rgbimg,CV_YCrCb2BGR);


还有一个方法就比较复杂

 

 

 

首先自己根据原理转换为rgb格式

然后利用cvSetData()函数写入数据生成IplImage格式的图片

首先定义转换的公式:

 

[html] view plain copy
 
  1. #defineMR(Y,U,V)(Y+(1.403)*(V-128))
  2. #defineMG(Y,U,V)(Y-(0.344)*(U-128)-(0.714)*(V-128))
  3. #defineMB(Y,U,V)(Y+((1.773)*(U-128)))
[html] view plain copy
 
  1. #defineMR(Y,U,V)(Y+(1.403)*(V-128))
  2. #defineMG(Y,U,V)(Y-(0.344)*(U-128)-(0.714)*(V-128))
  3. #defineMB(Y,U,V)(Y+((1.773)*(U-128)))


yuv转rgb的函数:

 

 

 

 

[html] view plain copy
 
  1. voidYUV420_C_RGB(char*pYUV,unsignedchar*pRGB,intheight,intwidth)
  2. {
  3. char*pY=pYUV;
  4. char*pU=pYUV+height*width;
  5. char*pV=pU+(height*width/4);
  6. unsignedchar*pBGR=NULL;
  7. unsignedcharR=0;
  8. unsignedcharG=0;
  9. unsignedcharB=0;
  10. charY=0;
  11. charU=0;
  12. charV=0;
  13. doubletmp=0;
  14. for(inti=0;i<height;++i)
  15. {
  16. for(intj=0;j<width;++j)
  17. {
  18. pBGR=pRGB+i*width*3+j*3;
  19. Y=*(pY+i*width+j);
  20. U=*pU;
  21. V=*pV;
  22. //B
  23. tmp=MB(Y,U,V);
  24. //B=(tmp>255)?255:(char)tmp;
  25. //B=(B<0)?0:B;
  26. B=(unsignedchar)tmp;
  27. //G
  28. tmp=MG(Y,U,V);
  29. //G=(tmp>255)?255:(char)tmp;
  30. //G=(G<0)?0:G;
  31. G=(unsignedchar)tmp;
  32. //R
  33. tmp=MR(Y,U,V);
  34. //R=(tmp>255)?255:(char)tmp;
  35. //R=(R<0)?0:R;
  36. R=(unsignedchar)tmp;
  37. *pBGR=R;
  38. *(pBGR+1)=G;
  39. *(pBGR+2)=B;
  40. if(i%2==0&&j%2==0)
  41. {
  42. *pU++;
  43. //*pV++;
  44. }
  45. else
  46. {
  47. if(j%2==0)
  48. {
  49. *pV++;
  50. }
  51. }
  52. }
  53. }
  54. }
[html] view plain copy
 
  1. voidYUV420_C_RGB(char*pYUV,unsignedchar*pRGB,intheight,intwidth)
  2. {
  3. char*pY=pYUV;
  4. char*pU=pYUV+height*width;
  5. char*pV=pU+(height*width/4);
  6. unsignedchar*pBGR=NULL;
  7. unsignedcharR=0;
  8. unsignedcharG=0;
  9. unsignedcharB=0;
  10. charY=0;
  11. charU=0;
  12. charV=0;
  13. doubletmp=0;
  14. for(inti=0;i<height;++i)
  15. {
  16. for(intj=0;j<width;++j)
  17. {
  18. pBGR=pRGB+i*width*3+j*3;
  19. Y=*(pY+i*width+j);
  20. U=*pU;
  21. V=*pV;
  22. //B
  23. tmp=MB(Y,U,V);
  24. //B=(tmp>255)?255:(char)tmp;
  25. //B=(B<0)?0:B;
  26. B=(unsignedchar)tmp;
  27. //G
  28. tmp=MG(Y,U,V);
  29. //G=(tmp>255)?255:(char)tmp;
  30. //G=(G<0)?0:G;
  31. G=(unsignedchar)tmp;
  32. //R
  33. tmp=MR(Y,U,V);
  34. //R=(tmp>255)?255:(char)tmp;
  35. //R=(R<0)?0:R;
  36. R=(unsignedchar)tmp;
  37. *pBGR=R;
  38. *(pBGR+1)=G;
  39. *(pBGR+2)=B;
  40. if(i%2==0&&j%2==0)
  41. {
  42. *pU++;
  43. //*pV++;
  44. }
  45. else
  46. {
  47. if(j%2==0)
  48. {
  49. *pV++;
  50. }
  51. }
  52. }
  53. }
  54. }


最后是写入IplImage的代码:

 

 

 

 

[html] view plain copy
 
  1. unsignedchar*pRGB=NULL;
  2. pRGB=(unsignedchar*)malloc(nSize*sizeof(unsignedchar*)*2);
  3. YUV420_C_RGB(pBuf,pRGB,nWidth,nHeight);
  4. IplImage*image;
  5. image=cvCreateImageHeader(cvSize(nWidth,nHeight),IPL_DEPTH_8U,3);
  6. cvSetData(image,pRGB,nWidth*3);
[html] view plain copy
 
  1. unsignedchar*pRGB=NULL;
  2. pRGB=(unsignedchar*)malloc(nSize*sizeof(unsignedchar*)*2);
  3. YUV420_C_RGB(pBuf,pRGB,nWidth,nHeight);
  4. IplImage*image;
  5. image=cvCreateImageHeader(cvSize(nWidth,nHeight),IPL_DEPTH_8U,3);
  6. cvSetData(image,pRGB,nWidth*3);

程序都运行过

 

 

 

编译环境为vs2008

opencv2.0版本

 

原帖地址:http://blog.csdn.net/dreamd1987/article/details/7259479

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值