1 /*g++ *.cpp `pkg-config --cflags --libs opencv` -std=c++11*/ 2 3 #include <opencv2/opencv.hpp> 4 using namespace std; 5 using namespace cv; 6 #define NUM_FRAME 300 7 #define SIZE 7 8 9 char path[100];//输入文件路径 10 11 //将图片序列转换为视频 12 void getVideo() 13 { 14 int i = 0; 15 IplImage* img = 0;//读入图像 16 IplImage* outimg = 0;//修改图像尺寸 17 char image_name[100];//图像名字 18 char videoname[100]; 19 strcpy(videoname, path); 20 strcat(videoname, "//1.avi"); 21 22 //初始化视频编写器,参数根据实际视频文件修改 23 CvVideoWriter *writer = 0; 24 //从文件读入视频 25 CvCapture* capture = cvCaptureFromAVI(videoname); 26 //读取和显示 27 int isColor = 1; 28 IplImage* frameimg;//从视频中提取的帧图像 29 int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);//视频的fps 30 int frameH = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);//视频的高度 31 int frameW = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);//视频的宽度 32 writer = cvCreateVideoWriter("..//output//2.avi",-1, fps, cvSize(frameW, frameH), isColor);//创建视频写入 33 printf("\tvideo height : %d\n\tvideo width : %d\n\tfps : %d\n", frameH, frameW, fps); 34 //创建窗口 35 cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 36 //读入图片,并制作幻灯片 37 while (i<SIZE) 38 { 39 CvSize Out_Img_size;//图片的大小 40 Out_Img_size.width = frameW;//使读入图片的大小与视频尺寸相同 41 Out_Img_size.height = frameH; 42 sprintf(image_name, "%d%s", ++i, ".jpg");//得到图片名字 43 char imgname[100]; 44 strcpy(imgname, path); 45 strcat(imgname, "//"); 46 strcat(imgname,image_name);//加上路径 47 img = cvLoadImage(imgname);//打开图片 48 outimg = cvCreateImage(Out_Img_size, img->depth, img->nChannels);//创建一张与视频帧大小相同的图像 49 cvResize(img, outimg, CV_INTER_LINEAR);//让打开的图像重写为指定大小 50 if (!img) 51 { 52 printf("Could not load image file...\n"); 53 exit(0); 54 } 55 //设置字体 56 CvFont font; 57 cvInitFont(&font, CV_FONT_VECTOR0, 0.5f, 0.5f, 0, 1, 8); 58 //在图像中显示文本字符串 59 cvPutText(outimg, "StudentID", cvPoint(frameW - 100, frameH - 60), &font, CV_RGB(255, 255, 255)); 60 cvPutText(outimg, "name", cvPoint(frameW - 100, frameH - 30), &font, CV_RGB(255, 255, 255)); 61 char key = cvWaitKey(20);//等待 62 //将图像写入视频,重复25次是为了使之放映速度变慢 63 for (int j = 0; j < 25; j++) { 64 cvShowImage("mainWin", outimg); 65 cvWriteFrame(writer, outimg); 66 } 67 } 68 i = 0; 69 while (1) 70 { 71 frameimg = cvQueryFrame(capture); //获取一帧图片 72 if (!frameimg)break;//读到尽头,退出 73 //设置字体 74 CvFont font; 75 cvInitFont(&font, CV_FONT_VECTOR0, 0.5f,0.5f , 0, 1, 8); 76 //在图像中显示文本字符串 77 cvPutText(frameimg, "StudentID", cvPoint(frameW - 100, frameH - 60), &font, CV_RGB(255, 255, 255)); 78 cvPutText(frameimg, "name", cvPoint(frameW - 100, frameH - 30), &font, CV_RGB(255, 255, 255)); 79 cvShowImage("mainWin", frameimg); 80 char key = cvWaitKey(20); 81 //将视频帧写入视频 82 cvWriteFrame(writer, frameimg); 83 } 84 cvReleaseVideoWriter(&writer); 85 cvDestroyWindow("mainWin"); 86 } 87 88 int main(int argc, char** argv) 89 { 90 strcpy(path, argv[1]); 91 getVideo(); 92 waitKey(); 93 system("pause"); 94 return 0; 95 }