gpt4 book ai didi

c++ - 未处理的异常 - OpenCV - cvReleaseCapture 和 cvReleaseImage - C++

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:37 25 4
gpt4 key购买 nike

我有一个程序使用 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 循环。

最佳答案

来自 openCV 文档:

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.

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

删除:

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

cvReleaseImage( &frame );

并替换

frame = cvQueryFrame( capture );

IplImage* frame = cvQueryFrame( capture );

关于c++ - 未处理的异常 - OpenCV - cvReleaseCapture 和 cvReleaseImage - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10951016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com