一、批量有序读取图片
#include<opencv2/opencv.hpp> using namespace cv; void main()
{
//批量读取图片(有序)
char filename[];
char winName[];
Mat srcImg;
for (int i = ; i < ; i++)
{
sprintf(filename,"%d.bmp",i);
sprintf(winName,"NO--%d",i);
srcImg=imread(filename);
if (srcImg.empty())
break;
imshow(winName,srcImg);
}
waitKey();
destroyAllWindows();
}
二、视频分解成图片
需要提前在E盘创建pic文件夹
#include<opencv2/opencv.hpp> using namespace cv; void main()
{
Mat frame;
char outfile[];
VideoCapture cap("E:\\2.avi");
if (!cap.isOpened())//打开失败
return;
int totalFrame=cap.get(CV_CAP_PROP_FRAME_COUNT);//获取视频总帧数
for (int i = ; i <=totalFrame; i++)
{
cap>>frame;
if (frame.empty())
break;
sprintf(outfile,"E:\\pic\\%d.bmp",i);
imwrite(outfile,frame);
imshow("video",frame);
waitKey();
}
cap.release();
destroyAllWindows();
}
三、图片合成视频
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std; void main()
{
VideoWriter writer("E:\\out.avi",CV_FOURCC('X','V','I','D'),,Size(,),true);//Size要和图片尺寸保持一致
char filename[];
Mat frame;
for (int i = ; i < ; i++)
{
sprintf(filename,"E:\\pic\\%d.bmp",i);
frame=imread(filename);
if(frame.empty()) break;
writer<<frame;
}
cout<<"write end!"<<endl;
destroyAllWindows();
}