未处理的异常 - OpenCV - cvReleaseCapture和cvReleaseImage - C ++ [英] Unhandled exception - OpenCV - cvReleaseCapture and cvReleaseImage - C++

查看:315
本文介绍了未处理的异常 - OpenCV - cvReleaseCapture和cvReleaseImage - C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  // WriteRealTimeCapturedVideo.cpp:定义控制台应用程序的入口点。 
#includestdafx.h
#includecv.h
#includehighgui.h
#include< stdio.h>

int main()
{
CvCapture * capture = cvCaptureFromCAM(1); // CV_CAP_ANY
if(!capture)
{
fprintf(stderr,ERROR:capture is NULL \\\
);
getchar();
return -1;
}
//创建一个窗口,其中将显示捕获的图像
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)
{
//获取一个框架
frame = cvQueryFrame(capture);
if(!frame)
{
fprintf(stderr,ERROR:frame is null ... \\\
);
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循环。

解决方案

从openCV文件:


cvQueryFrame



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



IplImage * cvQueryFrame(CvCapture * capture);



捕获
视频捕获结构。



函数cvQueryFrame从相机或视频文件中获取一个帧,
解压缩并返回。这个功能只是一个调用中
cvGrabFrame和cvRetrieveFrame的组合。


所以你不要不得不分配或释放框架



删除:

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

  cvReleaseImage(& frame); 

并替换

 code> frame = cvQueryFrame(capture); 

  IplImage * frame = cvQueryFrame(capture); 


I have a program that uses the OpenCV library (version 2.4.1) to capture video from my laptop's webcam (or any other connected camera) and save it to an .avi file. When I debug in Visual Studio 2010, I get an unhandled exception at the very end of the program, when either the CvCapture or the IplImage are being released. Here is the code:

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

I found that I have to have tbb.dll and tbb_debug.dll in the same directory as the source code (.cpp files) for the program to work. These dll's can be downloaded from Intel.

The video capture works, that is, the window appears and displays the video, but the exception occurs no matter how I rearrange the release statements. If I remove the release statements (except for the VideoWriter), I don't get the exception, but then the .avi file produced cannot be opened. The program exits the while loop when the user presses the Esc key.

解决方案

From openCV docu:

cvQueryFrame

Grabs and returns a frame from camera or file

IplImage* cvQueryFrame( CvCapture* capture );

capture video capturing structure.

The function cvQueryFrame grabs a frame from camera or video file, decompresses and returns it. This function is just a combination of cvGrabFrame and cvRetrieveFrame in one call. The returned image should not be released or modified by user.

So you don't have to allocate or release the "frame"

delete:

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

and

cvReleaseImage( &frame );

and replace

frame = cvQueryFrame( capture );

with

IplImage* frame = cvQueryFrame( capture );

这篇关于未处理的异常 - OpenCV - cvReleaseCapture和cvReleaseImage - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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