10

Using opencv, how can one split a video into an image sequence?
How can i split it so that the output will be a sequence of images?

Community
  • 1
  • 1
meroz
  • 101
  • 1
  • 1
  • 4

2 Answers2

12

For my surprise, I couldn't find an answer to this question on StackoverFlow.

I'm currently using OpenCV 2.1. This might be a little old but it works like a charm. The program will read an input file and create a sequence of images on the current folder named *frame_xx.jpg*

#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "highgui.h"

int main( int argc, char** argv )
{  
    if (argc < 2)
    {
        printf("!!! Usage: ./program <filename>\n");
        return -1;
    }

    printf("* Filename: %s\n", argv[1]);   

    CvCapture *capture = cvCaptureFromAVI(argv[1]);
    if(!capture) 
    {
        printf("!!! cvCaptureFromAVI failed (file not found?)\n");
        return -1; 
    }

    int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    printf("* FPS: %d\n", fps);

    IplImage* frame = NULL;
    int frame_number = 0;
    char key = 0;   

    while (key != 'q') 
    {
        // get frame 
        frame = cvQueryFrame(capture);       
        if (!frame) 
        {
            printf("!!! cvQueryFrame failed: no frame\n");
            break;
        }       

        char filename[100];
        strcpy(filename, "frame_");

        char frame_id[30];
        itoa(frame_number, frame_id, 10);
        strcat(filename, frame_id);
        strcat(filename, ".jpg");

        printf("* Saving: %s\n", filename);

        if (!cvSaveImage(filename, frame))
        {
            printf("!!! cvSaveImage failed\n");
            break;
        }

        frame_number++;

        // quit when user press 'q'
        key = cvWaitKey(1000 / fps);
    }

    // free resources
    cvReleaseCapture(&capture);

    return 0;
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • @mero0o That's another completely different question and depends on what platform you are using. I believe this question is already solved. Don't forget to accept my answer. – karlphillip Dec 04 '10 at 12:51
  • @mero0o Probably, but I don't know since I don't have it. Try it, if it doesn't work, move to *2.1*. Careful, the newest OpenCV changed a lot and my code probably won't compile. This code is specific to 2.1. By the way, Google has lots of results that show how to setup your environment to compile OpenCV stuff. Check this one out: http://rajputyh.blogspot.com/2010/03/configure-opencv-20-on-visual-studio.html – karlphillip Dec 04 '10 at 13:01
  • if i download opencv2.1 and i have visualstudi 2008 how can this work with this thus i can write this code and run it – meroz Dec 04 '10 at 13:09
  • Did you notice that it's also ME who is answering this same question on your other thread? I won't duplicate my answers here. – karlphillip Dec 04 '10 at 13:13
  • @karlphillip do you work with opencv on a regular basis? Just curious... I've seen you answer a lot of questions on opencv :) – Utkarsh Sinha Dec 05 '10 at 09:29
  • @karlphillip +1: this answer is more precise and actionable than mine, particularly if the OP uses OpenCV with C/C++ rather than Python. Good thing, it appears the OP needs a lot of help ;-) – mjv Dec 05 '10 at 17:17
  • @Utkarsh I don't. But I've played with it a lot while doing some real-time image processing experiments on the GPU. – karlphillip Dec 06 '10 at 12:08
  • @karlphillip Note suggested `itoa` implementations at http://stackoverflow.com/a/12386915/1979048, http://www.geeksforgeeks.org/implement-itoa/, etc. Without these the example code that you provided has no chance to compile on Linux, an operating system used by some Stackoverflow users. – Jonathan Ben-Avraham Aug 17 '16 at 09:28
10

Even after my edits, the question looks more like a "send-me-the-codez" demand than a typical StackOverflow question whereby the OP provides detailed information and demonstrates that he or she has put some real thought into it.

So I'll just give a "half answer"... Anyway I'm only moderately familiar with OpenCV's machine learning module and barely cognizant of the rest of the library. ;-)

In a nutshell, the pseudo code should look like that and the implementation would typically require the OpenCV methods mentioned.

   myCapt = cvCreateFileCapture("myInput.avi")   // Assuming the feed is from a file.

   while (there are frames and we still want them)
      cvGrabFrame(myCapt)
      myImg = cvRetrieveFrame(myCapt)
      // alternatively to cvGrabFrame + cvRetrieveFrame, could use cvQueryFrame()

      // save the file (or do something with it...)
      cvSaveImage("some_file_name_with_seq_nr", myImage)

   cvReleaseCapture(capture)

As hinted the above needs much syntax and logic work (eg. figuring out when to stop, producing the file name of the individual frames, and of course declaring properly the variables and using the right level of indirection w/ all them pointers ;-). Of course some of this is made easier if you use the Python interface.

The Reading and Writing Images and Video OpenCV documentation page, provides more detailed info about the individual methods.

mjv
  • 73,152
  • 14
  • 113
  • 156