0

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?

Maroun
  • 94,125
  • 30
  • 188
  • 241
kerry_13
  • 289
  • 1
  • 3
  • 12

2 Answers2

0

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++);
[...]
cedrou
  • 2,780
  • 1
  • 18
  • 23
  • thanks to answer, i can figure it out. but the problem is how to append the value of the counter, because in `cvsaveImage()`, file name is a const* char: `int cvSaveImage( const char* filename, const CvArr* image );` – kerry_13 Mar 15 '13 at 17:17
  • You can use sprintf() for example. – cedrou Mar 15 '13 at 17:22
0

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 );
Nate Hekman
  • 6,507
  • 27
  • 30