(转)OpenCV提取视频每一帧及将连续图片合成视频
参考博文:http://blog.sina.com.cn/s/blog_60b330b801018s0u.html
运行环境:Win7 OpenCV1.0 VC6.0,下面生成视频的代码用到的是XVID编码器,如果电脑上没有需要下载,否则生成的视频文件大小为0。或者将cvCreateVideoWriter("out.avi",CV_FOURCC('X','V','I','D'),fps,cvSize(frameW,frameH),isColor)的'X','V','I','D'修改为电脑上已有的编码器即可。
注:是将多张图像合并成视频的,所以最后生成的视频是没有声音的。
1 // 该程序实现视频和图片的相互转换. 2 // Image_to_video()函数将一组图片合成AVI视频文件. 3 // Video_to_image()函数将AVI视频文件读入,将每一帧存储为jpg文件. 4 // 5 //////////////////////////////////////////////////////////////////////// 6 #include <stdlib.h> 7 #include <stdio.h> 8 #include <math.h> 9 #include <cv.h> 10 #include <highgui.h> 11 #define NUM_FRAME 300 //只处理前300帧,根据视频帧数可修改 12 13 void Video_to_image(char* filename) 14 { 15 printf("------------- video to image ... ----------------\n"); 16 //初始化一个视频文件捕捉器 17 CvCapture* capture = cvCaptureFromAVI(filename); 18 //获取视频信息 19 cvQueryFrame(capture); 20 int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); 21 int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); 22 int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); 23 int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT); 24 printf("\tvideo height : %d\n\tvideo width : %d\n\tfps : %d\n\tframe numbers : %d\n", frameH, frameW, fps, numFrames); 25 //定义和初始化变量 26 int i = 0; 27 IplImage* img = 0; 28 char image_name[13]; 29 30 cvNamedWindow( "mainWin", CV_WINDOW_AUTOSIZE ); 31 //读取和显示 32 while(1) 33 { 34 35 img = cvQueryFrame(capture); //获取一帧图片 36 cvShowImage( "mainWin", img ); //将其显示 37 char key = cvWaitKey(20); 38 39 sprintf(image_name, "%s%d%s", "image", ++i, ".jpg");//保存的图片名 40 41 cvSaveImage( image_name, img); //保存一帧图片 42 43 if(i == NUM_FRAME) break; 44 } 45 cvReleaseCapture(&capture); 46 cvDestroyWindow("mainWin"); 47 } 48 void Image_to_video() 49 { 50 int i = 0; 51 IplImage* img = 0; 52 char image_name[13]; 53 printf("------------- image to video ... ----------------\n"); 54 //初始化视频编写器,参数根据实际视频文件修改 55 CvVideoWriter *writer = 0; 56 int isColor = 1; 57 int fps = 30; // or 25 58 int frameW = 400; // 744 for firewire cameras 59 int frameH = 240; // 480 for firewire cameras 60 writer=cvCreateVideoWriter("out.avi",CV_FOURCC('X','V','I','D'),fps,cvSize(frameW,frameH),isColor); 61 printf("\tvideo height : %d\n\tvideo width : %d\n\tfps : %d\n", frameH, frameW, fps); 62 //创建窗口 63 cvNamedWindow( "mainWin", CV_WINDOW_AUTOSIZE ); 64 while(i<NUM_FRAME) 65 { 66 sprintf(image_name, "%s%d%s", "image", ++i, ".jpg"); 67 img = cvLoadImage(image_name); 68 if(!img) 69 { 70 printf("Could not load image file...\n"); 71 exit(0); 72 } 73 cvShowImage("mainWin", img); 74 char key = cvWaitKey(20); 75 cvWriteFrame(writer, img); 76 } 77 cvReleaseVideoWriter(&writer); 78 cvDestroyWindow("mainWin"); 79 } 80 81 int main(int argc, char *argv[]) 82 { 83 char filename[13] = "infile.avi"; 84 Video_to_image(filename); //视频转图片 85 Image_to_video(); //图片转视频 86 return 0; 87 } 88 //--------------------------------------------------------------------------------