无法通过OpenCV C ++播放视频

时间:2016-12-30 00:19:45

标签: c++ opencv c++11 video

所以我想让OpenCV为我播放Megamind.avi视频,但每次运行我的代码都会给我这个错误:

startup

每当我运行样本OpenCV video reading and writing program时也会发生这种情况,所以我不确定是否有什么东西搞砸了,或者OpenCV是否有一些改变了某些方法。 这是我的参考代码:

2016-12-29 19:07:39.916 process[17091:382843] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff92e800db __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffa7b12a2a objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff92d9c4fb -[__NSArrayM objectAtIndex:] + 203
    3   libopencv_highgui.2.4.dylib         0x000000010b2f0270 _ZN13CvCaptureFileC2EPKc + 350
    4   libopencv_highgui.2.4.dylib         0x000000010b2eece2 _Z32cvCreateFileCapture_AVFoundationPKc + 34
    5   libopencv_highgui.2.4.dylib         0x000000010b2e27de cvCreateFileCapture + 14
    6   libopencv_highgui.2.4.dylib         0x000000010b2e2a8e _ZN2cv12VideoCapture4openERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 64
    7   libopencv_highgui.2.4.dylib         0x000000010b2e28ee _ZN2cv12VideoCaptureC2ERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 42
    8   process                             0x000000010adcd517 main + 215
    9   libdyld.dylib                       0x00007fffa83f4255 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

此外,如果我将程序应用于来自同一个github repo的vtest.avi视频文件,则会返回此错误。

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

using namespace std;
using namespace cv;

int main()
{
  const string source = "Megamind.avi";
  VideoCapture inputVideo(source);
  if (!inputVideo.isOpened())
  {
    cout  << "Could not open the input video" << source << endl;
    return -1;
  }
  cout  << "read!" << endl;
  namedWindow("Video",1);
  for(;;){
      Mat frame;
      inputVideo >> frame; // get a new frame from camera
      imshow("Video", frame);
      if(waitKey(30) >= 0) break;
  }
  VideoWriter outputVideo;
  return 0;
}

我在线阅读在for循环中添加if语句:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/opencv-20161020-7399-1yrk4nm/opencv-2.4.13.1/modules/highgui/src/window.cpp, line 261
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv-20161020-7399-1yrk4nm/opencv-2.4.13.1/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow

但它只会导致一个空窗口。不确定这是否会改变我的问题。

0 个答案:

没有答案