我建议不要使用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); //< ???
我建议不要使用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); //< ???
这是不可能的。以下是所有代码列表:
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).
或者(如果可能的话,使用一些工具),您可以设置相机仅显示灰度图像。
要将彩色图像转换为灰度图像,您需要调用 cvtColor 并使用代码 CV_BGR2GRAY
。这不应该花费太多时间。
CV_CAP_PROP_FORMAT
,但它会被简单地忽略。您需要手动将图像转换为灰度。如果您的设备支持,则可能需要重新实现cap_v4l.cpp
以将v4l接口设置为灰度格式。#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
传递给cv::VideoCapture::retrieve(cv::Mat,int)
的channel
参数会导致YUV NV21(也称为yuv420sp)图像被转换为灰度图像。这只是将Y通道映射到灰度图像中,不涉及任何实际转换或memcpy
,因此非常快。您可以在https://github.com/Itseez/opencv/blob/master/modules/videoio/src/cap_android.cpp#L407中检查此行为以及https://github.com/Itseez/opencv/blob/master/modules/videoio/src/cap_android.cpp#L511中的“颜色转换”。我同意这种行为完全没有记录,并且非常笨拙,但它为我节省了大量CPU时间。RaspiCam_Cv m_rapiCamera;
使用以下代码设置您需要的任何参数:
m_rapiCamera.set(CV_CAP_PROP_FORMAT, CV_8UC1);
然后像下面这样打开流:
m_rapiCamera.open();
你只会得到一个通道。
祝你好运!