#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void main()
{
VideoCapture cap("G:\\11_10_13.mp4");
if (!cap.isOpened())
{
return;
}
long totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);
cout << "The whole video has" << totalFrameNumber << "frames." << endl;
double frameRate = cap.get(CV_CAP_PROP_FPS);
cout << "The frame rate of the video is: " << frameRate << endl;
long frameToStart = 200;
cap.set(CV_CAP_PROP_POS_FRAMES, frameToStart);
cout << "From the " << frameToStart << "th frame to read." << endl;
int imgIndex(frameToStart);
for (; ; )
{
Mat frame;
cap >> frame;
if (frame.empty())
{
break;
}
char* imageSaveName = new char[64];
sprintf(imageSaveName, "G:\\11_10_13\\%05d.jpg", imgIndex);
imwrite(imageSaveName, frame);
delete[] imageSaveName;
imgIndex++;
}
cout << "total frames: " << imgIndex << endl;
}
在视频操作中,经常需要将视频转换为图像序列进行操作,这段代码的功能是“把从第n帧开始的视频的每一帧转化为图片进行保存”。代码亲测可用,欢迎提问。