opencv 將視訊批量儲存為每幀圖片
#include <opencv2/opencv.hpp>
#include <tchar.h>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <ostream>
using namespace std;
using namespace cv;
void main()
{
//類VideoCapture例項化再初始化
//VideoCapture capture;
//capture.open("Megamind.avi");
//類VideoCapture例項化的同時初始化
VideoCapture capture("1.mp4");
if (!capture.isOpened())
{
return;
}
int imgIndex = 0;
for (;;)
{
Mat frame;
capture >> frame;
if (frame.empty())
{
break;
}
char* imageSaveName = new char[200];
//將圖片的路徑及名稱輸入到imageSaveName中
sprintf(imageSaveName, "%05d.jpg", imgIndex);
//將每一幀影象frame儲存到imageSaveName指定的檔案中
//4表示每隔四幀進行儲存
if(imgIndex%4==0)
{
imwrite(imageSaveName, frame);
}
delete[] imageSaveName;
imgIndex++;
}
int i;
i = int(imgIndex / 4);
cout << "total frames: " << i << endl;
}
//轉載