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

HistogramTest.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 <HistogramTest.h>
00013 #include <phission.h>
00014 
00015 /* ------------------------------------------------------------------------ */
00016 int glbl_disable_displays = 0;
00017 int glbl_print_histogram = 0;
00018 
00019 /* ------------------------------------------------------------------------ */
00020 int usage ()
00021 {
00022     printf("\n\nUsage:\n");
00023     printf("\t--help            Display usage\n");
00024     printf("\t--nodisplay       Disable the allocation, opening or any use of a display.\n");
00025     printf("\t--test <value>    Sleep <value> seconds and then close test\n");
00026     printf("\t--count <value>   Loop <value> times during histogram before sleeping\n");
00027     printf("\n\n");
00028     exit(1);
00029 }
00030         
00031 /* ------------------------------------------------------------------------ */
00032 int main(int argc, char *argv[] )
00033 {
00034     phFUNCTION("main")
00035 
00036     uint32_t        i           = 0;
00037     uint32_t        j           = 0;
00038 
00039     char            *capture_path   = NULL;
00040     int             channel         = 0;
00041     
00042     /* fractional size of the main window */
00043     const uint32_t  box_size    = 8;
00044     /* histogram rect coords */
00045     uint32_t        bw          = 0;
00046     uint32_t        bh          = 0;
00047     uint32_t        x1          = 0;
00048     uint32_t        y1          = 0;
00049     uint32_t        x2          = 0;
00050     uint32_t        y2          = 0;
00051     
00052     /* Display variables */
00053     unsigned int        nDisplays   = glbl_disable_displays ? 0 : 1;
00054     int                 displaysOpen= 1;
00055     char                title[255];
00056     phDisplayInterface  **display   = NULL;
00057     
00058     /* Filter to run in the pipeline */
00059     phHistogramData         histData;
00060     histogram_Filter        *histogram  = new histogram_Filter();
00061     convert_Filter          *convert    = new convert_Filter();
00062     gaussian3x3_Filter      *gauss      = new gaussian3x3_Filter();
00063     
00064     phColor                 outcolor    = phColorHSV24_new(240,128,128);
00065     
00066 #if defined(WIN32)
00067     VFWSource               *capture = new VFWSource();
00068 #else
00069     V4LCapture              *capture = new V4LCapture();
00070 #endif
00071 
00072     /* The filter pipeline */
00073     phPipeline              *pipeline   = new phPipeline();
00074     
00075     /* Utility class to ease the starting and stopping of displays, captures and
00076      * pipelines */
00077     phSystem                system;
00078    
00079     /* Remove the code below when using this code as an example.
00080      * 
00081      * This just checks whether "--test" has been specified with
00082      * a time value argument. It's for testing all the examples
00083      * without the need for human intervention. */
00084     int             test            = 0;
00085     int             test_loop_count = 0;
00086     int             count           = 0;
00087 
00088     phArgTable      *arg_parser = new phArgTable();
00089 
00090     rc = arg_parser->add("--test",&test,phARG_INT);
00091     phCHECK_RC(rc,NULL,"arg_parser->add");
00092     rc = arg_parser->add("--count",&count,phARG_INT);
00093     phCHECK_RC(rc,NULL,"arg_parser->add");
00094     rc = arg_parser->add("--nodisplay",&glbl_disable_displays,phARG_BOOL);
00095     phCHECK_RC(rc,NULL,"arg_parser->add");
00096     rc = arg_parser->add("--help",(void *)&usage,phARG_FUNC);
00097     phCHECK_RC(rc,NULL,"arg_parser->add");
00098     
00099     rc = arg_parser->add("--path",&capture_path,phARG_CHAR);
00100     phCHECK_RC(rc,NULL,"arg_parser->add");
00101 
00102     rc = arg_parser->add("--channel",&channel,phARG_INT);
00103     phCHECK_RC(rc,NULL,"arg_parser->add");
00104     
00105     rc = arg_parser->parse(argc,argv);
00106     phCHECK_RC(rc,NULL,"arg_parser->parse");
00107 
00108     if (test) glbl_print_histogram = 0;
00109     if (glbl_disable_displays) nDisplays = 0;
00110 
00111     /* Set up the capture device */
00112     capture->set(320,240,capture_path);
00113     capture->setChannel(channel);
00114     capture->setColour(35000);
00115     capture->setHue(22700);
00116     capture->setContrast(27000);
00117     capture->setBrightness(22000);
00118 
00119     rc = system.addCapture(capture);
00120     phPRINT_RC(rc,NULL,"system.addCapture(capture)");
00121 
00122     display = new phDisplayInterface *[nDisplays];
00123     phCHECK_NULLPTR(display,"new","new phDisplayInterface *[nDisplays];");
00124     
00125     for (i = 0; i < nDisplays; i++ )
00126     {
00127         sprintf(title,"HistogramTest[%u]",i);
00128 
00129         if (i < 0)
00130         {
00131 #if defined(PH_HAVE_SDL)
00132             display[i] = new SDLDisplay(320,240,title);
00133 #elif defined(PH_HAVE_GDI)
00134             display[i] = new GDIDisplay(320,240,title);
00135 #elif defined(PH_HAVE_X11)
00136             display[i] = new X11Display(320,240,title);
00137 #endif
00138         }
00139         else
00140         {
00141 #if defined(PH_HAVE_GDI)
00142             display[i] = new GDIDisplay(320,240,title);
00143 #elif defined(PH_HAVE_X11)
00144             display[i] = new X11Display(320,240,title);
00145 #endif
00146         }
00147 
00148         rc = system.addDisplay(display[i]);
00149         phPRINT_RC(rc,NULL,"system.addDisplay(display[i:%d])",i);
00150     }
00151 
00152     rc = system.addPipeline(pipeline);
00153     phPRINT_RC(rc,NULL,"system.addPipeline(pipeline)");
00154     
00155 
00156     /* <> Attach the displays to the live sources */
00157     /* Image -> (b)phPipeline -> (a)Processed Output Display */
00158     
00159     /* <> Attach the Pipeline output image to the display so the
00160      * display updates when a processing has been completed
00161      * for each loop */
00162     if (nDisplays > 0)
00163         display[0]->setLiveSourceInput(pipeline->getLiveSourceOutput());
00164    
00165     if (nDisplays > 1)
00166         display[1]->setLiveSourceInput(capture->getLiveSourceOutput());
00167 
00168     if (nDisplays > 2)
00169         display[2]->setLiveSourceInput(histogram->getLiveSourceOutput());
00170 
00171 
00172     /* <> Attach the image to the live source input of the
00173      * Pipeline so processing is done automatically once the 
00174      * pipeline is started. */
00175     
00176     pipeline->setLiveSourceInput(capture->getLiveSourceOutput());
00177     
00178     /* <> Add the filter(s) into the pipeline in the order they will 
00179      * be used */
00180     /* Add the gaussian blur to the pipeline */
00181     pipeline->add(gauss);
00182 
00183     /* Set the convert filter parameters to convert to HSV before histogramming */
00184     convert->set(phImageHSV24);
00185     
00186     /* Add the convert filter to the pipeline */
00187     pipeline->add(convert);
00188    
00189     /* Set the histogram parameters */
00190     bw = (capture->getWidth() / box_size);
00191     bh = (capture->getHeight() / box_size);
00192     x1 = (capture->getWidth() / 2) - (bw / 2);
00193     y1 = (capture->getHeight() / 2) - (bh / 2);
00194     x2 = x1 + bw;
00195     y2 = y1 + bh;
00196     
00197     histogram->set( x1,/* x1 */
00198                     y1,/* y1 */
00199                     x2,/* x2 */
00200                     y2,/* y2 */
00201                     32, /* bins */
00202                     1,/* draw_rect */
00203                     &outcolor /* histogram rectangle output color & format */
00204                     );
00205     /* Add the histogram to the pipeline */                  
00206     pipeline->add(histogram);
00207           
00208     rc = histData.connect(histogram->getLiveHistogramOutput());
00209     phPRINT_RC(rc,NULL,"histData.connect()");
00210 
00211     /* <> Startup the system */
00212     rc = system.startup();
00213     phPRINT_RC(rc,NULL,"system.startup()");
00214     
00215     /* <> Keep going until all the windows are closed */
00216     displaysOpen = 1;
00217     while (displaysOpen)
00218     {
00219         /* Yielding is optional. This gives up the thread's timeslice
00220          * to prevent slow response in other threads. It consumes more
00221          * CPU cycles than sleeping. Use it instead of sleeping if
00222          * this loop is processing anything */
00223         
00224         phYield();
00225 
00226         if (glbl_print_histogram)
00227         {
00228             /* Get the most recent histogram data */
00229             if (histData.update() == phLiveObjectUPDATED)
00230             {
00231                 histData.print_data();
00232             }
00233         }
00234         
00235         if (nDisplays > 0)
00236         {
00237             displaysOpen = 0;
00238 
00239             for (i = 0; (i < nDisplays) && (displaysOpen == 0); i++)
00240             {
00241                 if (display[i]->isOpen() == 1)
00242                 {
00243                     displaysOpen = 1;
00244                 }
00245             }
00246         }
00247         /* Remove this if block when using this code as an example */
00248         /* Set the loop control value to end the loop when testing */
00249         if (test > 0)
00250         { 
00251             /* Let the program get a couple hist. data objects and print them*/
00252             if (test_loop_count > count)
00253             {
00254                 displaysOpen = 0;
00255                 phSleep(test); /* test's value should be a time (in secs) value > 0*/
00256             }
00257             test_loop_count++;
00258         }
00259     }
00260 
00261     /* <> Shutdown the system */
00262     rc = system.shutdown();
00263     phPRINT_RC(rc,NULL,"system.shutdown()");
00264 
00265 error:
00266     rc = histData.disconnect();
00267     phPRINT_RC(rc,NULL,"histData.disconnect()");
00268     
00269     for (i = 0; (i < nDisplays) && (display != NULL); i++)
00270     {
00271         phDelete(display[i]);
00272     }
00273     
00274     phDeleteArray(display);
00275 
00276     phDelete(histogram);
00277     phDelete(convert);
00278     phDelete(gauss);
00279     
00280     phDelete(pipeline);
00281     phDelete(capture);
00282 
00283     phFree(capture_path);
00284     phDelete(arg_parser);
00285 
00286     return phSUCCESS;
00287 }




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