Ask Your Question
0

Split video to images - OpenCV 2.4.5

asked 2013-04-25 05:43:52 -0600

mehdi gravatar image
I try to split .avi video into frame of images using this code :

#include <stdio.h>
#include < string.h>
#include <stdlib.h>
#include "cv.h"
#include <highgui.h>
#include "cxcore.h" 
int main( int argc, char** argv )
{    

    CvCapture *capture = cvCaptureFromAVI("xyz.avi");
    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, "frameSplit");

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

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

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

        frame_number++;

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

    // free resources
    cvReleaseCapture(&capture);

    return 0;
}

when i try to compile this program using : g++ -O2 -Wall -o VideoToImages VideoToImages.cpp pkg-config --cflags --libs opencv

i got this error : fatal error: string.h:no such file or directory compilation terminated

Any idea please Thanks

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2013-04-25 07:09:26 -0600

berak gravatar image

use

#include <string.h>

instead of

#include < string.h>

(cl does not care about the blank, gcc does)

edit flag offensive delete link more

Comments

I got a new error when i tried to compile the source

VideoToImages.cpp: In function ‘int main(int, char**)’: VideoToImages.cpp:38:40: error: ‘itoa’ was not declared in this scope

mehdi gravatar imagemehdi ( 2013-04-26 06:46:35 -0600 )edit

itoa

... totally unrelated to opencv, btw.

but throw that monstrosity away, instead of :

<code>

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

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

</code>

just do: string filename = cv::format("frameSplit%d.png",frame_id);

berak gravatar imageberak ( 2013-04-26 07:30:33 -0600 )edit

Should i rplace

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

by

string filename = cv::format("frameSplit%d.png",frame_id);

mehdi gravatar imagemehdi ( 2013-04-26 10:04:20 -0600 )edit

yes, that's what i meant.

or, char filename[100]; sprintf(filename, "frameSplit%d.png",frame_id); // if you don't like std::strings

berak gravatar imageberak ( 2013-04-26 10:15:42 -0600 )edit

i used the first solution

but i got another error when i try to compile

root@Hadoop:/usr/local/opencv/opencv-2.4.5/samples/c# g++ -O2 -Wall -o VideoToImages VideoToImages.cpp pkg-config --cflags --libs opencv

split.cpp: In function ‘int main(int, char**)’: split.cpp:42: error: ‘string’ was not declared in this scope split.cpp:42: error: expected ‘;’ before ‘filename’ split.cpp:43: error: ‘filename’ was not declared in this scope

and when i try the second solution i got another error

root@Hadoop:/usr/local/opencv/opencv-2.4.5/samples/c# g++ -O2 -Wall -o VideoToImages VideoToImages.cpp pkg-config --cflags --libs opencv

split.cpp: In function ‘int main(int, char**)’: split.cpp:42: error: ‘frame_id’ was not declared in this scope

le me know if i use the appropriate compilation

mehdi gravatar imagemehdi ( 2013-04-27 06:04:18 -0600 )edit

Question Tools

Stats

Asked: 2013-04-25 05:43:52 -0600

Seen: 2,873 times

Last updated: Apr 25 '13