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