读取视频,保存视频中所有的帧OR指定的帧 到新建的一个文件夹中

读取视频,保存视频中所有的帧到新建的一个文件夹中

#include <iostream>
#include <opencv2/opencv.hpp>
#include <direct.h>
//#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
	_mkdir("D:\\train\\");  //在D盘新建一个文件夹
	VideoCapture cap("D:\\vedio\\JYZ1.mp4");
	long totalFrameNum = cap.get(CV_CAP_PROP_FRAME_COUNT);
	cout << "total frame" << totalFrameNum << endl;
	Mat frame;
	bool flags = true;
	long currentFrame = 0;
	while (flags)
	{
		cap.read(frame);
		stringstream str;
		str << "cqh" << currentFrame << ".jpg";
		/*if (currentFrame % 30 == 0)
		{
		imwrite("D:\\train\\" + str.str(), frame);
		}*/      //这段本来是保存30的倍数的图片,我改成了把所有帧都存起来 如下:

		imwrite("D:\\train\\" + str.str(), frame);

		if (currentFrame >= totalFrameNum - 1)
		{
			flags = false;
		}
		currentFrame++;
	}

	cap.release();
	waitKey(0);
	system("pause");
	return 0;
}

如果要保存特定的不确定的几帧,比如视频关键帧提取时要保存的关键帧,一开始并不知道是哪几帧,关键帧随着循环存到了id中。要把这几帧保存到指定文件夹中的办法是:可以先定义一个Vector容器,因为这个容器大小不是确定的,然后把id的值循环存到Vector中,在把Vector中的数据利用迭代器遍历出来,判断currentFrame == 迭代器中的数,是就保存。这样就可以把指定的关键帧保存起来了。可能说不明白,以后放具体代码或者链接吧。

猜你喜欢

转载自blog.csdn.net/weixin_43384257/article/details/88560717
今日推荐