Tag Archives: OpenCV Video Conversion Images

OpenCV C++ Video to Images [/w code]

Hello, here I would like to present you an easy way to perform conversion from videos (in my case .mp4, but works with any format supported by OpenCV) to images.

You can either compile it and modify it as you please, or download and use this windows binaries (note you will need Microsoft c++ Redistributable installed)

Download binaries or sources!

Sample code follows:

// Alvise Memo, 12/02/2016

#include <opencv2\opencv.hpp>

int main(int argc, char ** argv)
{
 std::cout << "VideoToPNGs w/ OpenCV, Alvise Memo, 12/02/2016" << std::endl;

 if (argc < 3)
 {
 std::cout << "Usage: VideoToPNGs.exe pathtovideoinput.avi pathtooutput" << std::endl;
 std::getchar();
 return 1;
 }

 std::string input_file = argv[1];
 std::string output_folder = argv[2];
 
 std::cout << "Opening input file: " << input_file << std::endl;
 cv::VideoCapture vc;
 vc.open(input_file);
 assert(vc.isOpened() && "Input file DID NOT open correctly");

 cv::Mat input_frame;
 int frame_index = 0;
 while(vc.read(input_frame))
 {
 std::cout << "Reading frame " << frame_index << std::flush;

 std::string frame_name = std::to_string(frame_index);
 while (frame_name.length() < 5) frame_name = "0" + frame_name;

 bool saving = cv::imwrite(output_folder + "\\" + input_file + "_" + frame_name + ".bmp", input_frame);

 std::cout << " saved (" << saving << ")!" << std::endl;
 frame_index++;
 }
 vc.release();

 std::cout << "Exported " << frame_index << "frame/s.\nEnd." << std::endl;

 std::getchar();
 return 1;
}

 

Advertisements