VideoCapture使用OpenCV 3.0.0-rc1选择超时

时间:2016-03-10 07:08:30

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

我在使用Windows 8 Host的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;
}

1 个答案:

答案 0 :(得分:1)

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

相关问题