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

BlobTest.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 <BlobTest.h>
00013 #include <phission.h>
00014 
00015 /* ------------------------------------------------------------------------ */
00016 int     glbl_disable_displays       =   0;
00017 int     glbl_print_blobs            =   1;
00018 int     glbl_use_images             =   0;
00019 int     glbl_users_supplied_image   =   0;
00020 char    *glbl_users_imagename       =NULL;
00021 int     glbl_save_images            =   0;
00022 int     glbl_use_netdisplay         =   0;
00023 
00024 /* ------------------------------------------------------------------------ */
00025 void usage()
00026 {
00027     printf("\n\nUsage:\n");
00028     printf("\t--help                    Display usage\n");
00029     printf("\t--net                     Use the network display\n");
00030     printf("\t--nodisplay               Disable the allocation, opening or any use of a display.\n");
00031     printf("\t--test <value>            Iterate <value> times through the loop\n");
00032     printf("\t--save                    Save pipeline output images (0 by default)\n");
00033     printf("\t--images                  Use images (default is not to use images)\n");
00034     printf("\t--userimage <filename>    Use this file as input only. Not capture. Pump it through the pipeline.\n");
00035     printf("\t--path <pathstr>          Path to the input device\n");
00036     printf("\t--channel <value>         The channel to set a capture card to\n");
00037     printf("\n\n");
00038     exit(1);
00039 }
00040 
00041 /* ------------------------------------------------------------------------ */
00042 int main(int argc, char *argv[] )
00043 {
00044     phFUNCTION("main")
00045 
00046     unsigned int        i           = 0;
00047 
00048     /* Used to read in test image for blobbing */
00049     /* Note: If you use loaded_image for anything when glbl_use_images or 
00050              glbl_users_supplied_image is set, the image will be signalled and 
00051              copied to the pipeline and any listening class. */
00052     phImage             loaded_image;
00053     unsigned int        j           = 0;
00054     unsigned int        image_count = 2;
00055     phImage             *fileImages = NULL;
00056     char                filename[255];
00057     
00058     /* Used for saving images. This prevents us from signalling any 
00059        objects listening to loaded_image */
00060     phImage             saveimage;
00061     /* we don't want this object to notify any clients */
00062     saveimage.disableNotify();
00063     
00064     /* Display variables */
00065     unsigned int        nDisplays   = 2;
00066     int                 displaysOpen= 1;
00067     char                title[255];
00068     phDisplayInterface  **display   = NULL;
00069 
00070     unsigned int        port = 22345;
00071     
00072     char            *capture_path   = NULL;
00073     int             channel         = 0;
00074     
00075    
00076 #ifdef WIN32
00077     VFWSource           *capture = new VFWSource();
00078 #else
00079     V4LCapture          *capture = new V4LCapture();
00080 #endif
00081 
00082     /* The filter pipeline */
00083     phPipeline          *pipeline = new phPipeline();
00084 
00085     /* Filters to run in the pipeline */
00086     gaussian3x3_Filter  *gauss      = new gaussian3x3_Filter();
00087     blob_Filter         *blob_find  = new blob_Filter();
00088     const int32_t       maxColors   = 2;
00089     int32_t             nColors     = 0;
00090     phColor             incolors[maxColors];
00091     phColor             thresholds[maxColors];
00092     phColor             outcolor;
00093     
00094     /* blob data output from the blob filter */
00095     phBlobData          blob_data;
00096     const int32_t       minimum_size= 0;
00097     
00098     /* Utility class to ease the starting and stopping of displays, captures and
00099      * pipelines */
00100     phSystem            *system     = new phSystem();
00101 
00102     /* Remove the code below when using this code as an example.
00103      * 
00104      * This just checks whether "--test" has been specified with
00105      * a time value argument. It's for testing all the examples
00106      * without the need for human intervention. */
00107     int             test = 0;
00108 
00109     phArgTable      *arg_parser = new phArgTable();
00110 
00111     rc = arg_parser->add("--images",&glbl_use_images, phARG_BOOL);
00112     phCHECK_RC(rc,NULL,"arg_parser->add");
00113 
00114     rc = arg_parser->add("--net",&glbl_use_netdisplay, phARG_BOOL);
00115     phCHECK_RC(rc,NULL,"arg_parser->add");
00116 
00117     rc = arg_parser->add("--save",&glbl_save_images, phARG_BOOL);
00118     phCHECK_RC(rc,NULL,"arg_parser->add");
00119 
00120     rc = arg_parser->add("--nodisplay",&glbl_disable_displays, phARG_BOOL);
00121     phCHECK_RC(rc,NULL,"arg_parser->add");
00122 
00123     rc = arg_parser->add("--test",&test, phARG_INT);
00124     phCHECK_RC(rc,NULL,"arg_parser->add");
00125 
00126     rc = arg_parser->add("--userimage",&glbl_users_imagename, phARG_CHAR);
00127     phCHECK_RC(rc,NULL,"arg_parser->add");
00128 
00129     rc = arg_parser->add("--help",(void *)&usage, phARG_FUNC);
00130     phCHECK_RC(rc,NULL,"arg_parser->add");
00131     
00132     rc = arg_parser->add("--path",&capture_path,phARG_CHAR);
00133     phCHECK_RC(rc,NULL,"arg_parser->add");
00134 
00135     rc = arg_parser->add("--channel",&channel,phARG_INT);
00136     phCHECK_RC(rc,NULL,"arg_parser->add");
00137     
00138     rc = arg_parser->parse(argc,argv);
00139     phCHECK_RC(rc,NULL,"arg_parser->parse");
00140 
00141    
00142     /* if the user supplied a filename different from the default test
00143      * images, then use it */
00144     if (glbl_users_imagename != NULL)
00145     {
00146         glbl_users_supplied_image   = 1;
00147         glbl_use_images             = 0;
00148         image_count                 = 1;
00149     }
00150 
00151     /* Print information if the test option was given */
00152     if (test > 0)
00153     {
00154         glbl_print_blobs = 1;
00155     }
00156 
00157     /* Allocate an array to load images into if we're using images */
00158     if (image_count > 0)
00159     {
00160         fileImages = new phImage[image_count];
00161     }
00162 
00163     /* If the command line switch to disable the displays was given, then
00164      * don't allocate or open any displays */
00165     if (glbl_disable_displays) nDisplays = 0;
00166     
00167     /* Load the user files */
00168     if (glbl_users_supplied_image == 1)
00169     {
00170          /* load the images */
00171          rc = fileImages[0].load(glbl_users_imagename);
00172          phCHECK_RC(rc,NULL,"image loading failed");
00173 
00174          image_count = 1;
00175     }
00176     /* Load the default test images */
00177     else if (glbl_use_images == 1)
00178     {
00179         /* load the images */
00180         for (i = 0; i < image_count; i++)
00181         {
00182             //sprintf(filename,"../images/thisisatest_%04d.ppm",i);
00183             sprintf(filename,"../images/ColoredCells_%04d.ppm",i);
00184         
00185             rc = fileImages[i].load(filename);
00186             phCHECK_RC(rc,NULL,"image loading failed");
00187         }
00188     }
00189     /* Otherwise, set up the capture device parameters */
00190     else
00191     {
00192         capture->set(320,240,capture_path );
00193         capture->setChannel(channel);
00194         capture->setColour(35000);
00195         capture->setHue(22700);
00196         capture->setContrast(27000);
00197         capture->setBrightness(22000);
00198     
00199         rc = system->addCapture(capture);
00200         phPRINT_RC(rc,NULL,"system->addCapture(capture)");
00201     }
00202     
00203     if (nDisplays > 0)
00204     {
00205         display = new phDisplayInterface *[nDisplays];
00206         phCHECK_NULLPTR(display,"new","new phDisplayInterface *[nDisplays];");
00207     }
00208 
00209     for (i = 0; i < nDisplays; i++ )
00210     {
00211         sprintf(title,"BlobTest[%u]",i);
00212 
00213         if (glbl_use_netdisplay)
00214         {
00215             NetDisplay *n = new NetDisplay(320,240,title);
00216             display[i] = (phDisplayInterface *)n;
00217             n->setPort(port+i);
00218         }
00219         else
00220         {
00221 #if defined(PH_HAVE_X11)
00222             display[i] = new X11Display(320,240,title);
00223 #elif defined(PH_HAVE_GDI)
00224             display[i] = new GDIDisplay(320,240,title);
00225 #endif
00226         }
00227 
00228         rc = system->addDisplay(display[i]);
00229         phPRINT_RC(rc,NULL,"system->addDisplay(display[i:%d])",i);
00230     }
00231     
00232     rc = system->addPipeline(pipeline);
00233     phPRINT_RC(rc,NULL,"system->addPipeline(pipeline)");
00234     
00235     /* <> Attach the displays to the live sources */
00236     /* Image -> (b)phPipeline -> (a)Processed Output Display */
00237     
00238     /* <> Attach the Pipeline output image to the display so the
00239      * display updates when a processing has been completed
00240      * for each loop */
00241     if (nDisplays > 0)
00242         display[0]->setLiveSourceInput(pipeline->getLiveSourceOutput());
00243    
00244     if (nDisplays > 1)
00245     { 
00246         if ((glbl_use_images == 1) || (glbl_users_supplied_image == 1))
00247         {
00248             display[1]->setLiveSourceInput(&loaded_image);
00249         }
00250         else
00251         {
00252             display[1]->setLiveSourceInput(capture->getLiveSourceOutput());
00253         }
00254     }
00255 
00256     /* <> Attach the image to the live source input of the
00257      * Pipeline so processing is done automatically once the 
00258      * pipeline is started. */
00259     if ((glbl_use_images == 1) || (glbl_users_supplied_image == 1))
00260     {
00261         pipeline->setLiveSourceInput(&loaded_image);
00262     }
00263     else
00264     {
00265         pipeline->setLiveSourceInput(capture->getLiveSourceOutput());
00266     }
00267     
00268     if (glbl_use_images == 1)
00269     {
00270         incolors[0]     = phColorRGB24_new(255,0,0);
00271         thresholds[0]   = phColorRGB24_new(0,0,0);
00272 
00273         nColors = 1;
00274     }
00275     else
00276     {
00277         pipeline->add(gauss);
00278     
00279         incolors[0]     = phColorHSV24_new(0,200,200);
00280         thresholds[0]   = phColorHSV24_new(20,40,40);
00281     
00282         nColors = 1;
00283 
00284         /*
00285         incolors[0]     = phColorHSV24_new(240,195,128);
00286         thresholds[0]   = phColorHSV24_new(40,65,128);
00287         */
00288         
00289         /*
00290         incolors[1]     = phColorHSV24_new(0,200,128);
00291         thresholds[1]   = phColorHSV24_new(0,60,120);
00292         */
00293     }
00294     
00295     outcolor        = phColorRGB24_new(0,255,255);
00296     
00297     blob_find->set(incolors,thresholds,nColors,&outcolor,1,1);
00298     pipeline->add(blob_find);
00299     
00300     rc = blob_data.connect(blob_find->getLiveBlobOutput());
00301     phPRINT_RC(rc,NULL,"blob_data.connect()");
00302 
00303     /* <> Startup the system */
00304     rc = system->startup();
00305     phPRINT_RC(rc,NULL,"system->startup()");
00306 
00307     /* <> Keep going until all the windows are closed */
00308     displaysOpen = 1;
00309     while (displaysOpen)
00310     {
00311         if (glbl_save_images == 1)
00312         {
00313             if ((glbl_use_images == 0) && (glbl_users_supplied_image == 0))
00314             {
00315                 capture->copyImage(saveimage);
00316                 if (saveimage.isNull() == 0)
00317                     saveimage.save("tempin.ppm");
00318             }
00319 
00320             pipeline->copyOutputImage(saveimage);
00321             if (saveimage.isNull() == 0)
00322                 saveimage.save("tempout.ppm");
00323         }
00324 
00325         if ((glbl_use_images == 1) || (glbl_users_supplied_image == 1))
00326         {
00327             rc = loaded_image.setImage(fileImages[j]);
00328             phPRINT_RC(rc,NULL,"setImage failed");
00329             if (rc != 0) { displaysOpen = 0; continue; }
00330         
00331             j++;
00332             if (j >= image_count) j = 0;
00333         }
00334         
00335         if (glbl_print_blobs == 1)
00336         {
00337             /* Get the most recent blob data */
00338             if ((rc = blob_data.update()) == phLiveObjectUPDATED)
00339             {
00340                 blob_data.print_data(minimum_size);
00341             }
00342         }
00343         else
00344         {
00345             phPRINT_RC(rc,NULL,"blob_data.update()");
00346         }
00347         
00348         if (glbl_use_images == 1)
00349         {
00350             /* Sleep a little between frames, otherwise we get an 
00351              * insane frame rate */
00352         
00353             phMSleep(100);
00354         }
00355         else
00356         {
00357             /* Yielding is optional. This gives up the thread's timeslice
00358              * to prevent slow response in other threads. It consumes more
00359              * CPU cycles than sleeping. Use it instead of sleeping if
00360              * this loop is processing anything */
00361         
00362             phYield();
00363         }
00364         
00365 
00366         if (nDisplays > 0)
00367         {
00368             displaysOpen = 0;
00369             for (i = 0; (i < nDisplays) && (displaysOpen == 0); i++)
00370             {
00371                 if (display[i]->isOpen() == 1)
00372                 {
00373                     displaysOpen = 1;
00374                 }
00375             }
00376         }
00377         /* Remove this if block when using this code as an example */
00378         /* Set the loop control value to end the loop when testing */
00379         if (test > 0)
00380         { 
00381             test--;
00382             if (test == 0)
00383                 displaysOpen = 0;
00384         }
00385     }
00386 
00387     /* <> Shutdown the system */
00388     rc = system->shutdown();
00389     phPRINT_RC(rc,NULL,"system->shutdown()");
00390 
00391 error:
00392     rc = blob_data.disconnect();
00393     phPRINT_RC(rc,NULL,"blob_data.disconnect()");
00394     
00395     for (i = 0; (i < nDisplays) && (display != NULL); i++)
00396     {
00397         phDelete(display[i]);
00398     }
00399     
00400     phDeleteArray(display);
00401     phDelete(blob_find);
00402     phDelete(gauss);
00403     phDelete(pipeline);
00404     phDelete(capture);
00405     phDelete(system);
00406 
00407     phDeleteArray(fileImages);
00408 
00409     phFree(glbl_users_imagename);
00410     phFree(capture_path);
00411 
00412     phDelete(arg_parser);
00413 
00414     return phSUCCESS;
00415 }




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