c++ - VideoCapture 使用 OpenCV 3.0.0-rc1 选择超时

标签 c++ opencv webcam video-capture webcam-capture

我在带有 Windows 8 主机的 VirtualBox 中的 Ubuntu 14.04 LTS Guest 上使用 OpenCV 3.0.0-rc1。我有一个非常简单的程序可以从网络摄像头 (Logitech C170) 读取帧(来自 OpenCV 文档)。不幸的是,它不起作用(我尝试了 3 种不同的网络摄像头)。它每隔几秒抛出一个错误“选择超时”并读取一个帧,但该帧是黑色的。有什么想法吗?

代码如下:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace std;
using namespace cv;

// Main
int main(int argc, char **argv) {

/* webcam setup */
VideoCapture stream;
stream.open(0);
// check if video device has been initialized
if (!stream.isOpened()) { 
        fprintf(stderr, "Could not open Webcam device");  
        return -1;
    }
    int image_width = 640; // image resolution
    int image_height = 480;
    Mat colorImage,currentImage;
    bool loop = true;
    /* infinite loop for video stream */
    while (loop) {
        loop = stream.read(colorImage);  // read webcam stream
        cvtColor(colorImage, currentImage, CV_BGR2GRAY); // color to gray for current image   
        imshow("Matches", currentImage);
        if(waitKey(30) >= 0) break;
        // end stream while-loop
    }
    return 0;
}

最佳答案

我发现了问题:使用网络摄像头时,请确保使用 Devices->Webcams 而不是 Devices->USB 将其连接到虚拟机。即使网络摄像头在通过 Devices->USB 连接时被检测为 video0,但由于某些原因它无法工作。

关于c++ - VideoCapture 使用 OpenCV 3.0.0-rc1 选择超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35909821/

相关文章:

c++ - 在 box2d 中实现 "begin contact"

visual-c++ - OpenCV 2.3.1 和 OpenNI Kinect

android - 如何使用 OpenCV 在 android 上连接到外部 wifi 摄像头?

image-processing - 如何在 Java 中从实时网络摄像头进行面部识别?

c++ - SVM 训练矩阵每行的长度

c++ - VexCL、Thrust 和 Boost.Compute 的区别

c++ - 如何创建一个以另一个模板为模板的模板化类

python - Pyspark UDF获取openCV问题描述符的问题

javascript - react-webcam(npm 包)视频大小

html - 使用 GWT 使用用户的网络摄像头捕获视频/图像