This program takes an input video and saves each frame as a BMP image in a specified output folder:

 

/*
 * video2seq.cpp
 *
 *  Created on: May 14, 2016
 *      Author: gustavo
 */
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>

using namespace std;
using namespace cv;

void print_help();

int main(int argc, char **argv)
{
	std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;

	VideoCapture capture;

	if(argc > 2)
	{
		std::cout << "*Opening: " << argv[1] << std::endl;
		capture = VideoCapture(argv[1]);
	}
	else
	{
		print_help();
	}

	if(!capture.isOpened())
	{
		std::cerr << "Cannot initialize video!" << std::endl;
		return -1;
	}

	string output_folder = argv[2];
	int frame_n;

	Mat frame;

	int key = 0;
	while(key != 'q')
	{
		capture >> frame;
		if(frame.empty()) break;

		frame_n = capture.get(CV_CAP_PROP_POS_FRAMES);
		stringstream iss;
		iss << output_folder << "/" << setfill('0') << setw(5) << frame_n << ".bmp";
		imwrite(iss.str(), frame);
	}
}

void print_help(){
	cout << "USAGE:" << endl;
	cout << "video2seq <INPUT_FILE> <OUTPUT_FOLDER>" << endl;
	cout << endl;
}

You can change the output image format e.g. jpeg, png, etc.

Advertisements