saving an image sequence from video using opencv2

11,566

Solution 1

Use an index that will keep track of the number part in the filename. In the image capturing loop, add the index with the filename and build the final filename.

here is an example :

while(1)
{
     cap.read ( frame);
     if( frame.empty()) break;
     imshow("video", frame);
     char filename[80];
     sprintf(filename,"C:/Users/cssc/Desktop/testFolder/test_%d.png",i);
     imwrite(filename, frame);
     i++;
     char key = waitKey(10);
     if ( key == 27) break;
}

Solution 2

This is my code... I tryed a lot and finally made it this is c++ using opencv 3... hope it works

#include "opencv2/opencv.hpp"
#include <sstream>
#include <iostream>


using namespace cv;
using namespace std;

 Mat frame,img;
    int counter;

int main(int,char**)
   {
        VideoCapture vid("video3.avi");


while (!vid.isOpened())
{
    VideoCapture vid("video2.MOV");
    cout << "charging" << endl;
    waitKey(1000);

}

cout << "Video opened!" << endl;

while(1)
{
    stringstream file;

    vid.read(frame);
    if(frame.empty()) break;
    file << "/home/pedro/workspace/videoFrame/Debug/frames/image" << counter << ".jpg";
    counter++;
    imwrite(file.str(),frame);

    char key = waitKey(10);
 if ( key == 27)
 {break;}


}


} 
Share:
11,566
geophotologist
Author by

geophotologist

Updated on June 04, 2022

Comments

  • geophotologist
    geophotologist almost 2 years

    Newbie question and yes I have spent a lot of time sifting through similar questions and Answers with no luck.

    What I am trying to do is save frames from a video file in a sequential order. I have managed to save one image using c and I cannot seem to save images after that. I have started using c++ in opencv instead of c and all I can do is view the video and not save any jpg's from it.

    I am using opencv2.4.4a on mac if that helps. below is my c example

    #include <stdio.h>
    #include <stdlib.h>
    #include <opencv/cv.h>
    #include <opencv/highgui.h>
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    int main (int argc, char** argv)
    {
    //initializing capture from file
    CvCapture * capture = cvCaptureFromAVI ("/example/example.mov");
    
    //Capturing a frame
    IplImage* img = 0;
    if(!cvGrabFrame(capture))      //capture a frame
    {
    printf)Could not grab a fram\n\7");
    exit(0);
    }
    img=cvRerieveFrame(capture);    //retrieve the captured frame
    
    //writing an image to a file
    if (!cvSaveImage("/frames/test.jpg", img))
    printf("Could not save: %s\n","test.jpg");
    
    //free resources
    cvReleaseCapture(&capture);
    
    }
    

    Thank you in advance

    edit to the above.

    I have added to the above code which results in an image to be saved with the test.jpg and then gets rewritten with the next frame. How do I tell opencv to not copy over the last image and rename the next frame to test_2.jpg eg, test_1.jpg, test_2.jpg and so on?

    double num_frames = cvGetCaptureProperty (capture, CV_CAP_PROP_FRAME_COUNT);
    
    for (int i = 0; i < (int)num_frames; i++)
    {
    img = cvQueryFrame(capture);
    cvSaveImage("frames/test.jpg", img);
    }
    
    cvReleaseCapture(&capture);
    }
    
  • geophotologist
    geophotologist about 11 years
    thanks Barshan! I had to include int i = 0; i++; and it worked