Opencv VideoCapture.set greyscale?

I would not convert every frame using a video camera using cvtColor(frame, image, CV_RGB2GRAY);
Anyway to install VideoCapture to get directly in grayscale?

Example:

 VideoCapture cap(0); cap.set(CV_CAP_PROP_FRAME_WIDTH,420); cap.set(CV_CAP_PROP_FRAME_HEIGHT,340); cap.set(CV_CAP_GREYSCALE,1); //< ??? 
+7
source share
3 answers

It's impossible. Here is a list of all codes:

 CV_CAP_PROP_POS_MSEC - position in milliseconds from the file beginning CV_CAP_PROP_POS_FRAMES - position in frames (only for video files) CV_CAP_PROP_POS_AVI_RATIO - position in relative units (0 - start of the file, 1 - end of the file) CV_CAP_PROP_FRAME_WIDTH - width of frames in the video stream (only for cameras) CV_CAP_PROP_FRAME_HEIGHT - height of frames in the video stream (only for cameras) CV_CAP_PROP_FPS - frame rate (only for cameras) CV_CAP_PROP_FOURCC - 4-character code of codec (only for cameras). 

Or (if possible, using some utilities), you can configure the camera to display only grayscale images.

To convert a color image to grayscale, you must call cvtColor with the code CV_BGR2GRAY . It does not take much time.

+3
source

If your camera supports YUV420, you can simply take the Y-channel: http://en.wikipedia.org/wiki/YUV

As it is well explained here: Access to each individual channel in OpenCV

Warning: The Y-channel may not be the first Mat that you get with split (), so you should make imshow () of all of them separately and choose the one that looks like a "real" gray image. The rest will be just Waaaay out of contrast, so that will be obvious. For me it was the second checkmate.

As a rule, any camera should be able to make YUV420, since transferring frames directly to RGB is slower, so YUV is pretty much used by almost every camera. :)

+6
source

This is not possible if you are using v4l (the default cv capture method on desktop Linux). CV_CAP_PROP_FORMAT exists, but is simply ignored. You must convert the images to grayscale manually. If your device supports it, you might want to cap_v4l.cpp so that the v4l interface sets the format to grayscale.

In Android, this is possible with the following native code (for the 0th device):

 #include<opencv2/highgui/highgui.hpp> #include<opencv2/highgui/highgui_c.h> cv::VideoCapture camera(0); camera->open(0); cv::Mat dest(480,640,CV_8UC1); if(camera->grab()) camera->retrieve(dest,CV_CAP_ANDROID_GREY_FRAME); 

Here, passing CV_CAP_ANDROID_GREY_FRAME to the channel cv::VideoCapture::retrieve(cv::Mat,int) parameter converts the color of the YUV NV21 image (aka yuv420sp) to shades of gray. This is just a mapping of the Y-channel into a grayscale image that is not associated with any actual conversion or memcpy , therefore very fast. You can check this behavior at https://github.com/Itseez/opencv/blob/master/modules/videoio/src/cap_android.cpp#L407 and the "color conversion" at https://github.com/Itseez/opencv /blob/master/modules/videoio/src/cap_android.cpp#L511 . I agree that this behavior is not documented at all and is very inconvenient, but it saved me a lot of processor time.

+3
source

All Articles