OpenCV: how to get the original YUY2 image from a webcam?

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

http://opencv.willowgarage.com/wiki/CameraCapture : I was able to get an IPL image (Intel Processing Library image) using an example.

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

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

EDIT: I found: it is in the rar file linked on the page! Google does not “see” 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 learn this.

+4
source share
2 answers

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; } 
0
source

In case anyone else stumbles upon this:

ffmpeg has a nice window API that allows you to capture raw YUV images. or 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 } 
0
source

Source: https://habr.com/ru/post/1412911/


All Articles