Question

I want to save some frame of my video. I try using CvsaveImage() in visual C++ 2010, but it cannot save many frame, because I don't know how to save the next frame with the different name. So, the old frame will be overwritten by the next one.

Can anybody help me to save many frame with different name?

Was it helpful?

Solution

Use a counter, such as the frame number or an integer that you increment each time you save a new file. Append the value of this counter at the end of you file name and you'll get a unique filename until the counter overflows (more than 4 billion frames for an unsigned integer).

static unsigned int counter = 0;
[...]
char pFileName[MAX_PATH] = {0};
sprintf(pFileName, "<YourFilePath>\\Frame_%u.jpg", counter++);
[...]

OTHER TIPS

cvSaveImage() takes a const char* filename, but that just means cvSaveImage won't change the string; it doesn't mean you can't change it.

If it's simpler, you could wrap cvSaveImage with your own function and form the filename there:

int my_cvSaveImage( const char* baseFilename, int frameNumber, const CvArr* image )
{
    char filename[MAX_PATH] = {};
    sprintf( filename, "%s-%d.jpg", baseFilename, frameNumber );
    return cvSaveImage( filename, image );
}

Now you call:

my_cvSaveImage( "file", 1, image );
//...
my_cvSaveImage( "file", 2, image );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top