OpenCV VideoCapture根本无法从我的网络摄像头中读取

时间:2013-07-12 14:03:37

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

我在Ubuntu 13.04上使用OpenCV 2.4.6(在Acer C7 Chromebook上),我正在使用一个简单的测试程序来查看我的网络摄像头是否适用于OpenCV。它适用于Cheese和Skype,所以我知道网络摄像头本身不是问题。

这是我的代码(编译没有任何错误):

#include "opencv2/opencv.hpp"
#include <stdio.h>
#include <stdlib.h>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
  cv::VideoCapture cap;
  if(argc > 1)
    {
      cap.open(string(argv[1]));
    }
  else
    {
      cap.open(CV_CAP_ANY);
    }
  if(!cap.isOpened())
    {
      printf("Error: could not load a camera or video.\n");
    }
  Mat frame;
  namedWindow("video", 1);
  for(;;)
  {
    waitKey(20);
    cap >> frame;
  if(!frame.data)
    {
      printf("Error: no frame data.\n");
      break;
    }
    imshow("video", frame);
  }
}

如果我没有任何参数运行程序(因为我希望它使用CV_CAP_ANY),我得到了

Error: could not load a camera or video.
init done 
opengl support available 
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
Error: no frame data.

如果我指定/ dev / video0(我唯一的相机)作为参数,我得到

demux_wavpack: (open_wv_file:127) open_wv_file: non-seekable inputs aren't supported yet.
(ERROR)icvOpenAVI_XINE(): Unable to open source '/dev/video0'
init done 
opengl support available 
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
GStreamer Plugin: Embedded video playback halted; module source reported: Could not read from resource.
Error: no frame data.

如果我指定视频文件的路径作为参数,它就可以正常播放视频。

我很感激任何帮助。提前谢谢!

2 个答案:

答案 0 :(得分:3)

Opencv 2.4.6上存在错误,请查看here

此外试试这个:      cv :: VideoCapture cap = cv :: VideoCapture(0);

希望它有所帮助!

答案 1 :(得分:0)

您可以尝试将waitKey(20)值更改为20到80.某些计算机的值为20,30,40,50 ......