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

DDImageTest.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 <DDImageTest.h>
00013 #include <phission.h>
00014 
00015 /* ------------------------------------------------------------------------ */
00016 int glbl_disable_displays   = 0;
00017 int glbl_do_timing          = 0;
00018 int glbl_threshold          = 15;
00019 int glbl_stride             = 2;
00020 int glbl_pixel_threshold    = 4;
00021 int glbl_block_filter       = ddimage_BlockKernel;
00022 int glbl_set_test           = 0;
00023 #if PH_HAVE_AVCODEC
00024 char *glbl_filename         = NULL;
00025 #endif
00026 
00027 /* ------------------------------------------------------------------------ */
00028 void usage()
00029 {
00030     printf("\n\nUsage:\n");
00031     printf("\t--help\t\t\tdisplay usage\n");
00032     printf("\t--nodisplay\tdisable the allocation, opening or any use of a display.\n");
00033     printf("\t--test <value>\tsleep 'value' seconds and then close test\n");
00034     printf("\t--stride/-s <stride> The stride between differenced frames\n");
00035     printf("\t--pixels/-p <threshold0-16> The number of pixels that must be\n");
00036     printf("\t                            present in the 4x4 block filter.\n");
00037     printf("\t--block_filter/-b <1:2> 1-ddimage_BlockStandard 2-ddimage_BlockKernel\n");
00038     printf("\t                        Chooses the type of 4x4 block filter operation\n");
00039     printf("\t                        (default:2)\n");
00040     printf("\t--threshold/-t <value0-255> The threshold to determine if the\n");
00041     printf("\t                            differenced frame pixels are different \n");
00042     printf("\t                            enough to have motion\n");
00043     printf("\t--settest             Test the ddimage_Filter::set() method by\n");
00044     printf("\t                      alternating the values. If combined with\n");
00045     printf("\t                      --test 1, will perform one loop through all values\n");
00046     printf("\n\n");
00047     
00048     exit(1);
00049 }
00050 
00051 /* ------------------------------------------------------------------------ */
00052 /* ------------------------------------------------------------------------ */
00053 int main(int argc, char *argv[] )
00054 {
00055     phFUNCTION("main")
00056 
00057     uint32_t            i           = 0;
00058     uint32_t            width       = 320;
00059     uint32_t            height      = 240;
00060     uint32_t            nDisplays   = 3;
00061     int                 displaysOpen= 1;
00062     char                title[255];
00063     phDisplayInterface  **display   = NULL;
00064     phImageCapture      *capture    = NULL;
00065 
00066     phPipeline          *pipeline   = new phPipeline();
00067    
00068     gaussianBlur_Filter *gauss      = new gaussianBlur_Filter();
00069     convert_Filter      *convert    = new convert_Filter(phImageGREY8);
00070     ddimage_Filter      *ddimage    = NULL;
00071     
00072     char            *capture_path   = NULL;
00073     int             channel         = 0;
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 
00086     phArgTable      *arg_parser = new phArgTable();
00087 
00088     /* Setup and parse all the arguments */
00089     rc = arg_parser->add("--test",&test,phARG_INT);
00090     phCHECK_RC(rc,NULL,"arg_parser->add");
00091     rc = arg_parser->add("--nodisplay",&glbl_disable_displays,phARG_BOOL);
00092     phCHECK_RC(rc,NULL,"arg_parser->add");
00093     rc = arg_parser->add("--help",(void *)&usage,phARG_FUNC);
00094     phCHECK_RC(rc,NULL,"arg_parser->add");
00095     
00096     rc = arg_parser->add("--timing",&glbl_do_timing,phARG_BOOL);
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->add("--threshold",&glbl_threshold,phARG_INT);
00106     phCHECK_RC(rc,NULL,"arg_parser->add");
00107     rc = arg_parser->add("-t",&glbl_threshold,phARG_INT);
00108     phCHECK_RC(rc,NULL,"arg_parser->add");
00109     
00110     rc = arg_parser->add("--pixels",&glbl_pixel_threshold,phARG_INT);
00111     phCHECK_RC(rc,NULL,"arg_parser->add");
00112     rc = arg_parser->add("-p",&glbl_pixel_threshold,phARG_INT);
00113     phCHECK_RC(rc,NULL,"arg_parser->add");
00114     
00115     rc = arg_parser->add("--stride",&glbl_stride,phARG_INT);
00116     phCHECK_RC(rc,NULL,"arg_parser->add");
00117     rc = arg_parser->add("-s",&glbl_stride,phARG_INT);
00118     phCHECK_RC(rc,NULL,"arg_parser->add");
00119     
00120     rc = arg_parser->add("--block",&glbl_block_filter,phARG_INT);
00121     phCHECK_RC(rc,NULL,"arg_parser->add");
00122     rc = arg_parser->add("-b",&glbl_block_filter,phARG_INT);
00123     phCHECK_RC(rc,NULL,"arg_parser->add");
00124 
00125     rc = arg_parser->add("--settest",&glbl_set_test,phARG_BOOL);
00126     phCHECK_RC(rc,NULL,"arg_parser->add");
00127 
00128 #if PH_HAVE_AVCODEC
00129     rc = arg_parser->add("--file",&glbl_filename,phARG_CHAR);
00130     phCHECK_RC(rc,NULL,"arg_parser->add");
00131 #endif
00132     
00133     rc = arg_parser->parse(argc,argv);
00134     phCHECK_RC(rc,NULL,"arg_parser->parse");
00135     
00136     if (glbl_disable_displays) nDisplays = 0;
00137     if (test) glbl_do_timing = 1;
00138     
00139     if (glbl_threshold > 255)       glbl_threshold = 255;
00140     if (glbl_threshold < 0)         glbl_threshold = 0;
00141     if (glbl_pixel_threshold > 16)  glbl_pixel_threshold = 16;
00142     if (glbl_pixel_threshold < 0)   glbl_pixel_threshold = 0;
00143     ddimage    = new ddimage_Filter(glbl_threshold,
00144                                     glbl_stride,
00145                                     glbl_pixel_threshold,
00146                                     glbl_block_filter );
00147 
00148     /* 1.) Setup the capture device parameters */
00149 #if PH_HAVE_AVCODEC
00150     if (glbl_filename != NULL)
00151     {
00152         capture = new phAvcodecSource();
00153         capture->setPath(glbl_filename);
00154         //capture->set(width,height,glbl_filename);
00155         width = capture->getWidth();
00156         height = capture->getHeight();
00157         phFree(glbl_filename);
00158     }
00159     else
00160 #endif
00161     {
00162 #if defined(WIN32)
00163         capture = new VFWSource();
00164 #else
00165         capture = new V4LCapture();
00166 #endif
00167         capture->set(width,height,capture_path);
00168         capture->setChannel(channel);
00169         capture->setBrightness(28000);
00170         capture->setContrast(28000);
00171         capture->setColor(52000);
00172         capture->setHue(32000);
00173     }
00174     rc = system.addCapture(capture);
00175     phPRINT_RC(rc,NULL,"system.addCapture(capture)");
00176     
00177     display = new phDisplayInterface *[nDisplays];
00178     phCHECK_NULLPTR(display,"new","new phDisplayInterface *[nDisplays];");
00179 
00180     for (i = 0; i < nDisplays; i++ )
00181     {
00182         sprintf(title,"DDImageTest[%u]",i);
00183         
00184 #if defined(WIN32)
00185         display[i] = new GDIDisplay(width,height,title);
00186 #else
00187         display[i] = new X11Display(width,height,title);
00188 #endif
00189         display[i]->move(10,100+(i*width));
00190 
00191         rc = system.addDisplay(display[i]);
00192         phPRINT_RC(rc,NULL,"system.addDisplay(display[%u])",i);
00193     }
00194     
00195     rc = system.addPipeline(pipeline);
00196     phPRINT_RC(rc,NULL,"system.addPipeline(pipeline)");
00197 
00198     
00199     /* <> Attach the displays to the live sources */
00200     /* Capture -> (b)phPipeline -> (a)Processed Output Display */
00201     
00202     /* <> Attach the Pipeline output image to the display so the
00203      * display updates when a processing has been completed
00204      * for each loop */
00205     if (nDisplays > 0)
00206         display[0]->setLiveSourceInput(pipeline->getLiveSourceOutput());
00207     
00208     if (nDisplays > 1)
00209         display[1]->setLiveSourceInput(capture->getLiveSourceOutput());
00210     
00211     if (nDisplays > 2)
00212         display[2]->setLiveSourceInput(ddimage->getMaskImage());
00213     /* <> Attach the capture image to the live source input of the
00214      * Pipeline so processing is done automatically once the 
00215      * pipeline is started. */
00216     pipeline->setLiveSourceInput(capture->getLiveSourceOutput());
00217     
00218     /* <> Add the filter(s) into the pipeline in the order they will 
00219      * be used */
00220     pipeline->add(convert);
00221     pipeline->add(ddimage);
00222 
00223     /* Since timing in the pipeline is disabled by default, enable it */
00224     if (glbl_do_timing)
00225     {
00226         rc = pipeline->enableTiming();
00227         phPRINT_RC(rc,NULL,"pipeline->enableTiming()");
00228     }
00229 
00230     /* <> Startup the system */
00231     rc = system.startup();
00232     phPRINT_RC(rc,NULL,"system.startup()");
00233     
00234     /* <> Keep going until all the windows are closed */
00235     displaysOpen = 1;
00236     while (displaysOpen)
00237     {
00238         if (glbl_set_test)
00239         {
00240             glbl_threshold += 10;
00241             /* The absolute maximum for threshold is 255, but let's not go that
00242              * far because it won't give motion outline results anyway */
00243             if (glbl_threshold > 225)
00244             {
00245                 /* Set glbl_threshold to the program default */
00246                 glbl_threshold = 15;
00247                 
00248                 glbl_pixel_threshold++;
00249                 /* The absolute maximum for pixel threshold is 16, but let's not
00250                  * go that far because it won't give good motion results anyway
00251                  * */
00252                 if (glbl_pixel_threshold > 14)
00253                 {
00254                     /* 0 == no block filtering */
00255                     glbl_pixel_threshold = 0;
00256                 
00257                     glbl_block_filter = 
00258                         glbl_block_filter == ddimage_BlockStandard ? 
00259                                 ddimage_BlockKernel : ddimage_BlockStandard;
00260 
00261                     glbl_stride++;
00262                     /* Let's restrice ourselves to a stride of 10 */
00263                     if (glbl_stride > 10)
00264                     {
00265                         glbl_stride = 1;
00266                         /* let the program know it's time to end the example if
00267                          * we're in test mode */
00268                         glbl_set_test++;
00269                     }
00270                 }
00271             }
00272         
00273 #if 1
00274             phPROGRESS("thresh:%u pixthresh:%u block_filter:%s stride:%u\n",
00275                         glbl_threshold,
00276                         glbl_pixel_threshold,
00277                         (glbl_block_filter == ddimage_BlockKernel) ? 
00278                             "ddimage_BlockKernel" : "ddimage_BlockStandard",
00279                         glbl_stride );
00280 #endif
00281                         
00282             rc = ddimage->set(glbl_threshold,
00283                               glbl_stride,
00284                               glbl_pixel_threshold,
00285                               glbl_block_filter );
00286             phPRINT_RC(rc,NULL,"ddimage->set");
00287 
00288             /* We need to wait at least 2 seconds, otherwise we won't be
00289              * testing the current settings long enough */
00290             phMSleep(2000);
00291         }
00292         if (glbl_do_timing)
00293         {
00294             /* Reset the average calculations and sleep a second */
00295             rc = pipeline->resetAverage();
00296             phPRINT_RC(rc,NULL,"pipeline->resetAverage()");
00297             
00298             phMSleep(1000);
00299             
00300             /* Report the average using the phTimeStamp report facility
00301              * to allow for portable/robust reporting */
00302             pipeline->averageRuntime().report("DDImageTest - Filtering Average");
00303             phPRINT_RC(rc,NULL,"pipeline->averageRuntime()");
00304         }
00305         else
00306         {
00307             /* Sleep a while, don't loop tightly */
00308             phMSleep(100);
00309         }
00310         
00311         /* Yielding is optional. This gives up the thread's timeslice
00312          * to prevent slow response in other threads. It consumes more
00313          * CPU cycles than sleeping. Use it instead of sleeping if
00314          * this loop is processing anything */
00315         
00316         /* phYield(); */
00317    
00318         if (nDisplays > 0)
00319         {
00320             displaysOpen = 0;
00321             for (i = 0; (i < nDisplays) && (displaysOpen == 0); i++)
00322             {
00323                 if (display[i]->isOpen() == 1)
00324                 {
00325                     displaysOpen = 1;
00326                 }
00327             }
00328         }
00329         
00330         /* Remove this if block when using this code as an example */
00331         /* Set the loop control value to end the loop when testing */
00332         if (test > 0)
00333         { 
00334             if (!glbl_set_test)
00335             {
00336                 displaysOpen = 0;
00337                 /* test's value should be a time (in secs) value > 0*/
00338                 phSleep(test); 
00339             }
00340             else if (glbl_set_test > 1)
00341             {
00342                 displaysOpen = 0;
00343             }
00344         }
00345     }
00346 
00347 error:
00348     /* <> Shutdown the system */
00349     rc = system.shutdown();
00350     phPRINT_RC(rc,NULL,"system.shutdown()");
00351 
00352     for (i = 0; (i < nDisplays) && (display != NULL); i++)
00353     {
00354         phDelete(display[i]);
00355     }
00356   
00357     phDeleteArray(display);
00358     phDelete(capture);
00359     phDelete(ddimage);
00360     phDelete(convert);
00361     phDelete(gauss);
00362     phDelete(pipeline);
00363 
00364     phFree(capture_path);
00365     phDelete(arg_parser);
00366 
00367     return phSUCCESS;
00368 }




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