Opencv2.4.9将视频保存成30帧图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CV2017/article/details/82109776
#include <iostream>
#include "cv.h"
#include "opencv2/opencv.hpp"
#include <string.h>
#include <tchar.h>

using namespace std;
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{
	const string strFilePath = "D:\\WX\\打靶\\new2.mp4";
	VideoCapture cap;
	Mat frame;
	int frmNum = 0;

	char _path[255];
	char prefix[] = "D:\\WX\\打靶\\1\\";
	char postfix[] = ".png";

	cap.open(strFilePath);
	if (!cap.isOpened())
	{
		cout << "fail to open the video"<<endl;
		return -1;
	}

	long totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);
	cout << "total frames: " << totalFrameNumber << endl;
	bool flags = true;
	long currentFrame = 0;

	while(flags)
	{
		frmNum++;
		cap>>frame;
		if (frame.empty())
		{
			cout<<"video over"<<endl;
			return -1;
		}

		memset(_path, '\0', sizeof(char)*255);
		sprintf_s(_path, "%sframe_%04d%s",prefix,frmNum,postfix);

		if (currentFrame % 30 == 0)
		{
			imwrite(_path,frame);
		}
		
		if (currentFrame >= totalFrameNumber){
			flags = false;
		}
		currentFrame++;

		//imshow("video",frame);
		//waitKey(250);
	}

	waitKey(0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/CV2017/article/details/82109776