【问题标题】:OpenCV C++ Video Capture does not seem to workOpenCV C++ 视频捕获似乎不起作用
【发布时间】:2011-04-25 20:13:06
【问题描述】:

我使用的是 Mac OS X 10.6 机器。我使用 Xcode 及其 GCC 编译器从源代码编译 OpenCV 2.1 x64。

我在使用 OpenCV 的 C++ 视频阅读功能时遇到问题。这是我正在使用的简单测试代码(直接来自 OpenCV 文档):

#include "cv.h"
#include "highgui.h"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(200) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

程序编译正常,但是当我尝试运行它时,我看到网络摄像头上的绿灯亮了几秒钟,然后程序退出并显示错误消息:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /Users/mark/Downloads/OpenCV-2.1.0/src/cxcore/cxarray.cpp, line 2476
terminate called after throwing an instance of 'cv::Exception'
  what():  /Users/mark/Downloads/OpenCV-2.1.0/src/cxcore/cxarray.cpp:2476: error: (-206) Unrecognized or unsupported array type in function cvGetMat

在调试模式下,在 cap >> 框架行之后,矩阵似乎仍然是空的。

当我尝试从视频文件或图像中捕捉时,我得到了类似的行为,所以它不是相机。你觉得有什么问题吗?我能做些什么来完成这项工作?

编辑:我想补充一点,如果我使用 C 功能,一切正常。但如果可以的话,我想坚持使用 C++。

谢谢

【问题讨论】:

    标签: c++ macos opencv


    【解决方案1】:

    尝试简化程序,以便确定问题的确切位置,例如改变你的循环,使它看起来像这样:

    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
    //  cvtColor(frame, edges, CV_BGR2GRAY);
    //  GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    //  Canny(edges, edges, 0, 30, 3);
    //  imshow("edges", edges);
        imshow("edges", frame);
        if(waitKey(200) >= 0) break;
    }
    

    如果一切正常,请尝试一次添加一个处理调用,例如

    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
    //  GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    //  Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(200) >= 0) break;
    }
    

    等等……

    一旦您确定了有问题的线路,您就可以专注于该线路并进一步调查。

    【讨论】:

    • 我已经缩小了问题的范围,cap >> frame 似乎没有将帧抓取到矩阵中。在 cap >> frame 之后矩阵仍然是空的
    【解决方案2】:

    转到project->project properties->configuration properties->linker->input

    在附加依赖中粘贴cv210.lib cvaux210.lib cxcore210.lib highgui210.lib

    【讨论】:

      【解决方案3】:

      我也遇到过同样的问题。当我使用 C 特性时,有时也会出现类似的问题。从 C 代码的错误信息来看,我认为这是因为相机得到了一个 NULL 帧。所以我觉得可以这样解决:

      do
      {
          capture>>frame;
      }while(frame.empty());
      

      这样它就可以在我的机器上运行。

      【讨论】:

        【解决方案4】:

        您好,我为您找到了解决方案:)

        VideoCapture san_cap(0);
        if (san_cap.isOpened()) {
            while (1) {
        
        
        
                san_cap.read(san);
        
                imshow("Video", san);
        
                Mat frame;
                san_cap.read(frame);      // get a new frame from camera
                cvtColor(frame, edges, CV_BGR2GRAY);
        
                imshow("Video2", edges);
        
        
        
                int key = cv::waitKey(waitKeyValue);
        
                if (key == 27 ) {
                    break;
                }
            }
        } 
        

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题,似乎前两次尝试获取视频都不会返回任何信号,所以如果你尝试使用它,你会得到一个错误,这是我解决这个问题的方法,只需添加计数器并检查视频的大小。

          int cameraNumber = 0;
          if ( argc > 1 )
              cameraNumber = atoi(argv[1]);
          
          cv::VideoCapture camera;
          camera.open(cameraNumber);
          if ( !camera.isOpened() ) {
              cerr << "ERROR: Could not access the camera or video!" << endl;
              exit(1);
          }
          
          //give the camera 40 frames attempt to get the camera object, 
          //if it fails after X (40) attemts the app will terminatet, 
          //till then it will display 'Accessing camera' note;
          
          int CAMERA_CHECK_ITERATIONS = 40;
          while (true) {
          
              Mat cameraFrame;
              camera >> cameraFrame;
              if ( cameraFrame.total() > 0 ) {
                  Mat displayFrame( cameraFrame.size(), CV_8UC3 );
                  doSomething( cameraFrame, displayFrame );
                  imshow("Image", displayFrame );
              } else {
                  cout << "::: Accessing camera :::" << endl;
                  if ( CAMERA_CHECK_ITERATIONS > 0 ) CAMERA_CHECK_ITERATIONS--;
                  if ( CAMERA_CHECK_ITERATIONS < 0 ) break;
              }
          
          
              int key = waitKey(200);
              if (key == 27) break;
          
          }
          

          【讨论】:

            猜你喜欢
            • 2022-11-03
            • 1970-01-01
            • 1970-01-01
            • 2020-11-28
            • 2016-01-09
            • 1970-01-01
            • 2011-09-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多