OpenCV:isOpened()总是失败

时间:2018-05-02 04:17:18

标签: c++ opencv

我正在使用OpenCV的第一步,我正在尝试运行这段代码。它应该在新窗口中打开指定的视频并等待用户按ESC。我尝试将相对和绝对路径传递给VideoCapture,但VideoCapture :: isOpened()总是失败。为什么会这样?

如果我将0传递给VideoCapture并且不调用isOpened(),那么我会得到一个漂亮的小窗口。

请注意,我使用的是VS15和OpenCV 2.4(带有x86库)

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
   VideoCapture cap(path_to_video); // open the video file
   // VideoCapture cap(0);

   if(!cap.isOpened())  // check if we succeeded
       return -1;

   namedWindow("Video",1);

   for(;;)
   {
       Mat frame;
       cap >> frame; // get a new frame from camera        
       imshow("Video", frame);
       if(waitKey(30) >= 0) break;
   }

   return 0;
}
编辑:我通过重新安装OpenCV并创建一个新的Visual Studio项目来解决这个问题。上面的代码奇迹般地开始工作。

1 个答案:

答案 0 :(得分:-1)

  

如果我将0传递给VideoCapture并且不调用isOpened(),那么我得到一个   漂亮的小窗口。为什么会这样?

因为VideoCapture类有两个不同的构造函数。需要string尝试从文件中读取的文件。采用整数尝试从设备读取的那个。将0传递给第二个版本指定默认设备/摄像头。

  

VideoCapture ::open¶打开视频文件或视频捕获设备   捕获

     

C ++:bool VideoCapture :: open(const string& filename)
  C ++:bool VideoCapture :: open(int device)

     

参数:
  filename - 已打开视频文件的名称(例如video.avi)   或图像序列(例如,img_%02d.jpg,它将读取像   img_00.jpg,img_01.jpg,img_02.jpg,...)

     

打开的视频捕获设备的设备ID(即摄像机索引)。