Ask Your Question

Revision history [back]

Hi Javani,

Yes you can do it (assuming that you have the proper codecs installed to read the video). I think it is a beautiful exercise to get acquainted with OpenCV, you can do something like this:

#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>

int main (int argc, char **argv)
{
  if (argc !=2)
  { 
    std::cout << "USE: " << argv[0] << " <video-filename>" << std::endl;
    return 1;
  }

  //Open the video that you pass from the command line
  cv::VideoCapture cap(argv[1]);
  if (!cap.isOpened())
  {
    std::cerr << "ERROR: Could not open video " << argv[1] << std::endl;
    return 1;
  }

  int frame_count = 0;
  bool should_stop = false;

  while(!should_stop)
  {
    cv::Mat frame;
    cap >> frame; //get a new frame from the video
    if (frame.empty())
    {
      should_stop = true; //we arrived to the end of the video
      continue;
    }

    char filename[128];
    sprintf(filename, "frame_%06d.jpg", frame_count);
    cv::imwrite(filename, frame);
    frame_count++;
  }

  return 0;
}

Note that I just coded this "on the fly", I don't know if the code compiles or works properly, but you get the idea.

Also, ffmpeg can convert a video to images and you don't need to code anything.

I hope this helps