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

MLDataCollection.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 <MLDataCollection.h>
00013 #include <phission.h>
00014 
00015 /* ------------------------------------------------------------------------ */
00016 int glbl_verbose = 0;
00017 
00018 /* ------------------------------------------------------------------------ */
00019 void usage()
00020 {
00021     printf("\n\n Usage:\n");
00022     printf("\t--help\t\t\tdisplay usage\n");
00023     printf("\t-v/--verbose <level>\t\t\tPrints more information w/increasing level :)\n");
00024     printf("\t--filename <str>\tprefix to append to the filename (default:img)\n");
00025     printf("\t-f\t\t\tsame as --filename\n");
00026     printf("\t--type <str>\t\tjpg/jpeg/ppm              (default:ppm)\n");
00027     printf("\t-t\t\t\tsame as --filetype\n");
00028     printf("\t--fps\t\t\tset frame per second capture rate (1-30)\n");
00029     printf("\t--nimages <int>\t\tNumber of images to grab (default:0/continuous)\n");
00030     printf("\t-ni <int>\t\tSame as --nimages\n");
00031     printf("\t-c  \t\t\tset the color value       (0-65535)\n");
00032     printf("\t-h  \t\t\tset the hue value         (0-65535)\n");
00033     printf("\t-br \t\t\tset the brightness value  (0-65535)\n");
00034     printf("\t-con\t\t\tset the contrast value    (0-65535)\n");
00035     printf("\t-col\t\t\tset the color value       (0-65535)\n");
00036     printf("\n\n");
00037 
00038     exit(0);
00039 }
00040 
00041 /* ------------------------------------------------------------------------ */
00042 int save_frame( phImage     *img,        /* pointer to the image object      */
00043                 const char  *prefix,     /* unique-id for the particular img */
00044                 const char  *filetype,   /* "ppm" or "jpg"                   */
00045                 int         *counter,    /* frame number count               */
00046                 int         allow_newline/* allow this function to output \n */
00047                 )
00048 {
00049     char            filename[255];
00050     if ((img->isNull() == 0) && 
00051         (img->getWidth() != 0) &&
00052         (img->getHeight() != 0))
00053     {
00054         /* Create the filename */
00055         sprintf(filename,"%s_%04d.%s",prefix,*counter,filetype);
00056 
00057         /* print the frame we captured */
00058         if (glbl_verbose > 1)
00059         {
00060             printf("%s:%4d\n",prefix,*counter);
00061             fflush(stdout);
00062         }
00063 
00064         /* Save the file */
00065         img->save(filename);
00066 
00067         /* Increment the frame number */
00068         (*counter)++;
00069     }
00070 
00071     return phSUCCESS;
00072 }
00073 
00074 /* ------------------------------------------------------------------------ */
00075 int main(int argc, char *argv[] )
00076 {
00077     phFUNCTION("main")
00078 
00079     uint32_t        width       = 320;
00080     uint32_t        height      = 240;
00081     uint32_t        channel     = 1;
00082     uint32_t        format      = phImageRGB24;
00083     char           *capture_path= NULL;
00084     
00085     int32_t color       = 57000;
00086     int32_t hue         = 34500;
00087     int32_t contrast    = 36000;
00088     int32_t brightness  = 36000;
00089 
00090     char    *fileprefix = NULL;
00091     char    *filetype   = NULL;
00092 
00093     int     fps             = 0;
00094     int     fps_sleeptime   = 0;
00095 
00096     /* phLiveObject::connect, phLiveObject::update methods */
00097     phImage         connect_image;
00098     int             connect_frame_num = 0;
00099 
00100     int             i           = 0;
00101     int             nimages = 0;
00102 
00103     X11Display      *display = NULL;
00104 #ifdef WIN32
00105     VFWSource       *capture = new VFWSource();
00106 #else
00107     V4LCapture      *capture = new V4LCapture();
00108 #endif
00109         
00110     /* Utility class to ease the starting and stopping of displays, captures and
00111      * pipelines */
00112     phSystem        system;
00113    
00114     phArgTable      *arg_parser = new phArgTable();
00115     
00116     /* Setup and parse all the arguments */
00117     rc = arg_parser->add("--help",(void *)&usage,phARG_FUNC);
00118     phCHECK_RC(rc,NULL,"arg_parser->add");
00119 
00120     rc = arg_parser->add("-v",&glbl_verbose,phARG_INT);
00121     phCHECK_RC(rc,NULL,"arg_parser->add");
00122     rc = arg_parser->add("--verbose",&glbl_verbose,phARG_INT);
00123     phCHECK_RC(rc,NULL,"arg_parser->add");
00124     
00125     rc = arg_parser->add("--file",&fileprefix,phARG_CHAR);
00126     phCHECK_RC(rc,NULL,"arg_parser->add");
00127     rc = arg_parser->add("-f",&fileprefix,phARG_CHAR);
00128     phCHECK_RC(rc,NULL,"arg_parser->add");
00129     
00130     rc = arg_parser->add("--type",&filetype,phARG_CHAR);
00131     phCHECK_RC(rc,NULL,"arg_parser->add");
00132     rc = arg_parser->add("-t",&filetype,phARG_CHAR);
00133     phCHECK_RC(rc,NULL,"arg_parser->add");
00134     
00135     rc = arg_parser->add("--fps",&fps,phARG_INT);
00136     phCHECK_RC(rc,NULL,"arg_parser->add");
00137     
00138     rc = arg_parser->add("--nimages",&nimages,phARG_INT);
00139     phCHECK_RC(rc,NULL,"arg_parser->add");
00140     rc = arg_parser->add("-ni",&nimages,phARG_INT);
00141     phCHECK_RC(rc,NULL,"arg_parser->add");
00142     
00143     rc = arg_parser->add("-col",&color,phARG_INT);
00144     phCHECK_RC(rc,NULL,"arg_parser->add");
00145     rc = arg_parser->add("-h",&hue,phARG_INT);
00146     phCHECK_RC(rc,NULL,"arg_parser->add");
00147     rc = arg_parser->add("-con",&contrast,phARG_INT);
00148     phCHECK_RC(rc,NULL,"arg_parser->add");
00149     rc = arg_parser->add("-br",&brightness,phARG_INT);
00150     phCHECK_RC(rc,NULL,"arg_parser->add");
00151     rc = arg_parser->add("-c",&channel,phARG_INT);
00152     phCHECK_RC(rc,NULL,"arg_parser->add");
00153     
00154     rc = arg_parser->parse(argc,argv);
00155     phCHECK_RC(rc,NULL,"arg_parser->parse");
00156  
00157     /* Set up the capture parameters */
00158     capture->set        ( width, height, path );
00159     capture->setChannel ( channel );
00160     capture->setFormat  ( format  );
00161     
00162     capture->setColour(color);
00163     capture->setHue(hue);
00164     capture->setContrast(contrast);
00165     capture->setBrightness(brightness);
00166     
00167     if ((fps > 0) && (fps < 30)) fps_sleeptime = (1000000 / fps); /* time in us */
00168     if (nimages < 0) nimages = 5;
00169    
00170     /* Add the display for image/object alignment within the scene */
00171     display = new X11Display(width,height, path);
00172     phCHECK_NULLPTR(display,"new","new X11Display failed.");
00173   
00174     rc = display->setInput(capture->getOutput());
00175     phCHECK_RC(rc,NULL,"display->setInput");
00176     
00177     rc = system.addDisplay( display );
00178     phPRINT_RC(rc,NULL,"system.addDisplay( display )");
00179     
00180     /* Add the capture to the system to make starting and stopping easy */
00181     rc = system.addCapture( capture );
00182     phPRINT_RC(rc,NULL,"system.addCapture( capture )");
00183     
00184     /* connect the image to the output of the capture class. 
00185      * The capture class has an phImage object (derived from phLiveObject) */
00186     rc = connect_image.connect( capture->getOutput() );
00187     phPRINT_RC(rc,NULL,"image.connect()");
00188     
00189     /* startup */
00190     rc = system.startup();
00191     phPRINT_RC(rc,NULL,"system.startup()");
00192    
00193     if (glbl_verbose > 0)
00194     {
00195         phPRINT("Saving frames with the following settings:\n");
00196         phPRINT("color      :%6d\n",color);
00197         phPRINT("hue        :%6d\n",hue);
00198         phPRINT("brightness :%6d\n",brightness);
00199         phPRINT("contrast   :%6d\n",contrast);
00200         phPRINT("file prefix:%6s\n",fileprefix);
00201         phPRINT("file type  :%6s\n",filetype);
00202         phPRINT("fps        :%6d\n",fps);
00203         phPRINT("sleep (us) :%6d\n",fps_sleeptime);
00204     }
00205     
00206     while (system.displaysOpen() > 0)
00207     {
00208         phMSleep(10);
00209     }
00210 
00211     if (nimages == 0)
00212     {
00213         rc = display->open();
00214         phPRINT_RC(rc,NULL,"display->open");
00215     }
00216 
00217     while (((connect_frame_num < nimages) || (nimages == 0)) && 
00218            ((system.displaysOpen() > 0)   || (nimages != 0)) &&
00219            (capture->isOpen() > 0))
00220     {
00221         if ((connect_frame_num < nimages) ||
00222             (nimages == 0))
00223         {
00224             /* Update the data in the local connect_image by copying it from
00225              * the capture output images */
00226             rc = connect_image.update( phLiveObjectWAIT ); /* or NOWAIT */
00227             phPRINT_RC(rc,NULL,"image.update()");
00228 
00229             sync();
00230 
00231             /* if the object was updated, then save the new frame */
00232             if (rc == phLiveObjectUPDATED)
00233             {
00234                 save_frame(&connect_image,
00235                            (fileprefix  == NULL ? "img" : fileprefix),
00236                            /* type - ppm or jpeg/jpg */
00237                            (filetype    == NULL ? "ppm" : filetype), 
00238                            &connect_frame_num,
00239                            0 );
00240 
00241                 /* if we're not capturing 30 fps, we're sleeping */
00242                 if (fps_sleeptime) phUSleep(fps_sleeptime);
00243                 else phYield();
00244             }
00245         }
00246     }
00247 
00248     phPRINT("\n");
00249 
00250 error:
00251     rc = system.shutdown();
00252     phPRINT_RC(rc,NULL,"system.shutdown()");
00253 
00254     /* disconnect the connected image from the capture output image */
00255     /* Just do this to be clean, it will call disconnect from the constructor 
00256      * anyway */
00257     rc = connect_image.disconnect();
00258     phPRINT_RC(rc,NULL,"connect_image.disconnect");
00259 
00260     phDelete(capture);
00261     phDelete(display);
00262     phFree(fileprefix);
00263     phFree(filetype);
00264     
00265     phFree(capture_path);
00266     phDelete(arg_parser);
00267 
00268     return phSUCCESS;
00269 }




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:06 2007 for phission by  doxygen 1.4.4