我正在尝试在OpenCV中捕获我的Logitech C920视频流。使用Labview,我可以以30fps 1080p的速度访问MJPG流。在opencv中,我只能使用5fps或640x480。
这是与相机设置相关的代码:
this->camRef.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
this->camRef.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
this->camRef.set(CV_CAP_PROP_FOURCC,CV_FOURCC('M','J','P','G'));
这些都返回1,但我得到了一条1080p的5fps流,对应于YUY2流。
如果我添加以下行:
this->camRef.set(CV_CAP_PROP_FPS, 30);
这将返回0。
我得到了一个640x480的30帧每秒的流。对我来说,看起来 MJPG 设置没有被接受,但我不知道该怎么做或如何解决。
编辑:以下操作会导致程序崩溃。
this->camRef.read(this->image);
std::cout<< this->camRef.get(CV_CAP_PROP_FOURCC) << std::endl;
std::cout<< this->camRef.set(CV_CAP_PROP_FRAME_WIDTH, config.width) << std::endl;
std::cout<< this->camRef.set(CV_CAP_PROP_FRAME_HEIGHT, config.height) << std::endl;
std::cout<< this->camRef.set(CV_CAP_PROP_FOURCC,CV_FOURCC('M','J','P','G')) << std::endl;
std::cout<< this->camRef.get(CV_CAP_PROP_FOURCC) << std::endl;
然后在我的运行代码中,我有以下内容:
void camera::run()
{
while(true)
{
if(this->camRef.read(this->image) == 0)
{
if(this->capture)
{
cv::imwrite(fileName,this->image);
this->count++;
}
}
msleep(15);
}
}
编辑2:解决方法是在设置相机高度和宽度之前设置fourCC编解码器。