Opencv:cvGetMat中的空指针(传递NULL数组指针) [英] Opencv: Null pointer (NULL array pointer is passed) in cvGetMat
问题描述
我尝试使用opencv从网络摄像头捕获视频时收到此错误。这是我使用的代码:
I'm getting this error when trying to capture video from webcam using opencv. This is the code I'm using:
#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):
The error occur at cvShowImage("text", frame):
OpenCV错误:cvGetMat,
文件中的NULL指针(传递NULL数组指针)
/ 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调用抛出异常
(lldb )
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)
它是什么错误,如何解决呢?
What is it error about and how can I fix it?
推荐答案
首先,您应该检查在索引0是否有设备,例如
First you should check that whether there is a device at index 0, like this
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
}
希望这有助于
这篇关于Opencv:cvGetMat中的空指针(传递NULL数组指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!