尝试使用 opencv 从网络摄像头捕获视频时出现此错误。这是我正在使用的代码:
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main( int argc, const char** argv ) {
CvCapture *capture = cvCaptureFromCAM(0);
IplImage *frame;
cvNamedWindow("test");
while ( 1)
{
frame = cvQueryFrame(capture) ;
cvShowImage("test", frame);
int key = cvWaitKey(1);
if ( key == 27 ) break; // ESC key was pressed
}
// Memory deallocation
cvReleaseCapture(&capture);
cvDestroyWindow("test");
return 0;
}
错误发生在 cvShowImage("text", frame):
OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.6.1/modules/core/src/array.cpp, line 2382 libc++abi.dylib: terminate called throwing an exception (lldb)
这是什么错误,我该如何解决?
最佳答案
首先你应该检查索引0处是否有设备,像这样
if (!capture)
{
// print error and exit
cout << "ERROR: Capture is null!\n";
return -1;
}
其次:
while ( capture)
{
frame = cvQueryFrame(capture) ;
cvShowImage("test", frame);
int key = cvWaitKey(1);
if ( key == 27 ) break; // ESC key was pressed
}
希望对你有帮助
关于c++ - Opencv:cvGetMat 中的空指针(传递空数组指针),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18604793/