// 视频存为图片.cpp : 定义控制台应用程序的入口点。
//
/*=========================================================================
名称:视频保存为图片
时间:2013.08
说明:把读取视频保存为图片形式
=========================================================================*/
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
char filename[100]; //声明一个字符型数组,用来存放图片命名
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *frame; //声明一个图像指针,用来存放下一帧图像
CvCapture *capture; //声明一个视频抽象接口
capture = cvCreateFileCapture("E://3.avi");//加载本地视频
cvNamedWindow("capture"); //声明一个播放视频窗口
int frames = (int)cvGetCaptureProperty( //
capture,
CV_CAP_PROP_FRAME_COUNT
); //获取视频总帧数
int i=0;
while(1)
{
frame = cvQueryFrame(capture);//获取视频下一帧
if( !frame) //如果读取识别则停止
break;
sprintf(filename, "%s%d%s", "frame", i++, ".jpg");//保存的图片名
cvSaveImage(filename, frame);
cvShowImage("capture", frame);
char c = cvWaitKey(33);
if( c == 27)
break;
}
return 0;
}
此段代码来自http://lib.csdn.net/article/opencv/30225
相关的内容有http://blog.csdn.net/ss19890125/article/details/26257863
另外这个代码也可以http://blog.csdn.net/zhubenfulovepoem/article/details/9008387
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
using namespace std;
/*
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
char filename[100]; //声明一个字符型数组,用来存放图片命名
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *frame; //声明一个图像指针,用来存放下一帧图像
CvCapture *capture; //声明一个视频抽象接口
capture = cvCreateFileCapture("E://3.avi");//加载本地视频
cvNamedWindow("capture"); //声明一个播放视频窗口
int frames = (int)cvGetCaptureProperty( //
capture,
CV_CAP_PROP_FRAME_COUNT
); //获取视频总帧数
int i=0;
while(1)
{
frame = cvQueryFrame(capture);//获取视频下一帧
if( !frame) //如果读取识别则停止
break;
sprintf(filename, "%s%d%s", "frame", i++, ".jpg");//保存的图片名
cvSaveImage(filename, frame);
cvShowImage("capture", frame);
char c = cvWaitKey(33);
if( c == 27)
break;
}
return 0;
}
*/
void Video_to_image(char* filename)
{
printf("------------- video to image ... ----------------n");
//初始化一个视频文件捕捉器
CvCapture* capture = cvCaptureFromAVI(filename);
//此时下一帧待取的视频帧序号为0
int pos = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);
//获取视频信息
//cvQueryFrame(capture);把第0帧图像取了以后,下一帧待取的序号为1,所以下一行代码执行完毕pos就为1
pos = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
//从文件中载入的视频高度,宽度,帧率,总帧数
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("\nvideo height : %d\nvideo width : %d\nfps : %d\nframe numbers : %d", frameH, frameW, fps, numFrames);
//定义和初始化变量
int i = 0;
char image_name[30];
cvNamedWindow("mainWin",CV_WINDOW_AUTOSIZE);
//读取和显示
while (1)
{
IplImage* img = 0;
//一直没有取,所以pos还是为待取的视频帧序号1
pos = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
//取了第一帧
img = cvQueryFrame(capture); //获取一帧图片
//pos为待取的帧序号,所以此行代码执行以后,pos的值变化为2
pos = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
cvShowImage("mainWin", img); //将其显示
char key = cvWaitKey(20);
sprintf(image_name, "%s%d%s", "image", i++, ".jpg");//保存的图片名
cvSaveImage(image_name, img); //保存一帧图片
//if (i == 439){
//cout << "我是439帧" << endl;
//}
if (i >= numFrames ) break;//440>440为假,再次进入循环
}
cvReleaseCapture(&capture);
cvDestroyWindow("mainWin");
}
void Image_to_video()
{
int i = 0;
IplImage * img = NULL;
char image_name[30];
printf("------------- image to video ... ----------------n");
//初始化视频编写器,参数根据实际视频文件修改
CvVideoWriter * writer = 0;
int isColor = 1;
int fps = 30; // or 25,帧率
int frameW = 400; // 744 for firewire cameras
int frameH = 240; // 480 for firewire cameras
writer = cvCreateVideoWriter("out.avi", CV_FOURCC('X', 'V', 'I', 'D'), fps, cvSize(frameW, frameH), isColor);
printf("video height : %d\tvideo width : %d\tfps : %d\n", frameH, frameW, fps);
//创建窗口
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
while (i<440)
{
sprintf(image_name, "%s%d%s", "image", ++i, ".jpg");
img = cvLoadImage(image_name);
if (!img)
{
printf("Could not load image file...n");
exit(0);
}
cvShowImage("mainWin", img);
char key = cvWaitKey(20);
cvWriteFrame(writer, img);
}
cvReleaseVideoWriter(&writer);
cvDestroyWindow("mainWin");
}
int main(int argc, char *argv[])
{
char filename[20] = "highwayI_raw.avi";
Video_to_image(filename); //视频转图片
return 0;
}