title: how to Extract Video Frames and Save them as Images using c++
body: <p>I am now trying to save the frames of the video file into images on my pc. I am using Visual studio 2010 and opencv 2.3.1.  In this code(shown bellow) it can save frames of image sequence but for video file I can not save the frames.
  the problem specifically seems to be here:
  in the function of vidoeprocessing()</p>
  
  <pre><code>   string imageToSave =("output_MOG_" +  frameNumberString + ".png");
    bool saved = imwrite( imageToSave,fgMaskMOG);
    if(!saved) {
     cerr &lt;&lt; "Unable to save " &lt;&lt; imageToSave &lt;&lt; endl;
        } 
  </code></pre>
  
  <p>any one can help to solve this?<br>
  Thanks in advance.</p>
  
  <p>my code is this:</p>
  
  <pre><code>    //opencv
   #include &lt; opencv2/opencv.hpp&gt;
   #include &lt; opencv2/core/core.hpp&gt;
   #include &lt; opencv2/highgui/highgui.hpp&gt;
   #include &lt; opencv2/video/background_segm.hpp&gt;
   //#include &lt;opencv2/imgcodecs.hpp&gt;
   #include &lt;opencv2/imgproc/imgproc.hpp&gt;
   //#include "opencv2/videoio.hpp"
    #include &lt;opencv2/video/video.hpp&gt;
   //C
   #include &lt;stdio.h&gt;
  //C++
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  
  using namespace cv;
  using namespace std;
  
        // Global variables
    Mat frame; //current frame
   Mat fgMaskMOG; //fg mask fg mask generated by MOG method
   Ptr&lt;BackgroundSubtractor&gt; pMOG; //MOG Background subtractor
   int keyboard; //input from keyboard
  
   /** Function Headers */
  void help();
  void processVideo(char* videoFilename);
  void processImages(char* firstFrameFilename);
  
  void help()
  {
   cout
     &lt;&lt; "---------------------------------------------------------------------   -----" &lt;&lt; endl
   &lt;&lt; "This program shows how to use background subtraction methods provided by "  &lt;&lt; endl
  &lt;&lt; " OpenCV. You can process both videos (-vid) and images (-img)."             &lt;&lt; endl
                                                                                  &lt;&lt; endl
  &lt;&lt; "Usage:"                                                                     &lt;&lt; endl
  &lt;&lt; "./bs {-vid &lt;video filename&gt;|-img &lt;image filename&gt;}"                         &lt;&lt; endl
  &lt;&lt; "for example: ./bs -vid video.avi"                                           &lt;&lt; endl
  &lt;&lt; "or: ./bs -img /data/images/1.png"                                           &lt;&lt; endl
  &lt;&lt; "--------------------------------------------------------------------------" &lt;&lt; endl
  &lt;&lt; endl;
   }
  
  /**
   * @function main
   */
  int main(int argc, char* argv[])
  {
  
  
    //print help information
    help();
  
      //check for the input parameter correctness
      if(argc != 3) {
      cerr &lt;&lt;"Incorret input list" &lt;&lt; endl;
      cerr &lt;&lt;"exiting..." &lt;&lt; endl;
      return EXIT_FAILURE;
  }
  
  
  //create GUI windows
  namedWindow("Frame");
  namedWindow("FG Mask MOG ");
  
  //create Background Subtractor objects
  pMOG = new BackgroundSubtractorMOG();
  if(strcmp(argv[1], "-vid") == 0) {
      //input data coming from a video
      processVideo(argv[2]);
  }
  else if(strcmp(argv[1], "-img") == 0) {
      //input data coming from a sequence of images
      processImages(argv[2]);
  }
  else {
      //error in reading input parameters
      cerr &lt;&lt;"Please, check the input parameters." &lt;&lt; endl;
      cerr &lt;&lt;"Exiting..." &lt;&lt; endl;
      return EXIT_FAILURE;
  }
  //destroy GUI windows
  destroyAllWindows();
  return EXIT_SUCCESS;
  }
  
   //function processVideo
  
   void processVideo(char* videoFilename) {
    //create the capture object
      VideoCapture capture(videoFilename);
    if(!capture.isOpened()){
      //error in opening the video input
      cerr &lt;&lt; "Unable to open video file: " &lt;&lt; videoFilename &lt;&lt; endl;
      exit(EXIT_FAILURE);
   }
   //read input data. ESC or 'q' for quitting
    while( (char)keyboard != 'q' &amp;&amp; (char)keyboard != 27 ){
      //read the current frame
      if(!capture.read(frame)) {
          cerr &lt;&lt; "Unable to read next frame." &lt;&lt; endl;
          cerr &lt;&lt; "Exiting..." &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
  
      //update the background model
      pMOG-&gt;operator()(frame, fgMaskMOG,0.9);
      //get the frame number and write it on the current frame
  
      stringstream ss;
      rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
                cv::Scalar(255,255,255), -1);
      ss &lt;&lt; capture.get(CV_CAP_PROP_POS_FRAMES);
      string frameNumberString = ss.str();
  
      putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
              FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
      //show the current frame and the fg masks
      imshow("Frame", frame);
      imshow("FG Mask MOG  ", fgMaskMOG);
      //get the input from the keyboard
      keyboard = waitKey( 30 );
  }
  
  /*
  string imageToSave =("output_MOG_" +  frameNumberString + ".bmp");
      bool saved = imwrite( imageToSave,fgMaskMOG);
      if(!saved) {
      cerr &lt;&lt; "Unable to save " &lt;&lt; imageToSave &lt;&lt; endl;
         } 
        */
  
  
  //delete capture object
   capture.release();
   }
  
  /**
   * @function processImages
   */
  void processImages(char* fistFrameFilename) {
    //read the first file of the sequence
    frame = imread(fistFrameFilename);
    if(frame.empty()){
      //error in opening the first image
      cerr &lt;&lt; "Unable to open first image frame: " &lt;&lt; fistFrameFilename &lt;&lt;     endl;
      exit(EXIT_FAILURE);
  }
  //current image filename
  string fn(fistFrameFilename);
  //read input data. ESC or 'q' for quitting
  while( (char)keyboard != 'q' &amp;&amp; (char)keyboard != 27 ){
      //update the background model
      pMOG-&gt;operator()(frame, fgMaskMOG,0.9);
      //get the frame number and write it on the current frame
      size_t index = fn.find_last_of("/");
      if(index == string::npos) {
          index = fn.find_last_of("\\");
      }
      size_t index2 = fn.find_last_of(".");
      string prefix = fn.substr(0,index+1);
      string suffix = fn.substr(index2);
      string frameNumberString = fn.substr(index+1, index2-index-1);
      istringstream iss(frameNumberString);
      int frameNumber = 0;
      iss &gt;&gt; frameNumber;
      rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
                cv::Scalar(255,255,255), -1);
      putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
              FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
      //show the current frame and the fg masks
      imshow("Frame", frame);
      imshow("FG Mask MOG ", fgMaskMOG);
      //get the input from the keyboard
      keyboard = waitKey( 30 );
      //search for the next image in the sequence
      ostringstream oss;
      oss &lt;&lt; (frameNumber + 1);
      string nextFrameNumberString = oss.str();
      string nextFrameFilename = prefix + nextFrameNumberString + suffix;
      //read the next frame
      frame = imread(nextFrameFilename);
      if(frame.empty()){
          //error in opening the next image in the sequence
          cerr &lt;&lt; "Unable to open image frame: " &lt;&lt; nextFrameFilename &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
      //update the path of the current frame
      fn.assign(nextFrameFilename);
  
      // save subtracted images
      string imageToSave =("output_MOG_" +  frameNumberString + ".png");
      bool saved = imwrite( imageToSave,fgMaskMOG);
      if(!saved) {
      cerr &lt;&lt; "Unable to save " &lt;&lt; imageToSave &lt;&lt; endl;
         }
  }
  }
  </code></pre>