无法使用OpenCV从辅助网络摄像机读取VideoCapture中的帧 [英] Unable to read frames from VideoCapture from secondary webcam with OpenCV

查看:3645
本文介绍了无法使用OpenCV从辅助网络摄像机读取VideoCapture中的帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:



与主要网络摄像机(设备0)完美配合的简单示例:

  VideoCapture cap(0); 

if(!cap.isOpened()){
std :: cout< 无法从指定设备读取流。 << std :: endl;
return;
}

while(true)
{
//检索框架:
Mat frame;
if(!cap.read(frame)){
std :: cout< 无法从视频流中检索帧。 << std :: endl;
break;
}
//显示它:
imshow(MyVideo,frame);

//检查是否按下了Esc:
if(waitKey(1)== 27){
break;
}
// else continue:
}

cap.release();



问题:



网络摄像头,我想使用。但是,当我用 VideoCapture cap(0); 替换 VideoCapture cap(1); 正在正确打开(或至少 cap.isOpened()返回 true cap.read(frame)调用返回 false ,我找不到原因。



我试过的:




  • 尝试使用 VideoCapture 的设置稍微调用:

      cap.set(CV_CAP_PROP_FORMAT,CV_8UC3); 

    和随机的东西,但似乎没有什么帮助。


  • 我还发现这样: VideoCapture :: read在未压缩的视频上失败(Bug#2281),这似乎是在2.4.7版本上解决..但我只是更新了OpenCV到2.4.8,它仍然不工作...


  • 我试图使用AMCap从这台摄像机捕获原始视频,保存为 aaa.avi 文件并构造 VideoCapture

      VideoCapture cap(aaa.avi); 

    它工作原理(从文件读取)...我需要的是实时处理与实时视图。




HW,操作系统,SW详细信息:



HW:HP ProBook 4510s内置网络摄像头,总是工作正常

+外部网络摄像头CANYON CNR-FWCII3,由操作系统称为USB视频设备(麻烦的一个)
操作系统,SW :Windows 8.1 Pro x86,Visual Studio 2012 Pro,OpenCV 2.4.8〜使用vc11 build



问题:




  1. 我错过了什么?

  2. 还有什么我能做的吗?


...在这种情况下,OpenCV的API似乎相当差在那里人们似乎面临类似的问题,有人声称它是OS / HW depnendant作为借口。



任何帮助将不胜感激。经过一段时间后,我发现它总是只有第一次调用 read

c $ c>失败,并跳过第一帧开始工作正常,虽然这种行为的真正原因仍然未知。



后来 James Barnett (见上面的注释)指出,原因可能是需要一段时间,直到相机准备捕获,我的当前解决方案看起来(C ++ 11的睡眠):

  #include< chrono> 
#include< thread>
...

VideoCapture cap(1);

//给相机一些额外的时间准备:
std :: this_thread :: sleep_for(std :: chrono :: milliseconds(200));

if(!cap.isOpened()){
std :: cout< 无法从指定设备读取流。 << std :: endl;
return;
}

while(true)
{
//检索框架:
Mat frame;
if(!cap.read(frame)){
std :: cout< 无法从视频流中检索帧。 << std :: endl;
continue;
}

//显示它:
imshow(LiveStream,frame);

//如果Esc被按下则停止:
if(waitKey(1)== 27){
break;
}
}

cap.release();希望未来的某些访问者会发现它有帮助:)


Code:

Simple example that works perfectly with primary webcam (device 0):

VideoCapture cap(0);

if (!cap.isOpened()) {
     std::cout << "Unable to read stream from specified device." << std::endl;
     return;
}

while (true)
{
    // retrieve the frame:
    Mat frame;
    if (!cap.read(frame)) {
        std::cout << "Unable to retrieve frame from video stream." << std::endl;
        break;
    }
    // display it:
    imshow("MyVideo", frame);

    // check if Esc has been pressed:
    if (waitKey(1) == 27) {
        break;
    }
    // else continue:
}

cap.release();

Problem:

I have a second webcam, which I'd like to use. However, when I replace VideoCapture cap(0); with VideoCapture cap(1);, the stream is being opened correctly (or at least cap.isOpened() returns true) but the cap.read(frame) call returns false and I'm unable to find out why.

What I've tried:

  • I've been trying to play with VideoCapture's settings a bit like calling:

    cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
    

    and random stuff like that, but nothing seems to help.

  • I've also found this: VideoCapture::read fails on uncompressed video (Bug #2281), which seems to be solved on version 2.4.7.. but I've just updated OpenCV to 2.4.8 and it still doesn't work...

  • I've tried to use the AMCap to capture the raw video from this camera, save it as aaa.avi file and constructed VideoCapture by calling:

    VideoCapture cap("aaa.avi");
    

    and it works (while being read from file)... what I need is real-time processing with live view though.

HW, OS, SW details:

My HW: HP ProBook 4510s with built-in webcam that always works perfectly
+ external webcam CANYON CNR-FWCII3, refered by OS as "USB Video Device" (the troublesome one) OS, SW: Windows 8.1 Pro x86, Visual Studio 2012 Pro, OpenCV 2.4.8 ~ using vc11 build

Questions:

  1. Am I missing something?
  2. Is there anything else that I could do?
  3. Is there at least any way how to retrieve some additional information about what the problem might actually be?

... OpenCV's API seems quite poor in this case and everywhere where people seemed to be facing the similar issue, there was someone claiming it to be "OS / HW depnendant" as an excuse.

Any help will be appreciated.

解决方案

After some time I've found out that it is always only the first call of read that fails and skipping the first frame started to work fine although the true reason of this behavior remained unknown.

Later James Barnett (see comments above) has pointed out that the reason might be that it takes a while till the camera gets ready for capturing and my current solution looks the following way (C++11's sleep):

#include <chrono>
#include <thread>
...

VideoCapture cap(1);

// give camera some extra time to get ready:
std::this_thread::sleep_for(std::chrono::milliseconds(200));

if (!cap.isOpened()) {
     std::cout << "Unable to read stream from specified device." << std::endl;
     return;
}

while (true)
{
    // retrieve the frame:
    Mat frame;
    if (!cap.read(frame)) {
        std::cout << "Unable to retrieve frame from video stream." << std::endl;
        continue;
    }

    // display it:
    imshow("LiveStream", frame);

    // stop if Esc has been pressed:
    if (waitKey(1) == 27) {
        break;
    }
}

cap.release();

Hopefully some future visitors will find it helpful :)

这篇关于无法使用OpenCV从辅助网络摄像机读取VideoCapture中的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆