opencv学习之路(3)、批量读取图片、视频分解、视频合成
一、批量有序读取图片
1 #include<opencv2/opencv.hpp> 2 3 using namespace cv; 4 5 void main() 6 { 7 //批量读取图片(有序) 8 char filename[50]; 9 char winName[50]; 10 Mat srcImg; 11 for (int i = 1; i < 100; i++) 12 { 13 sprintf(filename,"%d.bmp",i); 14 sprintf(winName,"NO--%d",i); 15 srcImg=imread(filename); 16 if (srcImg.empty()) 17 break; 18 imshow(winName,srcImg); 19 } 20 waitKey(0); 21 destroyAllWindows(); 22 }
二、视频分解成图片
需要提前在E盘创建pic文件夹
1 #include<opencv2/opencv.hpp> 2 3 using namespace cv; 4 5 void main() 6 { 7 Mat frame; 8 char outfile[50]; 9 VideoCapture cap("E:\\2.avi"); 10 if (!cap.isOpened())//打开失败 11 return; 12 int totalFrame=cap.get(CV_CAP_PROP_FRAME_COUNT);//获取视频总帧数 13 for (int i = 1; i <=totalFrame; i++) 14 { 15 cap>>frame; 16 if (frame.empty()) 17 break; 18 sprintf(outfile,"E:\\pic\\%d.bmp",i); 19 imwrite(outfile,frame); 20 imshow("video",frame); 21 waitKey(15); 22 } 23 cap.release(); 24 destroyAllWindows(); 25 }
三、图片合成视频
1 #include<opencv2/opencv.hpp> 2 #include<iostream> 3 using namespace cv; 4 using namespace std; 5 6 void main() 7 { 8 VideoWriter writer("E:\\out.avi",CV_FOURCC('X','V','I','D'),20,Size(320,240),true);//Size要和图片尺寸保持一致 9 char filename[50]; 10 Mat frame; 11 for (int i = 1; i < 644; i++) 12 { 13 sprintf(filename,"E:\\pic\\%d.bmp",i); 14 frame=imread(filename); 15 if(frame.empty()) break; 16 writer<<frame; 17 } 18 cout<<"write end!"<<endl; 19 destroyAllWindows(); 20 }