#include <opencv2\opencv.hpp>
using namespace std;
#include <fstream>
int NUM_FRAME = 0;
void Video2image(char* filename)
{
printf("------------- video to image ... ----------------n");
//初始化一个视频文件捕捉器
CvCapture* capture = cvCaptureFromAVI(filename);
//获取视频信息
cvQueryFrame(capture);
int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
printf("tvideo height : %dntvideo width : %dntfps : %dntframe numbers : %dn", frameH, frameW, fps, numFrames);
//定义和初始化变量
int i = 0;
IplImage* img = 0;
NUM_FRAME = numFrames;
char image_name[13];
//读取和显示
while(1)
{
img = cvQueryFrame(capture); //获取一帧图片
cvShowImage( "mainWin", img ); //将其显示
cvWaitKey(20);
if(i<10)
{
sprintf(image_name, "%s%d%s", "image0000", ++i, ".jpg");//保存的图片名
}
else if(i<100)
{
sprintf(image_name, "%s%d%s", "image000", ++i, ".jpg");//保存的图片名
}
else if(i<1000)
{
sprintf(image_name, "%s%d%s", "image00", ++i, ".jpg");//保存的图片名
}
else if(i<10000)
{
sprintf(image_name, "%s%d%s", "image0", ++i, ".jpg");//保存的图片名
}
cvSaveImage( image_name, img); //保存一帧图片
if(i == NUM_FRAME) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("mainWin");
}
void Image2video(vector<string> fileName,int numCount)
{
int i = 0;
char image_name[13];
printf("------------- image to video ... ----------------n");
//初始化视频编写器,参数根据实际视频文件修改
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 30; // or 25
int frameW = 640; // 744 for firewire cameras
int frameH = 480; // 480 for firewire cameras
writer=cvCreateVideoWriter("out1.avi",CV_FOURCC('X','V','I','D'),fps,cvSize(frameW,frameH),isColor);
printf("tvideo height : %dntvideo width : %dntfps : %dn", frameH, frameW, fps);
NUM_FRAME = numCount;
while(i<NUM_FRAME)
{
IplImage* img = cvLoadImage(fileName[i].c_str());
cout<<i<<endl;
if(!img)
{
printf("Could not load image file...n");
exit(0);
}
cvShowImage("mainWin", img);
cvWaitKey(20);
cvWriteFrame(writer, img);
cvReleaseImage(&img);
i++;
}
cvReleaseVideoWriter(&writer);
cvDestroyWindow("mainWin");
}
int main(int argc, char *argv[])
{
char filename[13] = "out1.avi";
Video2image(filename);
//vector<string> img_path;//输入文件名变量
// vector<int> img_catg;
// int nLine = 0;
// string buf;
// ifstream svm_data( "fileName.txt" );//图片的路径都写在这个txt文件中,使用bat批处理文件可以得到这个txt文件
//float result;
// unsigned long n;
// while( svm_data )//将训练样本文件依次读取进来
// {
// if( getline( svm_data, buf ) )
// {
// img_path.push_back( buf );//图像路径
// }
// }
// svm_data.close();//关闭文件
// Image2video(img_path,1301);
return 0;
}