要用到视频序列,目前只有jpg图片,所以参考了一些网络资料,并做修改,转化测试成功。
程序:
#include <stdlib.h> #include <stdio.h> #include <math.h> #include <cv.h> #include <highgui.h> #define NUM_FRAME 300 //只处理前300帧,根据视频帧数可修改 void Video_to_image(char* filename,const char* dirname) { 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 : %d\n\tvideo width : %d\n\tfps : %d\n\tframe numbers : %d\n", frameH, frameW, fps, numFrames); //定义和初始化变量 int i = 0; IplImage* img = 0; char image_name[130]; cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); //读取和显示 while (1) { img = cvQueryFrame(capture); //获取一帧图片 cvShowImage("mainWin", img); //将其显示 char key = cvWaitKey(20); sprintf(image_name, "%s%s%d%s", dirname,"outImage", i++, ".jpg");//保存的图片名 cvSaveImage(image_name, img); //保存一帧图片 if (i == NUM_FRAME) break; } cvReleaseCapture(&capture); cvDestroyWindow("mainWin"); } void Image_to_video(const char* in,const char* out) { int num = 1; CvSize size = cvSize(1024, 768); //视频帧格式的大小 double fps = 30; //每秒钟的帧率 CvVideoWriter *writer = cvCreateVideoWriter(out, CV_FOURCC('D', 'I', 'V', 'X'), fps, size); //创建视频文件 char cname[100]; sprintf(cname,in , num); //加载图片的文件夹,图片的名称编号是1开始1,2,3,4,5.。。。 IplImage *src = cvLoadImage(cname); if (!src) { return; } IplImage *src_resize = cvCreateImage(size, 8, 3); //创建视频文件格式大小的图片 cvNamedWindow("avi"); while (src) { cvShowImage("avi", src_resize); cvWaitKey(1); cvResize(src, src_resize); //将读取的图片设置为视频格式大小相同 cvWriteFrame(writer, src_resize); //保存图片为视频流格式 cvReleaseImage(&src); //释放空间 num++; sprintf(cname,in, num); src = cvLoadImage(cname); //循环读取数据 } cvReleaseVideoWriter(&writer); cvReleaseImage(&src_resize); } int main(int argc, char *argv[]) { char infilename[130] = "C:/Users/jiang/Desktop/output/infile.avi"; const char *dirname = "C:/Users/jiang/Desktop/output/tupian/"; //Video_to_image(infilename,dirname); //视频转图片 const char *outImagename = "C:/Users/jiang/Desktop/output/breakdancer/cam3/3pic (%d).jpg"; const char *outVideoname = "C:/Users/jiang/Desktop/output/3outfile.avi"; Image_to_video(outImagename,outVideoname ); //图片转视频 return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u012361418/article/details/47028277