OpenCV C ++视频捕获似乎不工作 [英] OpenCV C++ Video Capture does not seem to work

查看:114
本文介绍了OpenCV C ++视频捕获似乎不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a Mac OS X 10.6 machine. I have OpenCV 2.1 x64 compiled from source using Xcode and its GCC compiler.

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

I am having trouble using the C++ video reading features of OpenCV. Here is the simple test code I am using (came straight from OpenCV documentation):

#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;
}

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

The program compiles fine, but when I try to run it, I see the green light on my webcam come on for a few seconds, then the program exits with the error message:

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 >>框线后仍然为空。

Under debug mode, the matrix still seems to be empty after the cap >> frame line.

当我尝试从视频文件或图像捕获时,我得到了类似的行为,所以它不是相机。什么是错的,你认为吗?我可以做什么,使这项工作?

I get similar behavior when I try to capture from a video file or an image, so it's not the camera. What is wrong, do you think? Anything I can do to make this work?

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

I'd like to add that if I use the C features, everything works fine. But I would like to stick with C++ if I can.

感谢

推荐答案

当我使用C的功能,有时类似的问题也出来了。从C代码的错误消息,我认为它发生,因为相机有一个NULL帧。所以我认为可以这样解决:

I've seen the same problem. When I use the C features, sometimes the similar question also comes up. From the error message of the C code, I think it happened because the camera got a NULL frame. So I think it can be solved in this way:

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

这样它在我的机器上工作。

That way it works on my machine.

这篇关于OpenCV C ++视频捕获似乎不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆