Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
153 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>
");
        return -1;
    }

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

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

    int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    printf("* FPS: %d
", 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
");
            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
", filename);

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

        frame_number++;

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

    // free resources
    cvReleaseCapture(&capture);

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...