YUY数据转换为RGB数据,并进行灰度化处理显示


BYTE clip255(long Value)
{
 BYTE retValue;
 
 if (Value > 255)
  retValue = 255;
 else if (Value < 0)
  retValue = 0;
 else
  retValue = (BYTE)Value;

 return retValue;
}
//win7采到的数据默认YUY格式,一个像素用2位表示,这里将YUY转换为RGB格式(3个字节),便于显示,不转换会出现黑框框
void YUY2_RGB2_ljh(unsigned char* YUY2buff,unsigned char  *RGBbuff,long dwSize)
{
       unsigned char *orgRGBbuff = RGBbuff;
       for( long count = 0; count < dwSize; count += 4 )
       {
              unsigned char Y0 = *YUY2buff;
              unsigned char U = *(++YUY2buff);
              unsigned char Y1 = *(++YUY2buff);
              unsigned char V = *(++YUY2buff);
              ++YUY2buff;
              long Y,C,D,E;
              unsigned char R,G,B;
              Y = Y0;
              C = Y - 16;
              D = U - 128;
              E = V - 128;
              R = clip255(( 298 * C           + 409 * E + 128) >> 8);
              G = clip255(( 298 * C - 100 * D - 208 * E + 128)>> 8);
              B = clip255(( 298 * C + 516 * D           + 128) >> 8);
   *(RGBbuff)   = B;
   *(++RGBbuff) = G;
              *(++RGBbuff) = R;
              Y = Y1;
              C = Y-16;
              D = U-128;
              E = V-128;
              R = clip255(( 298 * C           + 409 * E + 128) >> 8);
              G = clip255(( 298 * C - 100 * D - 208 * E + 128)>> 8);
              B = clip255(( 298 * C + 516 * D           + 128) >> 8);
             *(++RGBbuff) = B;
              *(++RGBbuff) = G;
              *(++RGBbuff) = R;
              ++RGBbuff;
       }
}
//数组按行翻转
void SwapArrayljh2(unsigned char*pS, long dwCount,int width,int height)

{
       char temp;
       long nToSwap = 0;                   // 要交换的序列号
       long nLineSr = 0;                     //当前所在行数
       long nPerLineTotal =width*3;    //  每行的总个数 528

       // 先进行  1) 操作
       for(long i = 0; i < dwCount/2; i++)   
       {
              nLineSr  = i/nPerLineTotal;             // 当前所在行数 0--527 第 0 行;528-1057 第1行
              nToSwap=  (height - nLineSr-1)*nPerLineTotal + (i % nPerLineTotal);
              temp= pS[i];
              pS[i]= pS[nToSwap];
              pS[nToSwap]= temp;

       }
}
//将数组中数据灰度化处理
void huiDuHua(unsigned char* rgbBuff,long dwSize)
{
 BYTE *pIm;
 int i,j;
 pIm = (BYTE*)malloc(dwSize*sizeof(BYTE));
 j=0;
 for(i=0;i<dwSize;i++)
 {
  pIm[i] = 0.2216*(float)rgbBuff[j+2]+0.7152*(float)rgbBuff[j+1]+0.0722*(float)rgbBuff[j];
  j+=3;
 }
 j=0;
 for(i=0;i<dwSize;i++)
 {
  rgbBuff[j] = pIm[i];
  rgbBuff[j+1] = pIm[i];
  rgbBuff[j+2] = pIm[i];
  j+=3;
 }
 free(pIm);
}

//在这里进行图像处理显示操作
BOOL CVideCapture::DisplayVideoImage(CWnd *ShowWnd, unsigned char VideoDate[], BITMAPINFO bmpInfo)
{
 CRect WndRect;
 ShowWnd->GetWindowRect(WndRect);

 CDC* pDC = ShowWnd->GetDC();
 CDC MemDC;
 MemDC.CreateCompatibleDC(pDC);
 CBitmap bmp;
 bmp.CreateCompatibleBitmap(pDC, bmpInfo.bmiHeader.biWidth, bmpInfo.bmiHeader.biHeight);
 CBitmap *pOldBmp = MemDC.SelectObject(&bmp);

 unsigned char *rgbBuff = new unsigned char[bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biHeight*3];
 YUY2_RGB2_ljh(VideoDate,rgbBuff,bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biHeight*2);
 //前一步生成的数据图像是倒立的,进行翻转
 SwapArrayljh2(rgbBuff,bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biHeight*3,bmpInfo.bmiHeader.biWidth,bmpInfo.bmiHeader.biHeight);
 //将彩色数据灰度化
 huiDuHua(rgbBuff,bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biHeight);
 //将数据发送到设备
 if(!::SetDIBitsToDevice(MemDC.GetSafeHdc(),0, 0, bmpInfo.bmiHeader.biWidth, bmpInfo.bmiHeader.biHeight, 0, 0, 0,
  bmpInfo.bmiHeader.biHeight, rgbBuff, &bmpInfo, DIB_RGB_COLORS))
 {
 }
 //显示灰度图像
 pDC->BitBlt(0, 0, WndRect.Width(), WndRect.Height(), &MemDC, 0, 0, SRCCOPY);
 return TRUE;

}

原文地址:https://www.cnblogs.com/ltfbk/p/4591971.html