opencv VideoCapture.set灰度?

时间:2012-06-22 15:39:30

标签: opencv

我会避免使用cvtColor(frame, image, CV_RGB2GRAY);来转换摄像机拍摄的每个帧 无论如何设置VideoCapture直接进入灰度?

示例:

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); //< ???

3 个答案:

答案 0 :(得分:6)

如果您的相机支持YUV420,那么您可以选择Y频道: http://en.wikipedia.org/wiki/YUV

如何做到这一点在这里得到了很好的解释: Access to each separate channel in OpenCV

警告:Y通道可能不是您使用split()获得的第一个Mat,因此您应该单独执行所有这些的imshow()并选择看起来像“真实”灰色图像的那个。其他人只会出于对比,所以这很明显。对我来说这是第二个垫子。

通常,任何相机都应该能够进行YUV420,因为直接用RGB发送帧速度较慢,因此几乎所有相机都使用YUV。 :)

答案 1 :(得分:4)

这是不可能的。这是所有代码的列表:

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).

或者(如果可能,使用某些实用程序)您可以将相机设置为仅显示灰度图像。

要将彩色图像转换为灰度,您必须使用代码CV_BGR2GRAY调用cvtColor。这不应该花费太多时间。

答案 2 :(得分:3)

如果您使用v4l(桌面Linux上的默认cv捕获方法),则无法执行此操作。 CV_CAP_PROP_FORMAT存在但却被忽略了。您必须手动将图像转换为灰度。如果您的设备支持它,您可能需要重新实现cap_v4l.cpp以便接口v4l以将格式设置为灰度。

在Android上,可以使用以下本机代码(对于第0个设备):

#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);

此处,将CV_CAP_ANDROID_GREY_FRAME传递给channel的{​​{1}}参数会导致YUV NV21(a.k.a yuv420sp)图像颜色转换为灰度。这只是Y通道到灰度图像的映射,它不涉及任何实际转换或cv::VideoCapture::retrieve(cv::Mat,int),因此速度非常快。您可以在https://github.com/Itseez/opencv/blob/master/modules/videoio/src/cap_android.cpp#L407和&#34;颜色转换&#34;中检查此行为在https://github.com/Itseez/opencv/blob/master/modules/videoio/src/cap_android.cpp#L511。我同意这种行为根本没有记录,并且非常尴尬,但它为我节省了大量的CPU时间。