3

尝试使用 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 错误:cvGetMat 中的空指针(传递了空数组指针),文件 /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.6.1/modules/core/src/array.cpp,行2382 libc++abi.dylib:终止调用抛出异常(lldb)

它是什么错误,我该如何解决?

4

1 回答 1

0

首先,您应该检查索引 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
}

希望这可以帮助

于 2013-09-04T03:39:58.937 回答