Opencv中利用VideoCapture类将视频保存为图片

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/BobAuditore/article/details/82153686
#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帧开始的视频的每一帧转化为图片进行保存”。代码亲测可用,欢迎提问。

展开阅读全文

没有更多推荐了,返回首页