00001 /* ------------------------------------------------------------------------- * 00002 * Copyright (C) Philip D.S. Thoren 00003 * Graph based segmentation application. 00004 * 05/27/2007 00005 * 00006 * This program does graph based segmentation on files or a live feed. 00007 * 00008 * If you change this, add your copyrights and comments for what you changed. 00009 * 00010 * ------------------------------------------------------------------------- */ 00011 #include <phission.h> 00012 00013 /* ------------------------------------------------------------------------- */ 00014 /* I need to make this code here part of Phission, this just makes the code 00015 * easier to work with if the portable stuff is renamed to a common name */ 00016 #if defined(PH_HAVE_X11) 00017 #define phDisplay X11Display 00018 #elif defined(PH_HAVE_GDI) 00019 #define phDisplay GDIDisplay 00020 #endif 00021 00022 #if defined(PH_HAVE_V4L) 00023 #define phCapture V4LCapture 00024 #define default_path "/dev/video0" 00025 #else 00026 #define phCapture VFWSource 00027 #define default_path "0" 00028 #endif 00029 00030 /* ------------------------------------------------------------------------ */ 00031 /* Global variables */ 00032 int glbl_disable_displays = 0; 00033 int glbl_print_blobs = 0; 00034 int glbl_save_images = 0; 00035 00036 /* image reading variables */ 00037 int glbl_use_images = 0; 00038 uint32_t glbl_input_total = 2; 00039 char *glbl_prefix =NULL; 00040 char *glbl_extension =NULL; 00041 00042 /* Capture variables */ 00043 char *glbl_path =NULL; 00044 int glbl_channel = 1; 00045 int32_t glbl_width = 320; 00046 int32_t glbl_height = 240; 00047 00048 /* ------------------------------------------------------------------------ */ 00049 void usage() 00050 { 00051 printf("\n\nUsage:\n"); 00052 printf("\t--help Display usage\n"); 00053 printf("\t--nodisplay Disable the allocation, opening or any use of a display.\n"); 00054 printf("\t--save Save pipeline output images (0 by default)\n"); 00055 printf("\t--nimages <total> Filename format: <prefix>_%%04d.ppm (2 by default).\n"); 00056 printf("\t--prefix <prefix> Use images. (<prefix> = image by default)\n"); 00057 printf("\t--ext <extension> The input image file extension (default: ppm\n"); 00058 printf("\t--printdata Print the blob data\n"); 00059 printf("\t-w <width> Width of the input data. (default: 320).\n"); 00060 printf("\t-h <height> Height of the input data. (default: 240).\n"); 00061 printf("\n\n"); 00062 exit(1); 00063 } 00064 00065 /* ------------------------------------------------------------------------ */ 00066 int parse_command_line( int argc, char *argv[] ) 00067 { 00068 phFUNCTION("parse_command_line") 00069 00070 phArgTable *arg_parser = new phArgTable(); 00071 00072 /* <> Setup all the command line options to the parser */ 00073 rc = arg_parser->add("-w",&glbl_width, phARG_INT32); 00074 phCHECK_RC(rc,NULL,"arg_parser->add"); 00075 00076 rc = arg_parser->add("-h",&glbl_height, phARG_INT32); 00077 phCHECK_RC(rc,NULL,"arg_parser->add"); 00078 00079 rc = arg_parser->add("--prefix",&glbl_prefix, phARG_CHAR); 00080 phCHECK_RC(rc,NULL,"arg_parser->add"); 00081 00082 rc = arg_parser->add("--ext",&glbl_extension, phARG_CHAR); 00083 phCHECK_RC(rc,NULL,"arg_parser->add"); 00084 00085 rc = arg_parser->add("--nimages",&glbl_input_total, phARG_UINT); 00086 phCHECK_RC(rc,NULL,"arg_parser->add"); 00087 00088 rc = arg_parser->add("--save",&glbl_save_images, phARG_BOOL); 00089 phCHECK_RC(rc,NULL,"arg_parser->add"); 00090 00091 rc = arg_parser->add("--printdata",&glbl_print_blobs, phARG_BOOL); 00092 phCHECK_RC(rc,NULL,"arg_parser->add"); 00093 00094 rc = arg_parser->add("--nodisplay",&glbl_disable_displays, phARG_BOOL); 00095 phCHECK_RC(rc,NULL,"arg_parser->add"); 00096 00097 rc = arg_parser->add("--help",(void *)&usage, phARG_FUNC); 00098 phCHECK_RC(rc,NULL,"arg_parser->add"); 00099 00100 rc = arg_parser->add("--path",&glbl_path,phARG_CHAR); 00101 phCHECK_RC(rc,NULL,"arg_parser->add"); 00102 00103 rc = arg_parser->add("--channel",&glbl_channel,phARG_INT); 00104 phCHECK_RC(rc,NULL,"arg_parser->add"); 00105 00106 /* <> Parse */ 00107 rc = arg_parser->parse(argc,argv); 00108 phCHECK_RC(rc,NULL,"arg_parser->parse"); 00109 00110 /* <> Do error checking and variable default setup */ 00111 00112 phDelete(arg_parser); 00113 00114 return phSUCCESS; 00115 error: 00116 phDelete(arg_parser); 00117 00118 return phFAIL; 00119 } 00120 00121 /* ------------------------------------------------------------------------ */ 00122 void next_image( char *prefix, 00123 char *ext, 00124 int total, 00125 uint32_t *index, 00126 phImage *image ) 00127 { 00128 phFUNCTION("next_image") 00129 char filename[255]; 00130 00131 snprintf(filename,255,"%s%010u.%s",prefix,*index,ext); 00132 00133 if (strcmp(ext,"ppm") != 0) 00134 { 00135 image->disableNotify(); 00136 00137 rc = image->load(filename); 00138 phPRINT_RC(rc,NULL,"image->load(%s)",filename); 00139 00140 image->enableNotify(); 00141 00142 image->convert(phImageRGB24); 00143 } 00144 else 00145 { 00146 rc = image->load(filename); 00147 phPRINT_RC(rc,NULL,"image->load(%s)",filename); 00148 } 00149 *index = (*index + 1) % total; 00150 } 00151 00152 /* ------------------------------------------------------------------------- */ 00153 int main (int argc, char *argv[] ) 00154 { 00155 phFUNCTION("main") 00156 00157 int i = 0; 00158 int done = 0; 00159 00160 /* Declare ------------------------------------------------------------- */ 00161 phSystem *system = NULL; 00162 phDisplayInterface *display = NULL; 00163 phDisplayInterface *pipeline_display = NULL; 00164 phImageCapture *capture = NULL; 00165 phPipeline *pipeline = NULL; 00166 00167 phLiveObject *input_source = NULL; 00168 phImage *input_image = NULL; 00169 uint32_t input_index = 0; 00170 char *input_ext = "ppm"; 00171 00172 char output_filename[255]; 00173 uint32_t output_index = 0; 00174 phImage *output_image = NULL; 00175 00176 cv_houghLines2_Filter *hough = NULL; 00177 gaussian3x3_Filter *gauss = NULL; 00178 convert_Filter *gray = NULL; 00179 threshold_Filter *thresh = NULL; 00180 meanNxN_Filter *mean = NULL; 00181 resize_Filter *resize = NULL; 00182 subtract_Filter *sub = NULL; 00183 cv_canny_Filter *canny = NULL; 00184 00185 const int canny_low = 80; 00186 const int canny_high = 200; 00187 00188 phHoughData *hough_data = NULL; 00189 00190 phPROGRESS("Program started.\n"); 00191 00192 /* Parse the command line ---------------------------------------------- */ 00193 rc = parse_command_line(argc, argv ); 00194 phCHECK_RC(rc,NULL,"parse_command_line"); 00195 00196 if (glbl_extension != NULL) 00197 { 00198 input_ext = glbl_extension; 00199 } 00200 00201 /* Allocate ------------------------------------------------------------ */ 00202 system = new phSystem(); 00203 pipeline = new phPipeline(); 00204 gauss = new gaussian3x3_Filter(); 00205 gray = new convert_Filter(phImageGRAY8); 00206 thresh = new threshold_Filter(0,140); 00207 mean = new meanNxN_Filter(5); 00208 resize = new resize_Filter(320,240); 00209 sub = new subtract_Filter(2,1,2); 00210 canny = new cv_canny_Filter(canny_low,canny_high); 00211 hough = new cv_houghLines2_Filter(); 00212 hough_data = new phHoughData(); 00213 00214 /* live data */ 00215 if (glbl_prefix == NULL) 00216 { 00217 capture = new phCapture(); 00218 input_source = capture->getOutput(); 00219 } 00220 /* Data being loaded from files */ 00221 else 00222 { 00223 input_image = new phImage(); 00224 input_source = input_image; 00225 } 00226 00227 output_image = new phImage(); 00228 00229 /* Don't allocate displays if we're not using them */ 00230 if (!glbl_disable_displays) 00231 { 00232 display = new phDisplay( glbl_width, glbl_height,"Capture" ); 00233 pipeline_display = new phDisplay( glbl_width, glbl_height,"Pipeline" ); 00234 } 00235 00236 /* Connect ------------------------------------------------------------- */ 00237 pipeline->setInput ( input_source ); 00238 00239 /* Don't connect displays if we're not using them */ 00240 if (!glbl_disable_displays) 00241 { 00242 display->setInput ( input_source ); 00243 display->setInput(resize->getLiveSourceOutput()); 00244 pipeline_display->setInput ( pipeline->getOutput() ); 00245 } 00246 00247 output_image->connect(pipeline->getOutput()); 00248 00249 hough_data->connect(hough->getOutput()); 00250 00251 /* Tell the system about it all --------------------------------------- */ 00252 if (capture != NULL) 00253 { 00254 system->add(capture); 00255 } 00256 00257 /* Don't add displays if we're not using them */ 00258 if (!glbl_disable_displays) 00259 { 00260 system->add(display); 00261 system->add(pipeline_display); 00262 } 00263 00264 system->add(pipeline); 00265 00266 /* Calibrate / Setup -------------------------------------------------- */ 00267 if (capture != NULL) 00268 { 00269 capture->set(glbl_width,glbl_height,glbl_path); 00270 if (glbl_path == NULL) 00271 { 00272 capture->setPath((char *)default_path); 00273 } 00274 capture->setChannel(glbl_channel); 00275 } 00276 00277 /* Don't adjust/setup displays if we're not using them */ 00278 if (!glbl_disable_displays) 00279 { 00280 display->move(0,0); 00281 pipeline_display->move(glbl_width + 50,0); 00282 } 00283 00284 /* Setup the pipeline ------------------------------------------------- */ 00285 pipeline->add(resize); 00286 pipeline->add(gauss); 00287 pipeline->add(gray); 00288 pipeline->add(thresh); 00289 pipeline->add(canny); 00290 00291 pipeline->add(hough); 00292 00293 pipeline->enableTiming(); 00294 00295 /* -------------------------------------------------------------------- */ 00296 hough->setMethod(phHoughProbalistic); 00297 hough->setDrawLines (1); 00298 hough->setRho (1); 00299 hough->setTheta (0.0174532925); 00300 hough->setThreshold (50); 00301 hough->setParam1 (30); 00302 hough->setParam2 (10); 00303 00304 /* Start -------------------------------------------------------------- */ 00305 system->startup(); 00306 00307 if (!glbl_disable_displays) 00308 done = 1; 00309 while ((system->displaysOpen() > 0) || (!done)) 00310 { 00311 /* ----------------------------------------------------------------- */ 00312 /* Load an image to segment */ 00313 if (glbl_prefix != NULL) 00314 { 00315 next_image( glbl_prefix, 00316 input_ext, 00317 glbl_input_total, 00318 &input_index, 00319 input_image ); 00320 phMSleep(1000); 00321 } 00322 00323 /* ----------------------------------------------------------------- */ 00324 if (glbl_save_images) 00325 { 00326 snprintf(output_filename,255, 00327 "images/input/cv_houghLines2_%04u.ppm", 00328 output_index); 00329 input_image->save(output_filename); 00330 } 00331 00332 /* ----------------------------------------------------------------- */ 00333 /* Throttle the loop to wait for the pipeline to complete */ 00334 if ((glbl_prefix != NULL) || 0) 00335 { 00336 output_image->update(); 00337 } 00338 /* if we're saving images, get the frames to save */ 00339 if (glbl_save_images) 00340 { 00341 snprintf(output_filename,255, 00342 "images/output/cv_houghLines2_%04u.ppm", 00343 output_index); 00344 00345 output_image->save(output_filename); 00346 00347 output_index++; 00348 } 00349 00350 rc = hough_data->update(); 00351 if ((rc == phLiveObjectUPDATED) && 00352 (hough_data->getLineCount())) 00353 { 00354 hough_data->print_data(); 00355 } 00356 #if 1 00357 phPRINT("-> %d (%d of %d)\n", pipeline->runtime().getMilliseconds(), 00358 input_index, glbl_input_total ); 00359 #endif 00360 /* Shutdown if we're saving */ 00361 /* When input_index == 0, it will be the next run through the list */ 00362 if (glbl_save_images && (input_index == 0)) 00363 { 00364 done = 1; 00365 system->stopDisplays(); 00366 continue; 00367 } 00368 } 00369 00370 /* Stop --------------------------------------------------------------- */ 00371 system->shutdown(); 00372 00373 #if 0 00374 phPRINT("Average %d\n", 00375 pipeline->averageRuntime().getMilliseconds(), 00376 input_index, 00377 glbl_input_total ); 00378 #endif 00379 00380 error: 00381 /* Cleanup ------------------------------------------------------------ */ 00382 phDelete(hough); 00383 phDelete(canny); 00384 phDelete(sub); 00385 00386 phDelete(output_image); 00387 phDelete(input_image); 00388 phDelete(pipeline); 00389 phDelete(capture); 00390 phDelete(display); 00391 phDelete(pipeline_display); 00392 phDelete(system); 00393 00394 phFree(glbl_prefix); 00395 phFree(glbl_path); 00396 phFree(glbl_extension); 00397 00398 phPROGRESS("Program finished.\n"); 00399 00400 return phSUCCESS; 00401 } 00402 00403
Copyright (C) 2002 - 2007 |
Philip D.S. Thoren ( pthoren@users.sourceforge.net ) University Of Massachusetts at Lowell Robotics Lab |