home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_02 / 1102069a < prev    next >
Text File  |  1992-12-13  |  14KB  |  467 lines

  1.     /**********************************************
  2.     *
  3.     *  grow(...
  4.     *
  5.     *  This function is an object detector.
  6.     *  Its input is an binary image array 
  7.     *  containing 0's and value's.
  8.     *  It searches through the image and connects
  9.     *  the adjacent values.
  10.     *
  11.     ***********************************************/
  12.  
  13. grow(binary, value)
  14.    short binary[ROWS][COLS],
  15.          value;
  16. {
  17.    char name[80];
  18.  
  19.    int first_call,
  20.        i,
  21.        j,
  22.        object_found,
  23.        pointer,
  24.        pop_i,
  25.        pop_j,
  26.        stack_empty,
  27.        stack_file_in_use;
  28.  
  29.    short g_label, stack[STACK_SIZE][2];
  30.  
  31.             /*************************************
  32.             *
  33.             *   Now begin the process of growing
  34.             *   regions.
  35.             *
  36.             **************************************/
  37.  
  38.    g_label       = 2;
  39.    object_found  = 0;
  40.    first_call    = 1;
  41.  
  42.    for(i=0; i<ROWS; i++){
  43.       for(j=0; j<COLS; j++){
  44.          stack_file_in_use =  0;
  45.          stack_empty       =  1;
  46.          pointer           = -1;
  47.  
  48.                /**********************************
  49.                *
  50.                *  Search for the first pixel of
  51.                *  a region.
  52.                *
  53.                ***********************************/
  54.  
  55.          if(binary[i][j] == value){
  56.             label_and_check_neighbor(binary, stack, g_label,
  57.                            &stack_empty, &pointer, i, j,
  58.                            value, &stack_file_in_use,
  59.                            &first_call);
  60.             object_found = 1;
  61.          }  /* ends if binary[i]j] == value */
  62.  
  63.                /*****************************
  64.                *
  65.                *  If the stack is not empty,
  66.                *  pop the coordinates of
  67.                *  the pixel off the stack
  68.                *  and check its 8 neighbors.
  69.                *
  70.                *******************************/
  71.  
  72.          while(stack_empty == 0){
  73.             pop_i = stack[pointer][0]; /* POP       */
  74.             pop_j = stack[pointer][1]; /* OPERATION */
  75.             --pointer;
  76.             if(pointer <= 0){
  77.                if(stack_file_in_use){
  78.                   pop_data_off_of_stack_file(
  79.                                  stack,
  80.                                  &pointer,
  81.                                  &stack_file_in_use);
  82.                }  /* ends if stack_file_in_use  */
  83.                else{
  84.                   pointer     = 0;
  85.                   stack_empty = 1;
  86.                }  /* ends else stack file is
  87.                      not in use  */
  88.             }  /*  ends if point <= 0  */
  89.  
  90.             label_and_check_neighbor(binary,
  91.                         stack, g_label,
  92.                         &stack_empty,
  93.                         &pointer, pop_i,
  94.                         pop_j, value,
  95.                         &stack_file_in_use,
  96.                         &first_call);
  97.          }  /* ends while stack_empty == 0 */
  98.  
  99.          if(object_found == 1){
  100.             object_found = 0;
  101.             ++g_label;
  102.          }  /* ends if object_found == 1 */
  103.  
  104.       }   /* ends loop over j */
  105.    }  /* ends loop over i */
  106.  
  107.    printf("\nGROW> found %d objects", g_label);
  108.  
  109. } /* ends grow  */
  110.  
  111.    /********************************************
  112.    *
  113.    *  label_and_check_neighbors(...
  114.    *
  115.    *  This function labels a pixel with an object
  116.    *  label and then checks the pixel's 8
  117.    *  neighbors.  If any of the neigbors are
  118.    *  set, then they are also labeled.
  119.    *
  120.    ***********************************************/
  121.  
  122. label_and_check_neighbor(binary_image, stack,
  123.                          g_label, stack_empty,
  124.                          pointer, r, e, value,
  125.                          stack_file_in_use,
  126.                          first_call)
  127. int   e,
  128.       *first_call,
  129.       *pointer,
  130.       r,
  131.       *stack_empty,
  132.       *stack_file_in_use;
  133.  
  134. short binary_image[ROWS][COLS],
  135.       g_label,
  136.       stack[STACK_SIZE][2],
  137.       value;
  138. {
  139.    int already_labeled = 0,
  140.        i, j;
  141.  
  142.    if (binary_image[r][e] == g_label)
  143.       already_labeled = 1;
  144.  
  145.    binary_image[r][e] = g_label;
  146.  
  147.       /***************************************
  148.       *
  149.       *   Look at the 8 neighors of the
  150.       *   point r,e.
  151.       *
  152.       *   Ensure the points you are checking
  153.       *   are in the image, i.e. not less
  154.       *   than zero and not greater than
  155.       *   ROWS-1 or COLS-1.
  156.       *
  157.       ***************************************/
  158.  
  159.    for(i=(r-1); i<=(r+1); i++){
  160.       for(j=(e-1); j<=(e+1); j++){
  161.  
  162.          if((i>=0)   &&
  163.             (i<=ROWS-1)  &&
  164.             (j>=0)   &&
  165.             (j<=COLS-1)){
  166.  
  167.             if(binary_image[i][j] == value){
  168.                *pointer           = *pointer + 1;
  169.                stack[*pointer][0] = i; /* PUSH      */
  170.                stack[*pointer][1] = j; /* OPERATION */
  171.                *stack_empty       = 0;
  172.  
  173.                if(*pointer >= (STACK_SIZE -
  174.                                STACK_FILE_LENGTH)){
  175.                   push_data_onto_stack_file(stack,
  176.                             pointer, first_call);
  177.                   *stack_file_in_use = 1;
  178.                }  /* ends if *pointer >=
  179.                      STACK_SIZE - STACK_FILE_LENGTH*/
  180.  
  181.             }  /* end of if binary_image == value */
  182.          }  /* end if i and j are on the image */
  183.       }  /* ends loop over i rows           */
  184.    }  /* ends loop over j columns        */
  185. }  /* ends label_and_check_neighbors  */
  186.  
  187.    /****************************************
  188.    *
  189.    *   push_data_onto_stack_file(...
  190.    *
  191.    *   This function takes the stack array
  192.    *   and pushes it onto the stack file.
  193.    *
  194.    *****************************************/
  195.  
  196. push_data_onto_stack_file(stack, pointer, first_call)
  197.    int   *first_call, *pointer;
  198.    short stack[STACK_SIZE][2];
  199. {
  200.    char  backup_file_name[MAX_NAME_LENGTH];
  201.    FILE  *backup_file_pointer, *stack_file_pointer;
  202.    int   diff, i;
  203.    short holder[STACK_FILE_LENGTH][2];
  204.  
  205.    printf("\nSFO> Start of push_data_onto_stack ");
  206.  
  207.    diff = STACK_SIZE - STACK_FILE_LENGTH;
  208.  
  209.        /*******************************************
  210.        *
  211.        *   Copy the elements to be stored to the
  212.        *   stack file into holder
  213.        *
  214.        ********************************************/
  215.  
  216.    for(i=0; i<STACK_FILE_LENGTH; i++){
  217.       holder[i][0] = stack[i][0];
  218.       holder[i][1] = stack[i][1];
  219.    }
  220.        /*******************************************
  221.        *
  222.        *   Move the elements of the stack down
  223.        *
  224.        *******************************************/
  225.  
  226.    for(i=0; i<diff; i++){
  227.       stack[i][0] = stack[i + STACK_FILE_LENGTH][0];
  228.       stack[i][1] = stack[i + STACK_FILE_LENGTH][1];
  229.    }
  230.  
  231.        /*******************************************
  232.        *
  233.        *   Fill the top of the stack with zeros
  234.        *
  235.        *******************************************/
  236.  
  237.    for(i=diff; i<STACK_SIZE; i++){
  238.       stack[i][0] = 0;
  239.       stack[i][1] = 0;
  240.    }
  241.  
  242.    *pointer = *pointer - STACK_FILE_LENGTH;
  243.  
  244.        /************************************************
  245.        *
  246.        *   Store the holder array into the stack file.
  247.        *   Open the stack file for writing in binary
  248.        *   mode. If the file does not exist it will be
  249.        *   created. If the file does exist it will be
  250.        *   over written.
  251.        *
  252.        *   PUSH - IF first_time == 1 then write to stack
  253.        *          ELSE write to stack.bak
  254.        *          append stack onto stack.bak
  255.        *          copy stack.bak to stack
  256.        *          this has the effect of writing
  257.        *          to the beginning of the stack.
  258.        *
  259.        ************************************************/
  260.  
  261.    if(*first_call == 1){
  262.  
  263.       *first_call = *first_call + 1;
  264.       if((stack_file_pointer = fopen(STACK_FILE,"wb"))
  265.                                      == NULL)
  266.          printf("\nSFO> Could not open stack file");
  267.       else{
  268.          /*printf("\n\nSFO> Writing to stack file");*/
  269.          fwrite(holder, sizeof(holder),
  270.                 1, stack_file_pointer);
  271.          fclose(stack_file_pointer);
  272.       }  /*  ends else could not open stack_file  */
  273.  
  274.    }  /*  ends if *first_call == 1  */
  275.    else{  /* else stack file has been used already  */
  276.       strcpy(backup_file_name, STACK_FILE);
  277.       append_string(".bak\0", backup_file_name);
  278.       if((backup_file_pointer =
  279.           fopen(backup_file_name, "wb")) == NULL)
  280.          printf("\nSFO> Could not open backup file");
  281.       else{
  282.          /*printf("\n\nSFO> Writing to backup file");*/
  283.          fwrite(holder, sizeof(holder),
  284.                 1, backup_file_pointer);
  285.          fclose(backup_file_pointer);
  286.       }  /*  ends else could not open backup_file  */
  287.  
  288.       append_stack_files(backup_file_name,
  289.                          STACK_FILE, holder);
  290.       copy_stack_files(backup_file_name,
  291.                        STACK_FILE, holder);
  292.  
  293.    }  /*  ends else first_call != 1  */
  294.  
  295.    printf("--- End of push_data_onto_stack");
  296.  
  297. }  /* ends push_data_onto_stack_file  */
  298.  
  299.    /****************************************
  300.    *
  301.    *   pop_data_off_of_stack_file(...
  302.    *
  303.    *   This function pops the stack array
  304.    *   off of the stack file.
  305.    *
  306.    *****************************************/
  307.  
  308. pop_data_off_of_stack_file(stack, pointer,
  309.                            stack_file_in_use)
  310.    int   *pointer, *stack_file_in_use;
  311.    short stack[STACK_SIZE][2];
  312. {
  313.    char  backup_file_name[MAX_NAME_LENGTH];
  314.    FILE  *backup_file_pointer, *stack_file_pointer;
  315.    int   i;
  316.    long  write_counter;
  317.    short holder[STACK_FILE_LENGTH][2],
  318.          holder2[STACK_FILE_LENGTH][2];
  319.  
  320.        /*******************************************
  321.        *
  322.        *   POP - Read 1 time from stack
  323.        *         Copy the remainder of stack to
  324.        *            stack.bak
  325.        *         Copy stack.bak to stack
  326.        *         This has the effect of popping off
  327.        *         of the stack.
  328.        *
  329.        *   Read the holder array from the stack file.
  330.        *   Open the stack file for reading in binary
  331.        *   mode.
  332.        *
  333.        *   If it requires more than one write to
  334.        *   copy the remainder of stack to
  335.        *   stack.bak then there is still data in the
  336.        *   stack file so set stack_file_in_use = 1.
  337.        *   Else set it to 0.
  338.        *
  339.        **********************************************/
  340.  
  341.    printf("\nSFO> Start of pop_data_off_of_stack ");
  342.    write_counter = 0;
  343.  
  344.    strcpy(backup_file_name, STACK_FILE);
  345.    append_string(".bak\0", backup_file_name);
  346.  
  347.    if( (stack_file_pointer =
  348.           fopen(STACK_FILE, "rb")) == NULL)
  349.       printf("\nSFO> Could not open stack file");
  350.    else{
  351.       /*printf("\n\nSFO> Reading from stack file");*/
  352.       fread(holder, sizeof(holder),
  353.             1, stack_file_pointer);
  354.       backup_file_pointer =
  355.             fopen(backup_file_name, "wb");
  356.       while( fread(holder2, sizeof(holder2),
  357.                    1, stack_file_pointer) ){
  358.          fwrite(holder2, sizeof(holder2),
  359.                 1, backup_file_pointer);
  360.          ++write_counter;
  361.       }  /* ends while reading  */
  362.       if(write_counter > 0)
  363.          *stack_file_in_use = 1;
  364.       else
  365.          *stack_file_in_use = 0;
  366.  
  367.       fclose(backup_file_pointer);
  368.       fclose(stack_file_pointer);
  369.    }  /* ends else could not open stack file  */
  370.  
  371.    copy_stack_files(backup_file_name,
  372.                     STACK_FILE, holder2);
  373.  
  374.    for(i=0; i<STACK_FILE_LENGTH; i++){
  375.       stack[i][0] = holder[i][0];
  376.       stack[i][1] = holder[i][1];
  377.    }
  378.  
  379.    *pointer = *pointer + STACK_FILE_LENGTH - 1;
  380.  
  381.    printf("--- End of pop_data_off_of_stack");
  382. }  /* ends pop_data_off_of_stack_file  */
  383.  
  384.    /*********************************************
  385.    *
  386.    *   append_stack_files(...
  387.    *
  388.    *   Append the second file onto the end
  389.    *   of the first.
  390.    *
  391.    ***********************************************/
  392.  
  393. append_stack_files(first_file, second_file, holder)
  394.    char first_file[], second_file[];
  395.    short holder[STACK_FILE_LENGTH][2];
  396. {
  397.    FILE  *first, *second;
  398.    int   i;
  399.  
  400.    if((first = fopen(first_file, "r+b")) == NULL)
  401.       printf("\n\nSFO> Cannot open file %s",
  402.              first_file);
  403.  
  404.    if((second = fopen(second_file, "rb")) == NULL)
  405.       printf("\n\nSFO> Cannot open file %s",
  406.              second_file);
  407.  
  408.           /*****************************************
  409.           *
  410.           *  Seek to the end of the first file and
  411.           *  to the beginning of the second file.
  412.           *
  413.           *******************************************/
  414.  
  415.    fseek(first, 0L, 2);
  416.    fseek(second, 0L, 0);
  417.  
  418.    while(fread(holder, sizeof(holder), 1, second) ){
  419.       fwrite(holder, sizeof(holder), 1, first);
  420.    }  /* ends while reading  */
  421.  
  422.    fclose(first);
  423.    fclose(second);
  424.  
  425. }  /*  ends append_stack_files  */
  426.  
  427.    /********************************************
  428.    *
  429.    *   copy_stack_files(...
  430.    *
  431.    *   Copy the first file to the second.
  432.    *
  433.    **********************************************/
  434.  
  435. copy_stack_files(first_file, second_file, holder)
  436.    char first_file[], second_file[];
  437.    short holder[STACK_FILE_LENGTH][2];
  438. {
  439.    FILE  *first, *second;
  440.    int   i;
  441.  
  442.    if( (first = fopen(first_file, "rb")) == NULL)
  443.       printf("\n\nSFO> Cannot open file %s",
  444.              first_file);
  445.  
  446.    if( (second = fopen(second_file, "wb")) == NULL)
  447.       printf("\n\nSFO> Cannot open file %s",
  448.              second_file);
  449.  
  450.           /******************************************
  451.           *
  452.           *  Seek to the beginning of the first file.
  453.           *
  454.           *******************************************/
  455.  
  456.    fseek(first, 0L, 0);
  457.  
  458.    while( fread(holder, sizeof(holder), 1, first) ){
  459.       fwrite(holder, sizeof(holder), 1, second);
  460.    }  /* ends while reading  */
  461.  
  462.    fclose(first);
  463.    fclose(second);
  464.  
  465. }  /*  ends copy_stack_files */
  466.  
  467.