Normally opencv uses directshow to get the RGB frame from the webcam in the windows. The Opencv VideoCapture class gets the CV_CAP_PROP_CONVERT_RGB property (a boolean flag indicating that whehter shoule images are converted to RGB), but it doesn’t work on all my webcams.
Instead of writing DirectShow codes and creating your own sample capture and callback to get a description of YUY2 data here (they created great tools to simplify directshow.) I modify CameraDs and ( installation document ) (web pages are in Chinese) to get data YUY2.
In CameraDs.cpp, change mt.subtype = MEDIASUBTYPE_RGB24;
at mt.subtype = MEDIASUBTYPE_YUY2;
(check if your webcam supports)
and makes a 2-channel image of YUY2 instead of the RGB 3 channel.
m_pFrame = cvCreateImage(cvSize(m_nWidth, m_nHeight), IPL_DEPTH_8U, 2);
and receives YUY2 data from the outside and changes it to RGB with the opencv interface, for example:
{ //query frame IplImage * pFrame = camera. QueryFrame(); //change them to rgb Mat yuv (480, 640,CV_8UC2 ,pFrame-> imageData); Mat rgb (480, 640,CV_8UC3 ); cvtColor(yuv ,rgb, CV_YUV2BGRA_YUY2); //show image //cvShowImage("camera", rgb); imshow("camera" ,rgb); if ( cvWaitKey(20 ) == 'q') break; }
source share