从文件opencv读取视频 [英] reading video from file opencv
问题描述
Hi所以我写了这段代码来捕获文件中的视频
Hi So I have written this code to capture a video from file
#include <stdio.h>
#include <cv.h>
#include "highgui.h"
#include <iostream>
//using namespace cv
int main(int argc, char** argv)
{
CvCapture* capture=0;
IplImage* frame=0;
capture = cvCaptureFromAVI(char const* filename); // read AVI video
if( !capture )
throw "Error when reading steam_avi";
cvNamedWindow( "w", 1);
for( ; ; )
{
frame = cvQueryFrame( capture );
if(!frame)
break;
cvShowImage("w", frame);
}
cvWaitKey(0); // key press to close window
cvDestroyWindow("w");
cvReleaseImage(&frame);
}
每次我运行它,我得到以下错误:
Everytime I run it, I get the following error:
CaptureVideo.cpp:在函数'int main(int,char **)'中:
CaptureVideo.cpp: In function ‘int main(int, char**)’:
CaptureVideo.cpp:13:28:错误:'char'之前的主表达式
CaptureVideo.cpp:13:28: error: expected primary-expression before ‘char’
任何帮助将非常感激。 p>
Any help will be much appreciated.
推荐答案
这是C ++的问题,所以你应该使用C ++接口。
This is C++ question, so you should use the C++ interface.
原始程式码中的错误:
- 您忘记移除
c $ c> c $ c> c code>
- 您不等待框架显示。
ShowImage
只有在它后面跟着WaitKey才有效。 - 我不知道如果capture = NULL意味着你的文件不是打开。
- You forgot to remove
char const*
incvCaptureFromAVI
. - You don't wait for the frame to be displayed.
ShowImage
only works if it is followed by WaitKey. - I'm not sure if capture=NULL would mean that your file was not opened. Use
isOpened
instead.
我已经更正了您的代码,并将其放入C ++接口,因此现在是一个合适的C ++代码。我的重写与您的程序一样,逐行执行。
I have corrected your code and put it into the C++ interface, so it is a proper C++ code now. My rewrite does line-by-line the same as your program did.
//#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <iostream>
using namespace cv
int main(int argc, char** argv)
{
string filename = "yourfile.avi";
VideoCapture capture(filename);
Mat frame;
if( !capture.isOpened() )
throw "Error when reading steam_avi";
namedWindow( "w", 1);
for( ; ; )
{
capture >> frame;
if(frame.empty())
break;
imshow("w", frame);
waitKey(20); // waits to display frame
}
waitKey(0); // key press to close window
// releases and window destroy are automatic in C++ interface
}
这篇关于从文件opencv读取视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!