Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV C++;视频捕获似乎不起作用_C++_Macos_Opencv - Fatal编程技术网

C++ OpenCV C++;视频捕获似乎不起作用

C++ OpenCV C++;视频捕获似乎不起作用,c++,macos,opencv,C++,Macos,Opencv,我使用的是MacOSX10.6机器。我使用Xcode及其GCC编译器从源代码处编译了OpenCV 2.1 x64 我在使用OpenCV的C++视频读取功能时遇到了困难。下面是我正在使用的简单测试代码(直接来自OpenCV文档): 程序编译得很好,但当我尝试运行它时,我看到网络摄像头上的绿灯亮了几秒钟,然后程序退出并显示错误消息: OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported a

我使用的是MacOSX10.6机器。我使用Xcode及其GCC编译器从源代码处编译了OpenCV 2.1 x64

我在使用OpenCV的C++视频读取功能时遇到了困难。下面是我正在使用的简单测试代码(直接来自OpenCV文档):

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

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++。p>
谢谢

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

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


一旦确定了有问题的行,您就可以集中精力进行进一步调查。

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


在附加依赖项粘贴中,我看到了相同的问题。当我使用C特性时,有时也会出现类似的问题。从C代码的错误信息来看,我认为这是因为相机得到了一个空帧。所以我认为可以这样解决:

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

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

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


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

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;

}
int-cameraNumber=0;
如果(argc>1)
cameraNumber=atoi(argv[1]);
摄像机;
照相机。打开(照相机编号);
如果(!camera.isOpened()){
cerr摄像机框架;
如果(cameraFrame.total()>0){
Mat displayFrame(cameraFrame.size(),CV_8UC3);
剂量测量(摄像机框、显示框);
imshow(“图像”,显示框);
}否则{

我是否可以缩小问题的范围,cap>>框架似乎没有将一个框架抓取到矩阵中。在cap>>框架之后,矩阵仍然是空的
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;
        }
    }
} 
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;

}