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 / detect.c < prev    next >
C/C++ Source or Header  |  1996-05-30  |  11KB  |  494 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. void transform_bbox(c_handle transform, double x1, double y1, double x2, 
  10.   double y2, double *newx1, double *newy1, double *newx2, double *newy2);
  11.  
  12. c_handle    image_trans;    
  13. c_handle    view_trans;        /* Window View Transform */
  14. c_handle    world_trans;    
  15.  
  16. int MANUAL;
  17. int K;
  18.  
  19. void find_polygons(double sensitivity, int constrain, int manual, int use2_5d,
  20.       int bb_x1, int bb_y1, int bb_x2, int bb_y2, int k_cycles, 
  21.     c_handle picked_pane)
  22. {
  23.  
  24.     c_handle    out_im;            /* Output Image    */
  25.     c_handle    view;
  26.  
  27.     int        init = 0;
  28.     int i,j;
  29.     int len;
  30.     Point        vecx, vecy, vecz;
  31.  
  32.     FILE         *fp,  *fopen();
  33.  
  34.         double         u_v_point[4]; 
  35.         double         x_y_z_point[4]; 
  36.  
  37.     double          u1, v1, u2, v2;
  38.         double          wx1, wy1, wx2, wy2;    
  39.     double_2        *imagePoint1, *imagePoint2;  
  40.  
  41.     c_handle     inImage;
  42.  
  43.  
  44.     char         imageName[280];
  45.     char        lineFile[280];
  46.     int        Ncorners;
  47.     int        ytemp, xtemp;
  48.     char        cornerFile[280];
  49.  
  50.  
  51.     /*
  52.      * If the manual flag is set, the parameters have already
  53.      * been adjusted by the user. Otherwise use the default 
  54.      * settings.
  55.      */
  56.     fprintf(stderr,"In Building Detect.\n"); 
  57.     MANUAL = manual;
  58.       K = k_cycles;
  59.     if (K == 0)
  60.         K_CYCLES = FALSE;
  61.     else
  62.         K_CYCLES = TRUE;
  63.     in_pane = picked_pane;        /* Global variable version */
  64.     if (!MANUAL)
  65.        setup_defaults(sensitivity);
  66.  
  67.     /*
  68.      * Non-adjustable defaults
  69.      */
  70.     SEARCH_ANGLE = DEFAULT_SEARCH_ANGLE;
  71.     CONSTRAIN = constrain;
  72.  
  73.  
  74.     inImage = working_image(get_2d_world(top_image(1,picked_pane)));
  75.     view = top_view(1,picked_pane);
  76.     view_trans = two_d_to_window_transform(view);
  77.     image_trans = image_to_window_transform(view);
  78.         world_trans = image_to_2d_transform(view);
  79.  
  80.     transform_bbox((c_handle)inverse_transform(image_trans),
  81.             (double)bb_x1,(double)bb_y1,
  82.             (double)bb_x2,(double)bb_y2,
  83.                 &u1, &v1, &u2, &v2);
  84.  
  85.     transform_bbox(world_trans,u1,v1,u2,v2,
  86.                 &wx1, &wy1, &wx2, &wy2);
  87.  
  88.     xstart = (int) floor(wx1);
  89.         ystart = (int) floor(wy1);
  90.     xstop = (int) ceil(wx2);
  91.         ystop = (int) ceil(wy2);
  92.     xdim = xstop - xstart;
  93.     ydim = ystop - ystart;
  94.  
  95. /*    printf("bounds %d %d %d %d\n", xstart, ystart, xstop, ystop); */
  96.  
  97.     /*** Draw the bounding region ***/
  98.  
  99.     imagePoint1 = (double_2 *) inverse_transform_point(world_trans,
  100.                 (double)xstart,(double)ystart,0);
  101.     imagePoint2 = (double_2 *) inverse_transform_point(world_trans,
  102.                 (double)xstart,(double)ystop,0);
  103.     draw_fat_line(picked_pane,imagePoint1->d1,imagePoint1->d2,
  104.                 imagePoint2->d1,imagePoint2->d2,2.0);
  105.     imagePoint1 = (double_2 *) inverse_transform_point(world_trans,
  106.                 (double)xstart,(double)ystop,0);
  107.     imagePoint2 = (double_2 *) inverse_transform_point(world_trans,
  108.                 (double)xstop,(double)ystop,0);
  109.     draw_fat_line(picked_pane,imagePoint1->d1,imagePoint1->d2,
  110.                 imagePoint2->d1,imagePoint2->d2,2.0);
  111.     imagePoint1 = (double_2 *) inverse_transform_point(world_trans,
  112.                 (double)xstop,(double)ystop,0);
  113.     imagePoint2 = (double_2 *) inverse_transform_point(world_trans,
  114.                 (double)xstop,(double)ystart,0);
  115.     draw_fat_line(picked_pane,imagePoint1->d1,imagePoint1->d2,
  116.                 imagePoint2->d1,imagePoint2->d2,2.0);
  117.     imagePoint1 = (double_2 *) inverse_transform_point(world_trans,
  118.                 (double)xstop,(double)ystart,0);
  119.     imagePoint2 = (double_2 *) inverse_transform_point(world_trans,
  120.                 (double)xstart,(double)ystart,0);
  121.     draw_fat_line(picked_pane,imagePoint1->d1,imagePoint1->d2,
  122.                 imagePoint2->d1,imagePoint2->d2,2.0);
  123.  
  124.  
  125.     if (use2_5d)
  126.         strcpy(lineFile, get2_5dlineFile(inImage));
  127.     else
  128.         strcpy(lineFile, get2dlineFile(inImage));
  129.         
  130.     sprintf(cornerFile,"%scorners",getTempDir(inImage));
  131.  
  132.         Ncorners = compute_intersections_region(lineFile,cornerFile,picked_pane,
  133.                         inImage, xstart,ystart, xstop,ystop);
  134.  
  135.     fprintf(stderr,"Found %d corners.\n",Ncorners);
  136.     if (MANUAL) {
  137.         fprintf(stderr,"--------Hit Return To Continue--------\n");
  138.         getchar();
  139.     }
  140.  
  141.     process_region(lineFile, cornerFile, inImage);
  142.  
  143.     /* Clean up Temp files 
  144.     sprintf(cmdstr,"rm %s",cornerFile);
  145.     system(cmdstr);  */
  146. }
  147.  
  148.  
  149. void
  150. process_region(char *lineFile, char *cornerFile, c_handle im)
  151. {
  152. /*
  153.  * NOTE: The global variables (xstart,ystart  xstop,ystop) must be 
  154.  *     initalized!
  155.  *
  156.  */
  157.     cList  *s_list;
  158.         int no_of_colors; 
  159.         MGvertex *meta_graph;
  160.         MGvertexList *ptr;
  161.         MGvertexList *iss_list;
  162.         MGvertexList *iss_max;
  163.  
  164.  
  165.     cList *vertex_list;
  166.     cList *corner_list;
  167.  
  168.  
  169.  
  170.  
  171.  
  172.     strcpy(IMAGE_NAME,(char *) name(get_2d_world(im)));
  173.     sprintf(IMAGE_NAME_3D,(char *) name(get_3d_world(im)));
  174.     /* 
  175.       * Get the associated projection matrix for this image...
  176.       */
  177.     projectionMat = get_projection_matrix(get_2d_world(im));
  178.  
  179.     /**** Load Low Level Features into Memory ****/
  180.     corner_list = read_corners_from_file(cornerFile);
  181.     line_list = load_line_data(xstart,ystart,xstop,ystop,lineFile,im);
  182.  
  183.  
  184.     buildGraph(corner_list,im, in_pane);
  185.     if (MANUAL) {
  186.         fprintf(stderr,"--------Hit Return To Continue--------\n");
  187.         getchar();
  188.     }
  189.            meta_graph = NULL; 
  190.            no_of_colors=0;
  191.  
  192.            tours(im,corner_list, &no_of_colors, &meta_graph);
  193.     virtualTours(im,corner_list,&no_of_colors,&meta_graph);
  194.  
  195.  
  196.            build_dependency_graph(corner_list,no_of_colors,meta_graph);
  197.  
  198.     /* If K is zero, then compute the independent subsets in 
  199.          * the graph and output the maximum of these subsets, 
  200.      * Otherwise, sort the exisiting meta_graph by node confidence
  201.      * and output the top K.
  202.      */
  203.     if (K == 0)
  204.                iss_list = all_indep_subsets(meta_graph);
  205.     else    
  206.         iss_list = sort_top_K_nodes(meta_graph,K);
  207.  
  208.     if (iss_list != NULL) {
  209.            if (iss_list->eList->vertex != NULL) {
  210.         if (K == 0)
  211.                  iss_max = get_max_iss(iss_list);
  212.         else    
  213.             iss_max = iss_list;
  214.         if (iss_max == NULL) 
  215.             fprintf(stderr,"NULL PTR!!\n");
  216.            refresh(in_pane);
  217.  
  218.         if (!USE_3D)
  219.             poly2curves(in_pane,iss_max,im);
  220.         else{
  221.             printf("3d!!\n");
  222.                    poly2_3dcurves(in_pane,iss_max);
  223.         }
  224.          }
  225.     
  226.  
  227.     } else 
  228.         fprintf(stderr,"ISS NULL\n");
  229.  
  230.     fprintf(stderr,"Done. Free Memory!\n");
  231.     free_meta_graph(meta_graph);
  232.     free_MGvertex_list(iss_list);
  233.  
  234.     free_list(corner_list);
  235.     free_line_list(line_list);
  236.     refresh(in_pane);
  237. }
  238.  
  239.  
  240. void free_list(cList *list)
  241. {
  242.  
  243.     cList *p, *q;
  244.     edgeList *e,*f;
  245.  
  246.     for (p = list; p != NULL; p = q) {
  247.  
  248.         for (e=p->eList; e != NULL; e = f) {
  249.             f = e->next;
  250.             free(e);
  251.         }
  252.         q = p->next;
  253.         free(p);
  254.     }
  255. }
  256.  
  257. void free_line_list(line *list)
  258. {
  259.  
  260.     line *p, *q;
  261.  
  262.     for (p = list; p != NULL; p=q) {
  263.         q = p->next;
  264.         free(p);
  265.     }
  266. }
  267.  
  268. void free_meta_graph(MGvertex *graph)
  269. {
  270.  
  271.     MGvertex *ptr;
  272.     MGvertex *f_ptr;
  273.  
  274.     for (ptr = graph; ptr != NULL; ptr = f_ptr) {
  275.         f_ptr = ptr->next;
  276.         free_vertex_list(ptr->vList);
  277.         free_edge_list(ptr->eList);
  278.         free(ptr);
  279.     }
  280. }
  281.  
  282. void free_vertex_list(vertexList *vlist)
  283. {
  284.  
  285.     vertexList *ptr;
  286.     vertexList *f_ptr;
  287.  
  288.     for (ptr = vlist; ptr != NULL; ptr = f_ptr) {
  289.         f_ptr = ptr->next;
  290.         free(ptr);
  291.     }
  292. }
  293.  
  294. void free_MGvertex_list(MGvertexList *vlist)
  295. {
  296.  
  297.     MGvertexList *ptr;
  298.     MGvertexList *f_ptr;
  299.  
  300.     for (ptr = vlist; ptr != NULL; ptr = f_ptr) {
  301.         f_ptr = ptr->next;
  302. /* CHANGED: May 29, 1996 */
  303.         free_edge_list(ptr->eList);
  304.         free(ptr);
  305.     }
  306. }
  307.  
  308. void free_edge_list(MGedgeList *elist)
  309. {
  310.  
  311.     MGedgeList *ptr;
  312.     MGedgeList *f_ptr;
  313.  
  314.     for (ptr = elist; ptr != NULL; ptr = f_ptr) {
  315.         f_ptr = ptr->next;
  316. /* CHANGED: May 29, 1996 */
  317.         free_meta_graph(ptr->vertex);
  318.         free(ptr);
  319.     }
  320. }
  321. int sleep(int time)
  322. {
  323.  
  324.         int j= 50;
  325.         int i;
  326.  
  327.         printf("Sleep...");
  328.         for (i=0; i < 10000*time; i++)
  329.                 j = j + 1;
  330.         printf("Done.\n");
  331. }
  332.  
  333. void load_and_draw_poly(char *file, c_handle pane)
  334. {
  335.  
  336.         FILE *fp, *fopen();
  337.         int x1,y1,x2,y2;
  338.  
  339.         if ( (fp = fopen(file,"r")) == NULL)  {
  340.                 fprintf(stderr,"Cannot open polygon data\n");
  341.                 return;
  342.         }
  343.  
  344.         while (fscanf(fp,"%d %d %d %d",&x1,&y1,&x2,&y2) != EOF)
  345.                 draw_fat_line(pane,(double)x1,(double)y1,(double)x2,(double)y2,
  346.                                         (double)LINE_WIDTH);
  347.  
  348.         fclose(fp);
  349.  
  350. }
  351.  
  352.  
  353. void
  354. setup_defaults(double Sensitivity)
  355. /*
  356.  * Setup the appropriate global defaults for the building
  357.  * detector.
  358.  *
  359.  * This should depend on the sensitivity rating, 
  360.  * and information from the image (ie- camera pose, illumination)
  361.  * 
  362.  * Currently, this is set only by the sensitivity.
  363.  *
  364.  */
  365. {
  366.  
  367.     MIN_LINE_SUPPORT = DEFAULT_LINE_SUPPORT_LOW -
  368.                 (DEFAULT_LINE_SUPPORT_LOW -
  369.                  DEFAULT_LINE_SUPPORT_HIGH)*Sensitivity;
  370.  
  371.     TOKEN_WINDOW =       TOKEN_WINDOW_LOW -
  372.                 (TOKEN_WINDOW_LOW - TOKEN_WINDOW_HIGH)*
  373.                     Sensitivity;
  374.  
  375.     TOKEN_SUPPORT = TOKEN_SUPPORT_LOW -
  376.                 (TOKEN_SUPPORT_LOW - TOKEN_SUPPORT_HIGH)*
  377.                     Sensitivity;
  378.  
  379.     ANGLE_THRESH    = DEFAULT_ANGLE_THRESH_LOW - 
  380.                 (DEFAULT_ANGLE_THRESH_LOW -
  381.                  DEFAULT_ANGLE_THRESH_HIGH)*Sensitivity;
  382.     LINE_SWEEP_COLUMN = LINE_SWEEP_COLUMN_LOW - 
  383.                 (LINE_SWEEP_COLUMN_LOW -
  384.                   LINE_SWEEP_COLUMN_HIGH)*Sensitivity;
  385.  
  386.     LINE_ENDPOINT_WINDOW = LINE_ENDPOINT_WINDOW_LOW - 
  387.                 (LINE_ENDPOINT_WINDOW_LOW -
  388.                   LINE_ENDPOINT_WINDOW_HIGH)*Sensitivity;
  389.  
  390. }
  391.  
  392. void
  393. set_building_parameters(double angle_error, double line_window,
  394.         double search_angle, double column, 
  395.         double line_support, double token_support,
  396.             double token_window)
  397. {
  398.  
  399.  
  400.  
  401.        MIN_LINE_SUPPORT   = line_support;
  402.        TOKEN_WINDOW       = token_window;
  403.        TOKEN_SUPPORT      = token_support;
  404.        ANGLE_THRESH       = angle_error;
  405.        LINE_SWEEP_COLUMN  = column;
  406.        LINE_ENDPOINT_WINDOW = line_window;
  407.  
  408. }
  409.  
  410.  
  411. /* THE FOLLOWING STUFF WAS ADDED BY BOB COLLINS ON NOV 5, 1995 TO
  412.    DEAL WITH GENERAL TRANSFORMATIONS OF BOUNDING BOXES */
  413.  
  414. double min4(double a, double b, double c, double d)
  415. {
  416.   if ((a < b))
  417.     if ((a < c))
  418.       if ((a < d))
  419.     return(a);
  420.       else
  421.         return(d);
  422.     else
  423.       if ((c < d))
  424.     return(c);
  425.       else
  426.     return(d);
  427.   else 
  428.     if ((b < c))
  429.         if ((b < d))
  430.       return(b);
  431.     else
  432.       return(d);
  433.     else
  434.       if ((c < d))
  435.     return(c);
  436.       else
  437.     return(d);
  438. }
  439.  
  440. double max4(double a, double b, double c, double d)
  441. {
  442.   if ((a > b))
  443.     if ((a > c))
  444.       if ((a > d))
  445.     return(a);
  446.       else
  447.         return(d);
  448.     else
  449.       if ((c > d))
  450.     return(c);
  451.       else
  452.     return(d);
  453.   else 
  454.     if ((b > c))
  455.         if ((b > d))
  456.       return(b);
  457.     else
  458.       return(d);
  459.     else
  460.       if ((c > d))
  461.     return(c);
  462.       else
  463.     return(d);
  464. }
  465.  
  466.  
  467. void
  468. transform_bbox(c_handle transform, double x1, double y1, double x2, double y2,
  469.            double *newx1, double *newy1, double *newx2, double *newy2)
  470. {
  471.   double_2     *userPoint;
  472.   double a1,a2,a3,a4, b1,b2,b3,b4;
  473.  
  474. /*  printf("tbbox input> %f %f %f %f\n", x1, y1, x2, y2); */
  475.  
  476.   userPoint = (double_2 *) transform_point(transform,x1,y1,0);
  477.   a1 = userPoint->d1;  b1 = userPoint->d2;
  478.   userPoint = (double_2 *) transform_point(transform,x1,y2,0);
  479.   a2 = userPoint->d1;  b2 = userPoint->d2;
  480.   userPoint = (double_2 *) transform_point(transform,x2,y1,0);
  481.   a3 = userPoint->d1;  b3 = userPoint->d2;
  482.   userPoint = (double_2 *) transform_point(transform,x2,y2,0);
  483.   a4 = userPoint->d1;  b4 = userPoint->d2;
  484.   *newx1 = min4(a1,a2,a3,a4);  
  485.   *newy1 = min4(b1,b2,b3,b4);  
  486.   *newx2 = max4(a1,a2,a3,a4);  
  487.   *newy2 = max4(b1,b2,b3,b4);  
  488.  
  489. /*  printf("tbbox output> %f %f %f %f\n", *newx1, *newy1, *newx2, *newy2); */
  490.   return;
  491. }
  492.  
  493.  
  494.