Ask Your Question

Save Image Sequence real time,but cannot save Consecutive pictures ,why?

asked 2013-04-11 20:28:25 -0500

xiaojidan gravatar image
#include <opencv2/opencv.hpp>

int main() 
                      char file[256];  
        time_t mytime;
        mytime = time(NULL);
        struct tm tm = *localtime(&mytime);
        printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

        CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );

       if ( !capture ) {
      fprintf( stderr, "ERROR: capture is NULL \n" );
      return -1;


 cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

while ( 1 ) {
  IplImage* frame = cvQueryFrame(capture);
 if ( !frame ) {

   fprintf( stderr, "ERROR: frame is null...\n" );

 cvShowImage( "mywindow", frame );

 sprintf(file,"%s%d%d%d_%d%d%d%d.jpg\n","D:/Projects/vs2010/text11/", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); 

 cvSaveImage(file, frame,0);

 // Do not release the frame!
 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
 //remove higher bits using AND operator
 if ( (cvWaitKey(10) & 255) == 27 ) break;


  cvReleaseCapture( &capture );
  cvDestroyWindow( "mywindow" );
  return 0;


edit retag flag offensive close merge delete


Please be so kind to not only post code, but to point out your system, configuration, where the problem arises. I mean this is an example of a bad question structure. Nevertheless I will have a look at your problem.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-12 02:42:45 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2013-04-12 02:50:00 -0500

Basically your problem is that you are storing the image within a time stamp of seconds. However, the processing processes multiple image frames within a second, overwriting your frame each time.

You have to create a unique timestamp. You can do this by adding a counter that increases with each write and adds a value at the end of your filename OR you could go down to millisecond level by using the tics functionality.

clock_t time;
time= clock();
double result = (float)time)/CLOCKS_PER_SEC;

This will output values like 0.022 for example. Multiply it with 1000 and you have a unique number combined with the second value you already reached before.

edit flag offensive delete link more


Hello , I am new come. Can you tell me how to insert the code in my codes, can you give me the complete code , thank you~

xiaojidan gravatar imagexiaojidan ( 2013-04-12 05:11:43 -0500 )edit

this is right???

clock_t time;

time= clock();

double result = (float)time/CLOCKS_PER_SEC;

sprintf(file,"%s%d%d%d_%d%d%d%d%d.jpg\n","D:/Projects/vs2010/text11/", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,counter );


xiaojidan gravatar imagexiaojidan ( 2013-04-12 05:16:53 -0500 )edit

To your first question, no I will not give you the complete code. Please if you want to learn something, put some effort in it... About the second comment, what you doing now is creating a counter AND using the time constant. However, you should only use one of them. The counter could be enough just don't forget to give it a initial value of 0.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-12 06:00:57 -0500 )edit

I try it , but it does not meet my needs, can you give me your email so that I would like to ask you more in-depth

xiaojidan gravatar imagexiaojidan ( 2013-04-12 06:17:50 -0500 )edit

No :) This is the place for you to work, I will not spread around my email adress just because you need help, nor will I reply by mail if you contact me one way or the other. Read again all posts I have made, I have literally served you the solution multiple times....

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-12 06:36:36 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-04-11 20:28:25 -0500

Seen: 617 times

Last updated: Apr 12 '13