Ask Your Question
0

Save Image Sequence real time

asked Apr 10 '13

Rsousa1980 gravatar image

updated Apr 10 '13

berak gravatar image

Good morning,

     I'm trying to create a small program that allows you to capture frames from 2 to 2 seconds of a web camera in real time.     My code is working, the problem is to record the images, only one image is recorded.     My idea was to have several files stored in the directory of images, with the name years months days hours min sec     It is possible to do this with cvSaveImage, or is otherwise more effective to do this.

sprintf(file,"%s","/home/ricardo/Imagens/");
sprintf(file,"%d%d%d_%d%d%d.pgm\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
cvSaveImage(file, frame,0);



#include stdio.h
#include <stdlib.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include<time.h>


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);

sprintf(file,"%d%d%d_%d%d%d.pgm\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

printf("%s",file);

getchar();

   CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
   if ( !capture ) {
     fprintf( stderr, "ERROR: capture is NULL \n" );
     getchar();
     return -1;
   }

   cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
   while ( 1 ) {
      IplImage* frame = cvQueryFrame(capture);
     if ( !frame ) {

       fprintf( stderr, "ERROR: frame is null...\n" );
       getchar();
       break;
     }
     cvShowImage( "mywindow", frame );
     sprintf(file,"%s","/home/ricardo/Imagens/");
     sprintf(file,"%d%d%d_%d%d%d.pgm\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
     cvSaveImage(file, frame,0);

     //sleep(2);
     // 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;
   }
   // Release the capture device housekeeping
   cvReleaseCapture( &capture );
   cvDestroyWindow( "mywindow" );
   return 0;
 }
Preview: (hide)

4 answers

Sort by » oldest newest most voted
3

answered Apr 10 '13

berak gravatar image

"only one image is recorded"

the problem is here:

sprintf(file,"%d%d%d_%d%d%d.pgm\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

it has a minimum resolution of 1 second, so all images taken in that timespan get saved under the same name.

so, either

  1. add the clock() time:

    sprintf(file,"%d%d%d_%d%d%d_%d.pgm\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, clock() );

  2. or maybe a counter:

    // somwhere out of the loop

    int counter = 0;

    // in the loop:

    sprintf(file,"%d%d%d_%d%d%d.pgm\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, counter);
    counter++;

Preview: (hide)

Comments

+1 for counter for real time storage OR by using clock to get millisecond precision :)

StevenPuttemans gravatar imageStevenPuttemans (Apr 10 '13)edit
0

answered Apr 11 '13

xiaojidan gravatar image

hello can you give me the complete code,I try it, but it does not work.please :

#include <opencv2 opencv.hpp="">

include<time.h>

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" ); getchar(); return -1; } int counter = 0; cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE ); while ( 1 ) { IplImage* frame = cvQueryFrame(capture); if ( !frame ) {

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

   break;
 }
 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);

 //sleep(2);
 // 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;

} // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyWindow( "mywindow" ); return 0; }

Preview: (hide)
0

answered Apr 11 '13

xiaojidan gravatar image

I copy your code ,but it does not work, I can not save the picture in the folder --"D:/Projects/vs2010/text11/"
can you tell me the reason,please...

int main() {

char file[256];

time_t mytime; mytime = time(NULL); struct tm tm = *localtime(&mytime);

CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY ); if ( !capture ) { fprintf( stderr, "ERROR: capture is NULL \n" ); getchar(); return -1; } int counter = 0; cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE ); while ( 1 ) { IplImage* frame = cvQueryFrame(capture); if ( !frame ) {

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

   break;
 }
 cvShowImage( "mywindow", frame );
 sprintf(file,"%d%d%d_%d%d%d_%d.pgm\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, clock() );
 sprintf(file,"%s","D:/Projects/vs2010/text11/");
 sprintf(file,"%d%d%d_%d%d%d.pgm\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); 

 cvSaveImage(file, frame,0);

 //sleep(2);
 // 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;

} // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyWindow( "mywindow" ); return 0; }

Preview: (hide)

Comments

sorry you give me a wrong suggestion , it does not save iamge...please

xiaojidan gravatar imagexiaojidan (Apr 12 '13)edit
0

answered Apr 11 '13

berak gravatar image

each of your sprintf calls overwrites your previous attempt, it does not work like strcat you've got to put it all into one call:

sprintf(file,"%s%d%d%d_%d%d%d.pgm\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);
Preview: (hide)

Comments

hello can you give me the complete code,I try it, but it does not work.please :

include <opencv2/opencv.hpp>

include<time.h>

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" ); getchar(); return -1; } int counter = 0; cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE ); while ( 1 ) { IplImage* frame = cvQueryFrame(capture); if ( !frame ) {

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

   break;
 }
 cvShowImage( "mywindow", frame )
xiaojidan gravatar imagexiaojidan (Apr 11 '13)edit

Question Tools

Stats

Asked: Apr 10 '13

Seen: 4,050 times

Last updated: Jul 16 '13