你好所以我写了这段代码来从文件中捕获视频
#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);
}
每次运行时,都会出现以下错误:
CaptureVideo.cpp: In function ‘int main(int, char**)’:
CaptureVideo.cpp:13:28: error: expected primary-expression before ‘char’
任何帮助将不胜感激。
最佳答案
这是 C++ 问题,所以你应该使用 C++ 接口(interface)。
原始代码中的错误:
- 您忘记删除
cvCaptureFromAVI
中的char const*
。 - 您无需等待显示框架。
ShowImage
仅在其后跟 WaitKey 时有效。 - 我不确定 capture=NULL 是否意味着您的文件未打开。请改用
isOpened
。
我已更正您的代码并将其放入 C++ 接口(interface),因此它现在是一个合适的 C++ 代码。我的重写与您的程序一样逐行进行。
//#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <iostream>
using namespace cv;
using std::string;
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
}
关于c++ - 从文件opencv中读取视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13709274/