【问题标题】:Unhandled exception - OpenCV - cvReleaseCapture and cvReleaseImage - C++未处理的异常 - OpenCV - cvReleaseCapture 和 cvReleaseImage - C++
【发布时间】:2012-06-12 15:23:09
【问题描述】:

我有一个程序使用 OpenCV 库(版本 2.4.1)从笔记本电脑的网络摄像头(或任何其他连接的摄像头)捕获视频并将其保存为 .avi 文件。当我在 Visual Studio 2010 中调试时,在程序的最后,当 CvCapture 或 IplImage 被释放时,我得到一个未处理的异常。代码如下:

    // WriteRealTimeCapturedVideo.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    #include "cv.h"
    #include "highgui.h"
    #include <stdio.h>

    int main()
    {
        CvCapture* capture = cvCaptureFromCAM( 1 ); //CV_CAP_ANY
        if ( !capture )
        {
            fprintf( stderr, "ERROR: capture is NULL \n" );
            getchar();
            return -1;
        }
        // Create a window in which the captured images will be presented
        cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

        double fps = cvGetCaptureProperty (capture, CV_CAP_PROP_FPS);

        CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));

        #ifndef NOWRITE
        CvVideoWriter* writer = cvCreateVideoWriter("Capture.avi", CV_FOURCC('M','J','P','G'), fps, size); //CV_FOURCC('M','J','P','G')
        #endif

        int width = (int)(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH));
        int height = (int)(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));

        IplImage* frame = cvCreateImage( cvSize( width,height ), IPL_DEPTH_8U, 1);

        while ( 1 )
        {
            // Get one frame
            frame = cvQueryFrame( capture );
            if ( !frame ) 
            {
                fprintf( stderr, "ERROR: frame is null...\n" );
                getchar();
                break;
            }
            cvShowImage( "mywindow", frame );
            #ifndef NOWRITE
            cvWriteToAVI( writer, frame );
            #endif
            char c = cvWaitKey(33);
            if( c == 27 ) break;
        }
        #ifndef NOWRITE
        cvReleaseVideoWriter( &writer );
        #endif
        cvDestroyWindow( "mywindow" );
        cvReleaseImage( &frame );
        cvReleaseCapture( &capture );
        return 0;
    }

我发现我必须将 tbb.dll 和 tbb_debug.dll 放在与源代码(.cpp 文件)相同的目录中,程序才能运行。这些 dll 可以从 Intel 下载。

视频捕获工作,即窗口出现并显示视频,但无论我如何重新排列发布声明,都会出现异常。如果我删除了发布声明(除了 VideoWriter),我不会得到异常,但是生成的 .avi 文件无法打开。当用户按下 Esc 键时,程序退出 while 循环。

【问题讨论】:

    标签: c++ exception opencv iplimage unhandled


    【解决方案1】:

    来自 openCV 文档:

    cvQueryFrame

    从相机或文件中抓取并返回帧

    IplImage* cvQueryFrame(CvCapture* 捕获);

    捕获 视频采集结构。

    函数 cvQueryFrame 从相机或视频文件中抓取一帧, 解压并返回。这个功能只是一个组合 一次调用 cvGrabFrame 和 cvRetrieveFrame。 返回的图片应该 用户不得发布或修改。

    所以你不必分配或释放“框架”

    删除:

    IplImage* frame = cvCreateImage( cvSize( width,height ), IPL_DEPTH_8U, 1);
    

    cvReleaseImage( &frame );
    

    替换

    frame = cvQueryFrame( capture );
    

    IplImage* frame = cvQueryFrame( capture );
    

    【讨论】:

    • 谢谢。现在可以了。我还必须将 fps 更改为恒定值 (30.0) 才能从文件中播放捕获的视频。 IplImage* frame = cvQueryFrame( capture ); 行是否为每一帧分配新内存?这个内存什么时候释放?
    • IplImage* frame = cvQueryFrame(capture);不会分配内存,这将返回一个指向捕获缓冲区中的图像之一的指针。如果您想对从相机获取的图像进行任何处理,您应该创建“帧”的副本。如果您只想显示或保存图像,则无需创建副本。
    【解决方案2】:

    我认为这条线导致了问题

    IplImage* frame = cvCreateImage( cvSize( width,height ), IPL_DEPTH_8U, 1);
    

    尝试其他代码,例如

    IplImage* frame = cvCreateImage( cvSize( width,height ), IPL_DEPTH_8U, 3);
    

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 2021-03-08
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多