开发者

openCV: How to split a video into image sequence?

Using ope开发者_Go百科ncv, 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?


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜