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

HSVSegmentExample.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 <HSVSegmentExample.h>
00013 #include <hsvsegment_Filter.h>
00014 #include <block_Filter.h>
00015 #include <ave_Filter.h>
00016 #include <phission.h>
00017 
00018 /* ------------------------------------------------------------------------ */
00019 int glbl_disable_displays = 0;
00020 int glbl_do_timing = 0;
00021 
00022 /* ------------------------------------------------------------------------ */
00023 void usage()
00024 {
00025     printf("\n\nUsage:\n");
00026     printf("\t--help                    Display usage\n");
00027     printf("\t-f | --file <filename>    Use a video file as input\t\t\n");
00028     printf("\t--nodisplay               Disable the allocation, opening or any use of a display.\n");
00029     printf("\t--test <value>            Loop <value> times and then close test\n");
00030     printf("\t--timing                  Print pipeline runtime\n");
00031     printf("\n\n");
00032     
00033     exit(1);
00034 }
00035 
00036 /* ------------------------------------------------------------------------ */
00037 /* ------------------------------------------------------------------------ */
00038 int main(int argc, char *argv[] )
00039 {
00040     phFUNCTION("main")
00041 
00042     unsigned int    i               = 0;
00043     unsigned int    z               = 0;
00044 
00045     unsigned int        nDisplays   = 2;
00046     int                 displaysOpen= 1;
00047     char                title[255];
00048     phDisplayInterface  **display   = NULL;
00049     
00050     char            *capture_path   = NULL;
00051     int             channel         = 0;
00052     
00053 #ifdef WIN32
00054     VFWSource           *vfwcapture = new VFWSource();
00055 #else
00056     V4LCapture          *v4lcapture = new V4LCapture();
00057 #endif
00058     phAvcodecSource     *avcapture  = new phAvcodecSource();
00059     phImageCapture      *capture    = NULL;
00060     
00061     char                *avfilename = NULL;
00062     
00063     phImage             saveimg;
00064     char                filename[255];
00065     
00066     phPipeline          *pipeline   = new phPipeline();
00067     convert_Filter      *convert    = new convert_Filter(phImageHSV24);
00068     meanNxN_Filter      *mean       = new meanNxN_Filter(5);
00069     medianNxN_Filter    *median     = new medianNxN_Filter(5);
00070     gaussianBlur_Filter *gauss      = new gaussianBlur_Filter();
00071     hsvsegment_Filter   *hsvsegment = new hsvsegment_Filter(  1,   1,   1,
00072                                                               0, 70,  55,
00073                                                             255, 255, 255);
00074     ave_Filter          *ave        = new ave_Filter(6);
00075     add_Filter          *add        = new add_Filter(2);
00076     uint32_t            bthresh[4]  = {0,0,0,0};
00077     block_Filter        *block      = new block_Filter();
00078     
00079     inverse_Filter      *inverse    = new inverse_Filter();
00080     original_Filter     *original   = new original_Filter();
00081 
00082 #if PH_HAVE_OPENCV
00083     canny_Filter    *canny      = new canny_Filter(40,95);
00084     cv_canny_Filter *cvcanny      = new cv_canny_Filter(40,85);
00085 #else
00086     canny_Filter    *canny      = new canny_Filter(50,105);
00087 #endif
00088     /*
00089     threshold_Filter    *threshold      = new threshold_Filter(0,100);
00090     subtract_Filter     *sub            = new subtract_Filter(3,3);
00091     add_Filter          *add2            = new add_Filter(2,2);
00092     */
00093     
00094     /* Utility class to ease the starting and stopping of displays, captures and
00095      * pipelines */
00096     phSystem        system;
00097    
00098     /* Remove the code below when using this code as an example.
00099      * 
00100      * This just checks whether "--test" has been specified with
00101      * a time value argument. It's for testing all the examples
00102      * without the need for human intervention. */
00103     int             test = 0;
00104 
00105     phArgTable      *arg_parser = new phArgTable();
00106 
00107     /* Setup and parse all the arguments */
00108     rc = arg_parser->add("--test",&test,phARG_INT);
00109     phCHECK_RC(rc,NULL,"arg_parser->add");
00110     rc = arg_parser->add("--file",&avfilename,phARG_CHAR);
00111     phCHECK_RC(rc,NULL,"arg_parser->add");
00112     rc = arg_parser->add("-f",&avfilename,phARG_CHAR);
00113     phCHECK_RC(rc,NULL,"arg_parser->add");
00114     rc = arg_parser->add("--nodisplay",&glbl_disable_displays,phARG_BOOL);
00115     phCHECK_RC(rc,NULL,"arg_parser->add");
00116     rc = arg_parser->add("--help",(void *)&usage,phARG_FUNC);
00117     phCHECK_RC(rc,NULL,"arg_parser->add");
00118     
00119     rc = arg_parser->add("--path",&capture_path,phARG_CHAR);
00120     phCHECK_RC(rc,NULL,"arg_parser->add");
00121 
00122     rc = arg_parser->add("--channel",&channel,phARG_INT);
00123     phCHECK_RC(rc,NULL,"arg_parser->add");
00124     
00125     rc = arg_parser->add("--timing",&glbl_do_timing,phARG_BOOL);
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     if (glbl_disable_displays) nDisplays = 0;
00132     if (test) glbl_do_timing = 1;
00133     
00134 
00135     /* 1.) Setup the capture device parameters */
00136     if (avfilename == NULL) /* using input file */
00137     {
00138 #ifdef WIN32
00139         capture = vfwcapture;
00140 #else
00141         capture = v4lcapture;
00142         capture->set(320,240,capture_path);
00143 #endif
00144     }
00145     else
00146     {
00147         capture = avcapture;
00148         capture->setPath(avfilename);
00149         capture->setFormat(phImageRGBA32);
00150     }
00151     capture->setChannel(channel);
00152     capture->setColor(43000);
00153     capture->setBrightness(30000);
00154     capture->setContrast(30000);
00155     capture->setColour(57000);
00156     capture->setHue(34500);
00157     capture->setContrast(34000);
00158     capture->setBrightness(34000);
00159     
00160     rc = system.addCapture(capture);
00161     phPRINT_RC(rc,NULL,"system.addCapture(capture)");
00162     
00163     display = new phDisplayInterface *[nDisplays];
00164     phCHECK_NULLPTR(display,"new","new phDisplayInterface *[nDisplays];");
00165 
00166     for (i = 0; i < nDisplays; i++ )
00167     {
00168         sprintf(title,"X11PipelineTest[%u]",i);
00169         
00170         display[i] = new X11Display(320,240,title);
00171 
00172         rc = system.addDisplay(display[i]);
00173         phPRINT_RC(rc,NULL,"system.addDisplay(display[%u])",i);
00174 
00175         display[i]->move(330*i,100);
00176     }
00177     
00178     rc = system.addPipeline(pipeline);
00179     phPRINT_RC(rc,NULL,"system.addPipeline(pipeline)");
00180 
00181     
00182     /* <> Attach the displays to the live sources */
00183     /* Capture -> (b)phPipeline -> (a)Processed Output Display */
00184     
00185     /* <> Attach the Pipeline output image to the display so the
00186      * display updates when a processing has been completed
00187      * for each loop */
00188     if (nDisplays > 0)
00189         display[0]->setLiveSourceInput(pipeline->getLiveSourceOutput());
00190     if (nDisplays > 1)
00191         display[1]->setLiveSourceInput(capture->getLiveSourceOutput());
00192     
00193     /* <> Attach the capture image to the live source input of the
00194      * Pipeline so processing is done automatically once the 
00195      * pipeline is started. */
00196     pipeline->setLiveSourceInput(capture->getLiveSourceOutput());
00197     
00198     /* <> Add the filter(s) into the pipeline in the order they will 
00199      * be used */
00200     pipeline->add(ave);
00201 #if 0    
00202     pipeline->add(gauss);
00203     pipeline->add(gauss);
00204     pipeline->add(canny);
00205     pipeline->add(inverse);
00206     pipeline->add(add);
00207 #endif 
00208     pipeline->add(original);
00209 
00210     pipeline->add(gauss);
00211     pipeline->add(gauss);
00212     pipeline->add(cvcanny);
00213     pipeline->add(add);
00214     
00215     pipeline->add(original);
00216     pipeline->add(gauss);
00217     pipeline->add(gauss);
00218     //pipeline->add(gauss);
00219     //pipeline->add(gauss);
00220     //pipeline->add(gauss);
00221     //pipeline->add(ave);
00222     //pipeline->add(median);
00223     //pipeline->add(mean);
00224     //pipeline->add(add);
00225     pipeline->add(add);
00226     pipeline->add(convert);
00227     //pipeline->add(gauss);
00228     pipeline->add(hsvsegment);
00229     //pipeline->add(block);
00230 
00231     /*
00232     pipeline->add(sub);
00233     pipeline->add(add2);
00234     pipeline->add(threshold);
00235     */
00236     
00237     /* Since timing in the pipeline is disabled by default, enable it */
00238     if (glbl_do_timing)
00239     {
00240         rc = pipeline->enableTiming();
00241         phPRINT_RC(rc,NULL,"pipeline->enableTiming()");
00242     }
00243 
00244     //saveimg.connect(pipeline->getLiveSourceOutput());
00245 
00246     /* <> Startup the system */
00247     rc = system.startup();
00248     phPRINT_RC(rc,NULL,"system.startup()");
00249     
00250 #if 0
00251     for (z = 0; z < 5; z ++) saveimg.update();
00252 #endif
00253     z = 0;
00254     /* <> Keep going until all the windows are closed */
00255     displaysOpen = 1;
00256     while (displaysOpen)
00257     {
00258         if (glbl_do_timing)
00259         {
00260             /* Reset the average calculations and sleep a second */
00261             rc = pipeline->resetAverage();
00262             phPRINT_RC(rc,NULL,"pipeline->resetAverage()");
00263             
00264             phMSleep(1000);
00265             
00266             /* Report the average using the phTimeStamp report facility
00267              * to allow for portable/robust reporting */
00268             pipeline->averageRuntime().report("X11PipelineTest - Filtering Average");
00269             phPRINT_RC(rc,NULL,"pipeline->averageRuntime()");
00270         }
00271         else
00272         {
00273             /* Sleep a while, don't loop tightly */
00274             phMSleep(100);
00275         }
00276 
00277 #if 0
00278         saveimg.update();
00279         sprintf(filename,"hsv_z:%05d.jpg",z);
00280         saveimg.save(filename);
00281 #endif
00282         
00283 #if 0
00284         z+=10;
00285         //if (z > 255) z = 0;
00286 #else
00287         z++;
00288 #endif
00289         
00290 #if 0
00291         //phPRINT("z:%05d\n",z);
00292         hsvsegment->set(  1,   1,   1,
00293                           0,   80,  100,
00294                          255, 255, 255);
00295 #endif        
00296         /* Yielding is optional. This gives up the thread's timeslice
00297          * to prevent slow response in other threads. It consumes more
00298          * CPU cycles than sleeping. Use it instead of sleeping if
00299          * this loop is processing anything */
00300         
00301         /* phYield(); */
00302    
00303         if (nDisplays > 0)
00304         {
00305             displaysOpen = 0;
00306             for (i = 0; (i < nDisplays) && (displaysOpen == 0); i++)
00307             {
00308                 if (display[i]->isOpen() == 1)
00309                 {
00310                     displaysOpen = 1;
00311                 }
00312             }
00313         }
00314         
00315         /* Remove this if block when using this code as an example */
00316         /* Set the loop control value to end the loop when testing */
00317         if (test > 0)
00318         { 
00319             test--;
00320             if (test == 0)
00321                 displaysOpen = 0;
00322         }
00323     }
00324 
00325 error:
00326     /* <> Shutdown the system */
00327     rc = system.shutdown();
00328     phPRINT_RC(rc,NULL,"system.shutdown()");
00329 
00330     for (i = 0; (i < nDisplays) && (display != NULL); i++)
00331     {
00332         phDelete(display[i]);
00333     }
00334   
00335     phDelete(display);
00336 
00337     phDelete(v4lcapture);
00338     phDelete(avcapture);
00339     
00340     phDelete(convert);
00341     phDelete(hsvsegment);
00342     phDelete(block);
00343   
00344     phDelete(pipeline);
00345 
00346     phFree(capture_path);
00347     phDelete(arg_parser);
00348 
00349     return phSUCCESS;
00350 }




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