home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / Software / ASCENDER / ascendMar8.tar / UMass / BuildingFinder / Staging / detect.c.sav < prev    next >
Text File  |  1995-11-07  |  9KB  |  400 lines

  1. #include "../polygons.h"
  2. #include "../stdDefs.h"
  3.  
  4. void free_meta_graph(MGvertex *graph);
  5. void free_vertex_list(vertexList *vlist);
  6. void free_edge_list(MGedgeList *elist);
  7. void free_MGvertex_list(MGvertexList *MGvlist);
  8. void load_and_draw_poly(char *file, c_handle pane);
  9.  
  10. c_handle    image_trans;    
  11. c_handle    view_trans;        /* Window View Transform */
  12.  
  13. int MANUAL;
  14.  
  15. void find_polygons(double sensitivity, int constrain, int manual, int use2_5d,
  16.       int bb_x1, int bb_y1, int bb_x2, int bb_y2, c_handle picked_pane)
  17. {
  18.  
  19.     c_handle    out_im;            /* Output Image    */
  20.     c_handle    view;
  21.  
  22.     int         startx,starty,stopx,stopy;
  23.     int        init = 0;
  24.     int i,j;
  25.     int len;
  26.     Point        vecx, vecy, vecz;
  27.  
  28.     FILE         *fp,  *fopen();
  29.  
  30.         double         u_v_point[4]; 
  31.         double         x_y_z_point[4]; 
  32.     double_2     *userPoint;
  33.     double_2     *imagePoint1, *imagePoint2;
  34.  
  35.     c_handle     inImage;
  36.  
  37.  
  38.     char         imageName[280];
  39.     char        lineFile[280];
  40.     int        Ncorners;
  41.     int        ytemp, xtemp;
  42.     char        cornerFile[280];
  43.  
  44.  
  45.     /*
  46.      * If the manual flag is set, the parameters have already
  47.      * been adjusted by the user. Otherwise use the default 
  48.      * settings.
  49.      */
  50.     MANUAL = manual;
  51.     in_pane = picked_pane;        /* Global variable version */
  52.     if (!MANUAL)
  53.        setup_defaults(sensitivity);
  54.  
  55.     /*
  56.      * Non-adjustable defaults
  57.      */
  58.     SEARCH_ANGLE = DEFAULT_SEARCH_ANGLE;
  59.     CONSTRAIN = constrain;
  60.  
  61.  
  62.  
  63.     inImage = working_image(get_2d_world(top_image(1,picked_pane)));
  64.     view = top_view(1,picked_pane);
  65.     view_trans = two_d_to_window_transform(view);
  66.     image_trans = image_to_window_transform(view);
  67.  
  68.  
  69.  
  70.     userPoint = (double_2 *) inverse_transform_point(view_trans,
  71.             (double)bb_x1,(double)bb_y1,0);
  72.     imagePoint1 = (double_2 *) inverse_transform_point(image_trans,
  73.             (double)bb_x1,(double)bb_y1,0);
  74.  
  75.     /* 
  76.      * Does the bounding box lay within the image, if not clip it to fit!
  77.      */
  78.      if (imagePoint1->d1 >= image_x_dim(inImage) )
  79.                         imagePoint1->d1 = image_x_dim(inImage);
  80.      if (imagePoint1->d2 >= image_y_dim(inImage) )
  81.                         imagePoint1->d2 = image_y_dim(inImage);
  82.      if (imagePoint1->d1 < 0 )
  83.             imagePoint1->d1 = 0;
  84.     if (imagePoint1->d2 < 0)
  85.             imagePoint1->d2 = 0;
  86.  
  87.  
  88.  
  89.  
  90.     startx = (int) floor(userPoint->d1);
  91.     starty = (int) floor(userPoint->d2);
  92.  
  93.     userPoint = (double_2 *) inverse_transform_point(view_trans,
  94.             (double)bb_x2,(double)bb_y2,0);
  95.     imagePoint2 = (double_2 *) inverse_transform_point(image_trans,
  96.             (double)bb_x2,(double)bb_y2,0);
  97.  
  98.      if (imagePoint2->d1 >= image_x_dim(inImage) )
  99.                         imagePoint2->d1 = image_x_dim(inImage);
  100.      if (imagePoint2->d2 >= image_y_dim(inImage) )
  101.                         imagePoint2->d2 = image_y_dim(inImage);
  102.      if (imagePoint2->d1 < 0 )
  103.             imagePoint2->d1 = 0;
  104.     if (imagePoint2->d2 < 0)
  105.             imagePoint2->d2 = 0;
  106.  
  107.     stopx = (int) floor(userPoint->d1);
  108.     stopy = (int) floor(userPoint->d2);
  109.  
  110.     ystart = starty - (WINDOW+1);
  111.     xstart = startx - (WINDOW+1);
  112.     xstop = stopx + (WINDOW+1);
  113.     ystop = stopy + (WINDOW+1);
  114.     xdim = xstop - xstart;
  115.     ydim = ystop - ystart;
  116.     
  117.     /*** Draw the bounding region ***/
  118.     draw_fat_line(picked_pane,imagePoint1->d1,imagePoint1->d2,
  119.                 imagePoint2->d1,imagePoint1->d2,2.0);
  120.     draw_fat_line(picked_pane,imagePoint2->d1,imagePoint1->d2,
  121.                 imagePoint2->d1,imagePoint2->d2,2.0);
  122.     draw_fat_line(picked_pane,imagePoint2->d1,imagePoint2->d2,
  123.                 imagePoint1->d1,imagePoint2->d2,2.0);
  124.     draw_fat_line(picked_pane,imagePoint1->d1,imagePoint2->d2,
  125.                 imagePoint1->d1,imagePoint1->d2,2.0);
  126.     if (xstart > xstop) {
  127.         xtemp = xstart;
  128.         xstart = xstop;
  129.         xstop = xtemp;
  130.     } 
  131.     if (ystart > ystop) {
  132.         ytemp = ystart;
  133.         ystart = ystop;
  134.         ystop = ytemp;
  135.     }
  136.  
  137.     if (use2_5d)
  138.         strcpy(lineFile, get2_5dlineFile(inImage));
  139.     else
  140.         strcpy(lineFile, get2dlineFile(inImage));
  141.         
  142.     sprintf(cornerFile,"%scorners",getTempDir(inImage));
  143.  
  144.         Ncorners = compute_intersections_region(lineFile,cornerFile,picked_pane,
  145.                         inImage, xstart,ystart, xstop,ystop);
  146.  
  147.     fprintf(stderr,"Found %d corners.\n",Ncorners);
  148.     if (MANUAL) {
  149.         fprintf(stderr,"--------Hit Return To Continue--------\n");
  150.         getchar();
  151.     }
  152.  
  153.     process_region(lineFile, cornerFile, inImage);
  154.  
  155.     /* Clean up Temp files 
  156.     sprintf(cmdstr,"rm %s",cornerFile);
  157.     system(cmdstr);  */
  158. }
  159.  
  160.  
  161. void
  162. process_region(char *lineFile, char *cornerFile, c_handle im)
  163. {
  164. /*
  165.  * NOTE: The global variables (xstart,ystart  xstop,ystop) must be 
  166.  *     initalized!
  167.  *
  168.  */
  169.     cList  *s_list;
  170.         int no_of_colors; 
  171.         MGvertex *meta_graph;
  172.         MGvertexList *ptr;
  173.         MGvertexList *iss_list;
  174.         MGvertexList *iss_max;
  175.  
  176.  
  177.     cList *vertex_list;
  178.     cList *corner_list;
  179.  
  180.  
  181.  
  182.  
  183.     strcpy(IMAGE_NAME,(char *) name(get_2d_world(im)));
  184.     sprintf(IMAGE_NAME_3D,(char *) name(get_3d_world(im)));
  185.     /* 
  186.       * Get the associated projection matrix for this image...
  187.       */
  188.     projectionMat = get_projection_matrix(get_2d_world(im));
  189.  
  190.     /**** Load Low Level Features into Memory ****/
  191.     corner_list = read_corners_from_file(cornerFile);
  192.     line_list = load_line_data(xstart,ystart,xstop,ystop,lineFile,im);
  193.  
  194.  
  195.     buildGraph(corner_list,im, in_pane);
  196.     if (MANUAL) {
  197.         fprintf(stderr,"--------Hit Return To Continue--------\n");
  198.         getchar();
  199.     }
  200.            meta_graph = NULL; 
  201.            no_of_colors=0;
  202.  
  203.            tours(im,corner_list, &no_of_colors, &meta_graph);
  204.     virtualTours(im,corner_list,&no_of_colors,&meta_graph);
  205.  
  206.  
  207.            build_dependency_graph(corner_list,no_of_colors,meta_graph);
  208.  
  209.            iss_list = all_indep_subsets(meta_graph);
  210.     if (iss_list != NULL) {
  211.         
  212.              iss_max = get_max_iss(iss_list);
  213.         if (iss_max == NULL) 
  214.             fprintf(stderr,"NULL PTR!!\n");
  215.            refresh(in_pane);
  216.  
  217.         if (!USE_3D)
  218.             poly2curves(in_pane,iss_max,im);
  219.         else{
  220.             printf("3d!!\n");
  221.                    poly2_3dcurves(in_pane,iss_max);
  222.         }
  223.     
  224.  
  225.     } else 
  226.         fprintf(stderr,"ISS NULL\n");
  227.     free_meta_graph(meta_graph);
  228.     free_MGvertex_list(iss_list);
  229.  
  230.     free_list(corner_list);
  231.     free_line_list(line_list);
  232.     refresh(in_pane);
  233. }
  234.  
  235.  
  236. void free_list(cList *list)
  237. {
  238.  
  239.     cList *p, *q;
  240.     edgeList *e,*f;
  241.  
  242.     for (p = list; p != NULL; p = q) {
  243.  
  244.         for (e=p->eList; e != NULL; e = f) {
  245.             f = e->next;
  246.             free(e);
  247.         }
  248.         q = p->next;
  249.         free(p);
  250.     }
  251. }
  252.  
  253. void free_line_list(line *list)
  254. {
  255.  
  256.     line *p, *q;
  257.  
  258.     for (p = list; p != NULL; p=q) {
  259.         q = p->next;
  260.         free(p);
  261.     }
  262. }
  263.  
  264. void free_meta_graph(MGvertex *graph)
  265. {
  266.  
  267.     MGvertex *ptr;
  268.     MGvertex *f_ptr;
  269.  
  270.     for (ptr = graph; ptr != NULL; ptr = f_ptr) {
  271.         f_ptr = ptr->next;
  272.         free_vertex_list(ptr->vList);
  273.         free_edge_list(ptr->eList);
  274.         free(ptr);
  275.     }
  276. }
  277.  
  278. void free_vertex_list(vertexList *vlist)
  279. {
  280.  
  281.     vertexList *ptr;
  282.     vertexList *f_ptr;
  283.  
  284.     for (ptr = vlist; ptr != NULL; ptr = f_ptr) {
  285.         f_ptr = ptr->next;
  286.         free(ptr);
  287.     }
  288. }
  289.  
  290. void free_MGvertex_list(MGvertexList *vlist)
  291. {
  292.  
  293.     MGvertexList *ptr;
  294.     MGvertexList *f_ptr;
  295.  
  296.     for (ptr = vlist; ptr != NULL; ptr = f_ptr) {
  297.         f_ptr = ptr->next;
  298.         free(ptr);
  299.     }
  300. }
  301.  
  302. void free_edge_list(MGedgeList *elist)
  303. {
  304.  
  305.     MGedgeList *ptr;
  306.     MGedgeList *f_ptr;
  307.  
  308.     for (ptr = elist; ptr != NULL; ptr = f_ptr) {
  309.         f_ptr = ptr->next;
  310.         free(ptr);
  311.     }
  312. }
  313. int sleep(int time)
  314. {
  315.  
  316.         int j= 50;
  317.         int i;
  318.  
  319.         printf("Sleep...");
  320.         for (i=0; i < 10000*time; i++)
  321.                 j = j + 1;
  322.         printf("Done.\n");
  323. }
  324.  
  325. void load_and_draw_poly(char *file, c_handle pane)
  326. {
  327.  
  328.         FILE *fp, *fopen();
  329.         int x1,y1,x2,y2;
  330.  
  331.         if ( (fp = fopen(file,"r")) == NULL)  {
  332.                 fprintf(stderr,"Cannot open polygon data\n");
  333.                 return;
  334.         }
  335.  
  336.         while (fscanf(fp,"%d %d %d %d",&x1,&y1,&x2,&y2) != EOF)
  337.                 draw_fat_line(pane,(double)x1,(double)y1,(double)x2,(double)y2,
  338.                                         (double)LINE_WIDTH);
  339.  
  340.         fclose(fp);
  341.  
  342. }
  343.  
  344.  
  345. void
  346. setup_defaults(double Sensitivity)
  347. /*
  348.  * Setup the appropriate global defaults for the building
  349.  * detector.
  350.  *
  351.  * This should depend on the sensitivity rating, 
  352.  * and information from the image (ie- camera pose, illumination)
  353.  * 
  354.  * Currently, this is set only by the sensitivity.
  355.  *
  356.  */
  357. {
  358.  
  359.     MIN_LINE_SUPPORT = DEFAULT_LINE_SUPPORT_LOW -
  360.                 (DEFAULT_LINE_SUPPORT_LOW -
  361.                  DEFAULT_LINE_SUPPORT_HIGH)*Sensitivity;
  362.  
  363.     TOKEN_WINDOW =       TOKEN_WINDOW_LOW -
  364.                 (TOKEN_WINDOW_LOW - TOKEN_WINDOW_HIGH)*
  365.                     Sensitivity;
  366.  
  367.     TOKEN_SUPPORT = TOKEN_SUPPORT_LOW -
  368.                 (TOKEN_SUPPORT_LOW - TOKEN_SUPPORT_HIGH)*
  369.                     Sensitivity;
  370.  
  371.     ANGLE_THRESH    = DEFAULT_ANGLE_THRESH_LOW - 
  372.                 (DEFAULT_ANGLE_THRESH_LOW -
  373.                  DEFAULT_ANGLE_THRESH_HIGH)*Sensitivity;
  374.     LINE_SWEEP_COLUMN = LINE_SWEEP_COLUMN_LOW - 
  375.                 (LINE_SWEEP_COLUMN_LOW -
  376.                   LINE_SWEEP_COLUMN_HIGH)*Sensitivity;
  377.  
  378.     LINE_ENDPOINT_WINDOW = LINE_ENDPOINT_WINDOW_LOW - 
  379.                 (LINE_ENDPOINT_WINDOW_LOW -
  380.                   LINE_ENDPOINT_WINDOW_HIGH)*Sensitivity;
  381.  
  382. }
  383.  
  384. void
  385. set_building_parameters(double angle_error, double line_window,
  386.         double search_angle, double column,  double edge_mag,
  387.         double edge_var, double line_support, double token_support,
  388.             double token_window)
  389. {
  390.  
  391.  
  392.        MIN_LINE_SUPPORT   = line_support;
  393.        TOKEN_WINDOW       = token_window;
  394.        TOKEN_SUPPORT      = token_support;
  395.        ANGLE_THRESH       = angle_error;
  396.        LINE_SWEEP_COLUMN  = column;
  397.        LINE_ENDPOINT_WINDOW = line_window;
  398.  
  399. }
  400.