Ask Your Question
0

Save Image Sequence real time

asked 2013-04-10 04:14:44 -0600

Rsousa1980 gravatar image

updated 2013-04-10 04:26:56 -0600

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;
 }
edit retag flag offensive close merge delete

4 answers

Sort by » oldest newest most voted
3

answered 2013-04-10 04:39:00 -0600

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

edit flag offensive delete link more

Comments

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

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-10 06:14:37 -0600 )edit
0

answered 2013-04-11 09:45:51 -0600

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

edit flag offensive delete link more
0

answered 2013-04-10 22:23:09 -0600

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

edit flag offensive delete link more

Comments

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

xiaojidan gravatar imagexiaojidan ( 2013-04-11 20:40:38 -0600 )edit
0

answered 2013-04-11 04:37:37 -0600

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);
edit flag offensive delete link more

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 ( 2013-04-11 09:44:52 -0600 )edit

Question Tools

Stats

Asked: 2013-04-10 04:14:44 -0600

Seen: 4,025 times

Last updated: Jul 16 '13