//YUY2视频转化RGB格式
inline void YUY2_RGB(BYTE *YUY2buff,BYTE *RGBbuff,int dwSize)
{
//B = 1.164(Y - 16) + 2.018(U - 128)
//G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
//R = 1.164(Y - 16) + 1.596(V - 128)
BYTE *orgRGBbuff = RGBbuff;
for(int count=0;count<dwSize;count+=4)
{
//Y0 U0 Y1 V0
float Y0 = *YUY2buff;
float U = *(++YUY2buff);
float Y1 = *(++YUY2buff);
float V = *(++YUY2buff);
++YUY2buff;
*(RGBbuff) = (BYTE)(Y0 + (1.370705 * (V-128)));
*(++RGBbuff) = (BYTE)(Y0 - (0.698001 * (V-128)) - (0.337633 * (U-128)));
*(++RGBbuff) = (BYTE)(Y0 + (1.732446 * (U-128)));
*(++RGBbuff) = (BYTE)(Y1 + (1.370705 * (V-128)));
*(++RGBbuff) = (BYTE)(Y1 - (0.698001 * (V-128)) - (0.337633 * (U-128)));
*(++RGBbuff) = (BYTE)(Y1 + (1.732446 * (U-128)));
++RGBbuff;
}
}
采集摄像头的数据为YUY2格式,OpenCV支持的RGB格式,因此,需要将YUY2转换为RGB格式,再将RGB转换为OpenCV支持的BGR格式,最后就可以显示啦
int CameraShow(BYTE* pData, int len)
{
//change them to rgb
Mat rgb(m_stCameraParam.vHeight,m_stCameraParam.vWidth, CV_8UC3 );
YUY2_RGB(pData, rgb.data, len); //
Mat bgr;
cv::cvtColor(rgb, bgr, CV_RGB2BGR); // RGB转换为BGR
/*显示画面*/
CRect rect(656, 38, 908, 360);
IplImage* iplimg = NULL;
iplimg = &IplImage(bgr);
if (NULL == iplimg) {
return 0;
}
assert(NULL != iplimg);
CvvImage m_CvvImage;
m_CvvImage.CopyOf(iplimg, 1);
HDC hdc = ::GetWindowDC(m_hWnd);
m_CvvImage.DrawToHDC(hdc, rect);
::ReleaseDC(m_hWnd, hdc);
return 0;
}