Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

V4LCaptureTest.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011  ---------------------------------------------------------------------------*/
00012 #include <V4LCaptureTest.h>
00013 #include <phission.h>
00014 
00015 /* ------------------------------------------------------------------------ */
00016 void usage()
00017 {
00018     printf("\n\nUsage:\n");
00019     printf("\t--help            Display usage\n");
00020     printf("\t--test            Doesn't save images, waits for CTRL-C\n");
00021     printf("\t--nimages <value> Save <value> number of images from capture\n");
00022     printf("\n\n");
00023 
00024     exit(0);
00025 }
00026 
00027 /* ------------------------------------------------------------------------ */
00028 typedef struct save_frame_t
00029 {
00030     phImage *img;           /* pointer to the image object      */
00031     char    *prefix;        /* unique-id for the particular img */
00032     char    *filetype;      /* "ppm" or "jpg"                   */
00033     int     counter;        /* frame number count               */
00034     int     allow_newline;  /* allow this function to output \n */
00035 } save_frame_param;
00036 
00037 /* ------------------------------------------------------------------------ */
00038 int save_frame( save_frame_param *p )
00039 {
00040     char filename[255];
00041 
00042     if ((p->img->isNull()   == 0) && 
00043         (p->img->getWidth() != 0) &&
00044         (p->img->getHeight()!= 0))
00045     {
00046         /* Create the filename */
00047         sprintf(filename,"%s_%04d.%s",p->prefix,p->counter,p->filetype);
00048 
00049         /* print the frame we captured */
00050         printf("%s:%4d, ",p->prefix,p->counter);
00051         /* Print a newline and tab in every 3 frames */
00052         if ((((p->counter + 1) % 3) == 0) && (p->allow_newline)) printf("\n\t\t");
00053         fflush(stdout);
00054         
00055         /* Save the file */
00056         p->img->save(filename);
00057 
00058         /* Increment the frame number */
00059         p->counter++;
00060     }
00061 
00062     return phSUCCESS;
00063 }
00064 
00065 /* ------------------------------------------------------------------------ */
00066 int main(int argc, char *argv[] )
00067 {
00068     phFUNCTION("main")
00069 
00070     uint32_t        width       = 640;
00071     uint32_t        height      = 480;
00072     uint32_t        channel     = 2;
00073     uint32_t        format      = phImageBGRA32;
00074     char           *capture_path= NULL;
00075 
00076     int             i           = 0;
00077     int             nimages     = 30;
00078     
00079     /* There are 2 ways to get images ... */
00080     /* phImageCapture::copyImage(image) ... */
00081     phImage             copy_image;
00082     save_frame_param    copy_params     = { &copy_image,
00083                                             "copy",
00084                                             "ppm", /* or jpg */
00085                                             0, 
00086                                             1 };
00087     /* .. and the phLiveObject::connect, phLiveObject::update methods */
00088     phImage             connect_image;
00089     save_frame_param    connect_params  = { &connect_image,
00090                                             "update",
00091                                             "ppm", /* or jpg */
00092                                             0,
00093                                             0 };
00094 
00095 #ifdef WIN32
00096     VFWSource       *capture = new VFWSource();
00097 #else
00098     V4LCapture      *capture = new V4LCapture();
00099 #endif
00100         
00101     /* Utility class to ease the starting and stopping of displays, captures and
00102      * pipelines */
00103     phSystem        system;
00104    
00105     /* Remove the code below when using this code as an example.
00106      * 
00107      * This just checks whether "--test" has been specified with
00108      * a time value argument. It's for testing all the examples
00109      * without the need for human intervention. */
00110     int             test = 0;
00111 
00112     phArgTable      *arg_parser = new phArgTable();
00113     
00114     /* Setup and parse all the arguments */
00115     rc = arg_parser->add("--test",&test,phARG_BOOL);
00116     phCHECK_RC(rc,NULL,"arg_parser->add");
00117     rc = arg_parser->add("--nimages",&nimages,phARG_INT);
00118     phCHECK_RC(rc,NULL,"arg_parser->add");
00119     rc = arg_parser->add("--help",(void *)&usage,phARG_FUNC);
00120     phCHECK_RC(rc,NULL,"arg_parser->add");
00121 
00122     rc = arg_parser->add("--path",&capture_path,phARG_CHAR);
00123     phCHECK_RC(rc,NULL,"arg_parser->add");
00124 
00125     rc = arg_parser->add("--channel",&channel,phARG_INT);
00126     phCHECK_RC(rc,NULL,"arg_parser->add");
00127     
00128     rc = arg_parser->parse(argc,argv);
00129     phCHECK_RC(rc,NULL,"arg_parser->parse");
00130  
00131     /* Set up the capture parameters */
00132     capture->set        ( width, height, capture_path );
00133     capture->setChannel ( channel );
00134     capture->setFormat  ( format  );
00135 
00136     /* Add the capture to the system to make starting and stopping easy */
00137     rc = system.addCapture( capture );
00138     phPRINT_RC(rc,NULL,"system.addCapture( &capture )");
00139     
00140     /* connect the image to the output of the capture class. 
00141      * The capture class has an phImage object (derived from phLiveObject) */
00142     rc = connect_image.connect( capture->getOutput() );
00143     phPRINT_RC(rc,NULL,"image.connect()");
00144     
00145     /* startup */
00146     rc = system.startup();
00147     phPRINT_RC(rc,NULL,"system.startup()");
00148    
00149     if (nimages)
00150     {
00151         printf("Saving frame:\t");
00152         fflush(stdout);
00153     }
00154     
00155     while (capture->isOpen())
00156     {
00157         while (((copy_params.counter    < nimages) || 
00158                 (connect_params.counter < nimages)) &&
00159                 capture->isOpen())
00160         {
00161             if (connect_params.counter < nimages)
00162             {
00163                 /* Update the data in the local connect_image by copying it from
00164                  * the capture output images */
00165                 rc = connect_image.update( phLiveObjectWAIT ); /* or NOWAIT */
00166                 phPRINT_RC(rc,NULL,"image.update()");
00167 
00168                 /* if the object was updated, then save the new frame */
00169                 if (rc == phLiveObjectUPDATED)
00170                 {
00171                     save_frame(&connect_params);
00172                 }
00173             }
00174 
00175             if (copy_params.counter < nimages)
00176             {
00177                 /* Go into the capture object and copy out the image data into
00178                  * the local image */
00179                 if (capture->copyImage(copy_image) == phSUCCESS)
00180                 {
00181                     save_frame(&copy_params);
00182                 }
00183             }
00184         }
00185 
00186         if ((copy_params.counter   >= nimages) && 
00187             (connect_params.counter >= nimages) &&
00188             (test)) 
00189         {
00190             system.shutdown();
00191         }
00192         else
00193         {
00194             phMSleep(100);
00195         }
00196 
00197     }
00198     if (nimages)
00199     {
00200         printf("\n");
00201         fflush(stdout);
00202     }
00203     
00204 error:
00205     rc = system.shutdown();
00206     phPRINT_RC(rc,NULL,"system.shutdown()");
00207 
00208     /* disconnect the connected image from the capture output image */
00209     /* Just do this to be clean, it will call disconnect from the constructor 
00210      * anyway */
00211     rc = connect_image.disconnect();
00212     phPRINT_RC(rc,NULL,"connect_image.disconnect");
00213     
00214     phFree(capture_path);
00215     phDelete(arg_parser);
00216 
00217     return phSUCCESS;
00218 }




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:08 2007 for phission by  doxygen 1.4.4