C++ 无法通过OpenCV C+播放视频+;
因此,我试图让OpenCV为我播放视频,但每次我运行代码时,都会出现以下错误:C++ 无法通过OpenCV C+播放视频+;,c++,opencv,c++11,video,C++,Opencv,C++11,Video,因此,我试图让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
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
每次运行示例时都会发生这种情况,因此我不确定是否有什么地方出了问题,或者OpenCV中是否有任何更改破坏了某些方法。
以下是我的代码供参考:
#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语句:
if (!frame.empty()) {
imshow("window", frame);
}
但结果只是一个空窗口。不确定这是否会改变我的问题。异常似乎表明您正在访问数组边界之外的索引。是否可能您没有使用所需的源参数调用程序?我将arg更改为实际文件的位置,但似乎没有修复任何问题。我很确定这个错误与for循环有关,因为它是导致错误的原因。
if (!frame.empty()) {
imshow("window", frame);
}