无法从带有 OpenCV 的辅助网络摄像头的 VideoCapture 读取帧
与主网络摄像头(设备 0)完美配合的简单示例:
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();
问题:
我有第二个网络摄像头,我想使用它.但是,当我将 VideoCapture cap(0);
替换为 VideoCapture cap(1);
时,流被正确打开(或至少cap.isOpened()
返回 true
) 但是 cap.read(frame)
调用返回 false
,我无法找出原因.
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.
我一直在尝试使用
VideoCapture
的设置,有点像调用:
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.
我也发现了这个:VideoCapture::read 在未压缩的视频上失败(错误 #2281),这似乎在 2.4.7 版本上解决了.. 但我刚刚将 OpenCV 更新到 2.4.8,但它仍然不起作用......
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...
我尝试使用 AMCap 从该相机捕获原始视频,将其保存为 aaa.avi
文件并通过调用构造 VideoCapture
:
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.
我的硬件:HP ProBook 4510s 内置网络摄像头,始终完美运行
+ 外部网络摄像头 CANYON CNR-FWCII3,被操作系统称为USB 视频设备"(麻烦的那个)操作系统、软件:Windows 8.1 Pro x86、Visual Studio 2012 Pro、OpenCV 2.4.8 ~ 使用 vc11 构建
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
- 我是不是遗漏了什么?
- 我还能做些什么吗?
- 至少有什么方法可以检索一些关于问题实际可能存在的附加信息吗?
...在这种情况下,OpenCV 的 API 似乎很糟糕,而且在人们似乎面临类似问题的任何地方,都有人声称它是操作系统/硬件依赖"作为借口.
... 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.
任何帮助将不胜感激.
推荐答案
一段时间后我发现总是只有第一次调用 read
失败并跳过第一帧开始尽管这种行为的真正原因仍然未知,但仍能正常工作.
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.
后来James Barnett(见上面的评论)指出原因可能是需要一段时间直到相机准备好捕捉并且我当前的解决方案如下所示(C++11 的睡眠):
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 :)
相关文章