图像处理之 YUYV422 To OpenCV IplImage

可能你需要 将 YUYV422 格式的buffer 转换到 opencv 中的IplImage 
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <highgui/highgui.hpp>  
  2. using namespace cv;  
  3. class  ConvertYuvForCV  
  4. {  
  5.   public:  
  6.        ConvertYuvForCV(const int&iWidth,const int &Height,const string &strCvWindowName);  
  7.         ~ConvertYuvForCV();  
  8.         bool CvWindowShow();  
  9.         void YUYV422ToIplImage(const unsigned char *pYUYV422,const int &iLen);  
  10.   private:  
  11.          void YUVToRGB(const int &iY, const int & iU, const int & iV, int &iR, int &iG, int &iB);  
  12.          void ConvertYUYV422ToRGB(const unsigned char* pYUYV422, const int &iLen,unsigned char *pRGB);  
  13.          void ConvertYUYV422ToBGR(const unsigned char* pYUYV422, const int &iLen,unsigned char *pRGB);  
  14.   private:  
  15.          int m_iWidth;  
  16.          int m_Height;  
  17.          IplImage *m_IplImage;  
  18.          unsigned char *m_pBGR;  
  19.          std::string  m_strCvWindowName;  
  20. };  
  21.   
  22. ConvertYuvForCV::ConvertYuvForCV(const int&iWidth,const int &iHeight ,const string &strCvWindowName)  
  23. {  
  24.     m_iWidth = iWidth;  
  25.     m_Height = iHeight;  
  26.     m_IplImage = cvCreateImage(cvSize(iWidth, iHeight),IPL_DEPTH_8U, 3);  
  27.     m_pBGR     = new unsigned char[iWidth*iHeight*3];  
  28.     m_strCvWindowName = strCvWindowName;  
  29.     if(!m_strCvWindowName.empty()){  
  30.         cvNamedWindow(m_strCvWindowName.c_str());  
  31.     }  
  32. }  
  33. ConvertYuvForCV::~ConvertYuvForCV()  
  34. {  
  35.     cvReleaseImage(&m_IplImage);  
  36.     delete[]m_pBGR;  
  37.     if(!m_strCvWindowName.empty()){  
  38.         cvDestroyWindow(m_strCvWindowName.c_str());  
  39.     }  
  40. }  
  41. void ConvertYuvForCV::YUYV422ToIplImage(const unsigned char *pYUYV422,const int &iLen)  
  42. {  
  43.     ConvertYUYV422ToBGR(pYUYV422,iLen,m_pBGR);  
  44.     cvSetData(m_IplImage,m_pBGR,m_iWidth*3);  
  45. }  
  46. void ConvertYuvForCV::YUVToRGB(const int &iY, const int & iU, const int & iV, int &iR, int &iG, int &iB)  
  47. {  
  48.     assert( &iR != NULL && &iG != NULL && &iB != NULL);  
  49.   
  50.     iR = iY + 1.13983 * (iV - 128);  
  51.     iG = iY - 0.39465 * (iU - 128) - 0.58060 * (iV - 128);  
  52.     iB = iY + 2.03211 * (iU - 128);  
  53.   
  54.     iR = iR > 255 ? 255 : iR;  
  55.     iR = iR < 0   ? 0   : iR;  
  56.   
  57.     iG = iG > 255 ? 255 : iG;  
  58.     iG = iG < 0   ? 0   : iG;  
  59.   
  60.     iB = iB > 255 ? 255 : iB;  
  61.     iB = iB < 0   ? 0   : iB;  
  62. }  
  63. void ConvertYuvForCV::ConvertYUYV422ToRGB(const unsigned char* pYUYV422, const int &iLen,unsigned char *pRGB)  
  64. {  
  65.     //YUYV422 640*480*2     TO  BGR 640*480*3  
  66.     int iR, iG, iB;  
  67.     int iY0, iY1, iU, iV;  
  68.     int i = 0;  
  69.     int j = 0;  
  70.     for(i = 0; i < iLen; i += 4)  
  71.     {  
  72.         iY0 = pYUYV422[i + 0];  
  73.         iU  = pYUYV422[i + 1];  
  74.         iY1 = pYUYV422[i + 2];  
  75.         iV  = pYUYV422[i + 3];  
  76.         YUVToRGB(iY0, iU, iV, iR, iG, iB);  
  77.         pRGB[j++] = iR;  
  78.         pRGB[j++] = iG;  
  79.         pRGB[j++] = iB;  
  80.         YUVToRGB(iY1, iU, iV, iR, iG, iB);  
  81.         pRGB[j++] = iR;  
  82.         pRGB[j++] = iG;  
  83.         pRGB[j++] = iB;  
  84.     }  
  85.     pRGB[j] = '\0';  
  86. }  
  87. void ConvertYuvForCV::ConvertYUYV422ToBGR(const unsigned char* pYUYV422, const int &iLen,unsigned char *pBGR)  
  88. {  
  89.     //YUYV422 640*480*2     TO  BGR 640*480*3  
  90.     int iR, iG, iB;  
  91.     int iY0, iY1, iU, iV;  
  92.     int i = 0;  
  93.     int j = 0;  
  94.     for(i = 0; i < iLen; i += 4)  
  95.     {  
  96.         //其排列方式是y0 u y1 v  
  97.         //直接提取出来y、u、v三个分量,然后使用公式转成RGB即可  
  98.         //因为两个y共享一对uv。故y0 u y1 v能提取出两组(y, u, v)  
  99.         iY0 = pYUYV422[i + 0];  
  100.         iU  = pYUYV422[i + 1];  
  101.         iY1 = pYUYV422[i + 2];  
  102.         iV  = pYUYV422[i + 3];  
  103.         YUVToRGB(iY0, iU, iV, iR, iG, iB);  
  104.         pBGR[j++] = iB;  
  105.         pBGR[j++] = iG;  
  106.         pBGR[j++] = iR;  
  107.         YUVToRGB(iY1, iU, iV, iR, iG, iB);  
  108.         pBGR[j++] = iB;  
  109.         pBGR[j++] = iG;  
  110.         pBGR[j++] = iR;  
  111.     }  
  112.     pBGR[j] = '\0';  
  113. }  
  114. bool  ConvertYuvForCV::CvWindowShow()  
  115. {  
  116.     if(!m_strCvWindowName.empty())  
  117.     {  
  118.         cvShowImage(m_strCvWindowName.c_str(), m_IplImage);  
  119.         cvWaitKey(33);  
  120.     }  
  121.     else  
  122.     {  
  123.         std::cout<<"Have no Available CvWindow "<<std::endl;  
  124.         return false;  
  125.     }  
  126.     return true;  
  127. }  
  128. int main()  
  129. {  
  130.     const char* pFilename = "/root/Test_yuyv422.yuv";  
  131.     FILE* pFin = fopen(pFilename, "rb");  
  132.     if( pFin == NULL )  
  133.     {  
  134.         printf("can't open the file\n");  
  135.         return -1;  
  136.     }  
  137.     int iWidth = 352;  
  138.     int iHeight = 288;  
  139.     int iFrameSize = iWidth * iHeight * 2;  
  140.     unsigned char* pYUYV422Buff = new unsigned char[iFrameSize];  
  141.     string strCvWindowName("FUCK");  
  142.     ConvertYuvForCV oConvertYuvForCV(iWidth,iHeight,strCvWindowName);  
  143.     while( 1 )  
  144.     {  
  145.         int iRet = fread(pYUYV422Buff, 1, iFrameSize, pFin);  
  146.         if( iRet != iFrameSize )  
  147.         {  
  148.            break;  
  149.         }  
  150.         oConvertYuvForCV.YUYV422ToIplImage(pYUYV422Buff,iFrameSize);  
  151.         oConvertYuvForCV.CvWindowShow();  
  152.     }  
  153.     delete[]pYUYV422Buff;  
  154.     return 0;  
  155. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值