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 < prev    next >
C/C++ Source or Header  |  1996-02-29  |  11KB  |  489 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.         
  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.     } else 
  227.         fprintf(stderr,"ISS NULL\n");
  228.  
  229.     fprintf(stderr,"Done. Free Memory!\n");
  230.     free_meta_graph(meta_graph);
  231.     free_MGvertex_list(iss_list);
  232.  
  233.     free_list(corner_list);
  234.     free_line_list(line_list);
  235.     refresh(in_pane);
  236. }
  237.  
  238.  
  239. void free_list(cList *list)
  240. {
  241.  
  242.     cList *p, *q;
  243.     edgeList *e,*f;
  244.  
  245.     for (p = list; p != NULL; p = q) {
  246.  
  247.         for (e=p->eList; e != NULL; e = f) {
  248.             f = e->next;
  249.             free(e);
  250.         }
  251.         q = p->next;
  252.         free(p);
  253.     }
  254. }
  255.  
  256. void free_line_list(line *list)
  257. {
  258.  
  259.     line *p, *q;
  260.  
  261.     for (p = list; p != NULL; p=q) {
  262.         q = p->next;
  263.         free(p);
  264.     }
  265. }
  266.  
  267. void free_meta_graph(MGvertex *graph)
  268. {
  269.  
  270.     MGvertex *ptr;
  271.     MGvertex *f_ptr;
  272.  
  273.     for (ptr = graph; ptr != NULL; ptr = f_ptr) {
  274.         f_ptr = ptr->next;
  275.         free_vertex_list(ptr->vList);
  276.         free_edge_list(ptr->eList);
  277.         free(ptr);
  278.     }
  279. }
  280.  
  281. void free_vertex_list(vertexList *vlist)
  282. {
  283.  
  284.     vertexList *ptr;
  285.     vertexList *f_ptr;
  286.  
  287.     for (ptr = vlist; ptr != NULL; ptr = f_ptr) {
  288.         f_ptr = ptr->next;
  289.         free(ptr);
  290.     }
  291. }
  292.  
  293. void free_MGvertex_list(MGvertexList *vlist)
  294. {
  295.  
  296.     MGvertexList *ptr;
  297.     MGvertexList *f_ptr;
  298.  
  299.     for (ptr = vlist; ptr != NULL; ptr = f_ptr) {
  300.         f_ptr = ptr->next;
  301.         free(ptr);
  302.     }
  303. }
  304.  
  305. void free_edge_list(MGedgeList *elist)
  306. {
  307.  
  308.     MGedgeList *ptr;
  309.     MGedgeList *f_ptr;
  310.  
  311.     for (ptr = elist; ptr != NULL; ptr = f_ptr) {
  312.         f_ptr = ptr->next;
  313.         free(ptr);
  314.     }
  315. }
  316. int sleep(int time)
  317. {
  318.  
  319.         int j= 50;
  320.         int i;
  321.  
  322.         printf("Sleep...");
  323.         for (i=0; i < 10000*time; i++)
  324.                 j = j + 1;
  325.         printf("Done.\n");
  326. }
  327.  
  328. void load_and_draw_poly(char *file, c_handle pane)
  329. {
  330.  
  331.         FILE *fp, *fopen();
  332.         int x1,y1,x2,y2;
  333.  
  334.         if ( (fp = fopen(file,"r")) == NULL)  {
  335.                 fprintf(stderr,"Cannot open polygon data\n");
  336.                 return;
  337.         }
  338.  
  339.         while (fscanf(fp,"%d %d %d %d",&x1,&y1,&x2,&y2) != EOF)
  340.                 draw_fat_line(pane,(double)x1,(double)y1,(double)x2,(double)y2,
  341.                                         (double)LINE_WIDTH);
  342.  
  343.         fclose(fp);
  344.  
  345. }
  346.  
  347.  
  348. void
  349. setup_defaults(double Sensitivity)
  350. /*
  351.  * Setup the appropriate global defaults for the building
  352.  * detector.
  353.  *
  354.  * This should depend on the sensitivity rating, 
  355.  * and information from the image (ie- camera pose, illumination)
  356.  * 
  357.  * Currently, this is set only by the sensitivity.
  358.  *
  359.  */
  360. {
  361.  
  362.     MIN_LINE_SUPPORT = DEFAULT_LINE_SUPPORT_LOW -
  363.                 (DEFAULT_LINE_SUPPORT_LOW -
  364.                  DEFAULT_LINE_SUPPORT_HIGH)*Sensitivity;
  365.  
  366.     TOKEN_WINDOW =       TOKEN_WINDOW_LOW -
  367.                 (TOKEN_WINDOW_LOW - TOKEN_WINDOW_HIGH)*
  368.                     Sensitivity;
  369.  
  370.     TOKEN_SUPPORT = TOKEN_SUPPORT_LOW -
  371.                 (TOKEN_SUPPORT_LOW - TOKEN_SUPPORT_HIGH)*
  372.                     Sensitivity;
  373.  
  374.     ANGLE_THRESH    = DEFAULT_ANGLE_THRESH_LOW - 
  375.                 (DEFAULT_ANGLE_THRESH_LOW -
  376.                  DEFAULT_ANGLE_THRESH_HIGH)*Sensitivity;
  377.     LINE_SWEEP_COLUMN = LINE_SWEEP_COLUMN_LOW - 
  378.                 (LINE_SWEEP_COLUMN_LOW -
  379.                   LINE_SWEEP_COLUMN_HIGH)*Sensitivity;
  380.  
  381.     LINE_ENDPOINT_WINDOW = LINE_ENDPOINT_WINDOW_LOW - 
  382.                 (LINE_ENDPOINT_WINDOW_LOW -
  383.                   LINE_ENDPOINT_WINDOW_HIGH)*Sensitivity;
  384.  
  385. }
  386.  
  387. void
  388. set_building_parameters(double angle_error, double line_window,
  389.         double search_angle, double column, 
  390.         double line_support, double token_support,
  391.             double token_window)
  392. {
  393.  
  394.  
  395.  
  396.        MIN_LINE_SUPPORT   = line_support;
  397.        TOKEN_WINDOW       = token_window;
  398.        TOKEN_SUPPORT      = token_support;
  399.        ANGLE_THRESH       = angle_error;
  400.        LINE_SWEEP_COLUMN  = column;
  401.        LINE_ENDPOINT_WINDOW = line_window;
  402.  
  403. }
  404.  
  405.  
  406. /* THE FOLLOWING STUFF WAS ADDED BY BOB COLLINS ON NOV 5, 1995 TO
  407.    DEAL WITH GENERAL TRANSFORMATIONS OF BOUNDING BOXES */
  408.  
  409. double min4(double a, double b, double c, double d)
  410. {
  411.   if ((a < b))
  412.     if ((a < c))
  413.       if ((a < d))
  414.     return(a);
  415.       else
  416.         return(d);
  417.     else
  418.       if ((c < d))
  419.     return(c);
  420.       else
  421.     return(d);
  422.   else 
  423.     if ((b < c))
  424.         if ((b < d))
  425.       return(b);
  426.     else
  427.       return(d);
  428.     else
  429.       if ((c < d))
  430.     return(c);
  431.       else
  432.     return(d);
  433. }
  434.  
  435. double max4(double a, double b, double c, double d)
  436. {
  437.   if ((a > b))
  438.     if ((a > c))
  439.       if ((a > d))
  440.     return(a);
  441.       else
  442.         return(d);
  443.     else
  444.       if ((c > d))
  445.     return(c);
  446.       else
  447.     return(d);
  448.   else 
  449.     if ((b > c))
  450.         if ((b > d))
  451.       return(b);
  452.     else
  453.       return(d);
  454.     else
  455.       if ((c > d))
  456.     return(c);
  457.       else
  458.     return(d);
  459. }
  460.  
  461.  
  462. void
  463. transform_bbox(c_handle transform, double x1, double y1, double x2, double y2,
  464.            double *newx1, double *newy1, double *newx2, double *newy2)
  465. {
  466.   double_2     *userPoint;
  467.   double a1,a2,a3,a4, b1,b2,b3,b4;
  468.  
  469. /*  printf("tbbox input> %f %f %f %f\n", x1, y1, x2, y2); */
  470.  
  471.   userPoint = (double_2 *) transform_point(transform,x1,y1,0);
  472.   a1 = userPoint->d1;  b1 = userPoint->d2;
  473.   userPoint = (double_2 *) transform_point(transform,x1,y2,0);
  474.   a2 = userPoint->d1;  b2 = userPoint->d2;
  475.   userPoint = (double_2 *) transform_point(transform,x2,y1,0);
  476.   a3 = userPoint->d1;  b3 = userPoint->d2;
  477.   userPoint = (double_2 *) transform_point(transform,x2,y2,0);
  478.   a4 = userPoint->d1;  b4 = userPoint->d2;
  479.   *newx1 = min4(a1,a2,a3,a4);  
  480.   *newy1 = min4(b1,b2,b3,b4);  
  481.   *newx2 = max4(a1,a2,a3,a4);  
  482.   *newy2 = max4(b1,b2,b3,b4);  
  483.  
  484. /*  printf("tbbox output> %f %f %f %f\n", *newx1, *newy1, *newx2, *newy2); */
  485.   return;
  486. }
  487.  
  488.  
  489.