OpenCV : how to get raw YUY2 image from webcam?

让人想犯罪 __ 提交于 2020-01-14 12:53:52

问题


Do you know how to get raw YUY2 image from webcam, using OpenCV-DirectShow (no VFW) ?

http://opencv.willowgarage.com/wiki/CameraCapture : I succeed getting IPL image (Intel Processing Library image) using the example.

Using the function cvShowImage() in the code, the image is nice on the screen. But I don't want to show image on the screen, nor IPL format, I just want YUYV raw data...

The second part of the wiki page would be what I want, but deviceSetupWithSubtype() does not seem to exist any longer in OpenCV 2.4 (even Google does not know it).

EDIT : I found : it is in the rar file linked on the page ! Google does not "see" in rar files. Here is the link : http://opencv.willowgarage.com/wiki/CameraCapture?action=AttachFile&do=get&target=Camera+property+Settings.rar . I am going to study this.


回答1:


In case anyone else stumbles across this:

ffmpeg has a good API for windows that allows raw YUV image capture. either the CLI:

ffmpeg.exe -f dshow -i video="your-webcam" -vframes 1 test.yuv

or their c API:

AVFormatContext *camera = NULL;
  AVPacket packet;
  avdevice_register_all();
  AVInputFormat *inFrmt = av_find_input_format("dshow");
  int ret = avformat_open_input(&camera, "video=your-webcam", inFrmt, NULL);
  av_init_packet(&packet);

  while (int ret = av_read_frame(camera, &packet) >= 0) {
    // packet.data has raw image data
  }



回答2:


typically, opencv use directshow to get RGB frame from webcam in windows. Opencv VideoCapture class get CV_CAP_PROP_CONVERT_RGB property(boolean flag indicating whehter images shoule be converted to RGB), but it's not working in all my webcams.

Instead of writing DirectShow codes and make your own sample grabber and callback to get the YUY2 data desribe here (they hava made wonderful tools to simplify directshow development.) I modify CameraDs and the (setup Doc) class(the web pages are in Chinese) to get YUY2 data.

In CameraDs.cpp change mt.subtype = MEDIASUBTYPE_RGB24; to mt.subtype = MEDIASUBTYPE_YUY2; (check whether your webcam support this )

and makes a 2 channel YUY2 image instead of RGB 3 channel.

m_pFrame = cvCreateImage(cvSize(m_nWidth, m_nHeight), IPL_DEPTH_8U, 2);

and gets the YUY2 data outside and change it to RGB with opencv interface like:

{
               //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;
        } 


来源:https://stackoverflow.com/questions/10630710/opencv-how-to-get-raw-yuy2-image-from-webcam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!