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

  1.    /************************************************
  2.    *
  3.    *   valley_threshold_segmentation(...
  4.    *
  5.    *   This function segments an image using 
  6.    *   thresholding.  It uses the histogram valleys
  7.    *   to find the hi and low values of the 
  8.    *   threshold.
  9.    *
  10.    *   If the segment parameter is 0, you only
  11.    *   threshold the array - you do not segment.
  12.    *
  13.    *************************************************/
  14.  
  15. valley_threshold_segmentation(in_name, out_name,
  16.                               the_image, out_image,
  17.                               il, ie, ll, le,
  18.                               value, segment)
  19.    char   in_name[], out_name[];
  20.    int    il, ie, ll, le, segment;
  21.    short  the_image[ROWS][COLS],
  22.           out_image[ROWS][COLS], value;
  23. {
  24.    int      length, peak1, peak2, width;
  25.    short    hi, low;
  26.    struct   tiff_header_struct image_header;
  27.    unsigned long histogram[GRAY_LEVELS+1];
  28.  
  29.    if(does_not_exist(out_name)){
  30.       printf("\n\nVTS> output file does not exist %s",
  31.               out_name);
  32.       read_tiff_header(in_name, &image_header);
  33.       round_off_image_size(&image_header,
  34.                            &length, &width);
  35.       image_header.image_length = length*ROWS;
  36.       image_header.image_width  = width*COLS;
  37.       create_allocate_tiff_file(out_name, &image_header,
  38.                                 out_image);
  39.    }  /* ends if does_not_exist */
  40.  
  41.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  42.    zero_histogram(histogram);
  43.    calculate_histogram(the_image, histogram);
  44.    smooth_histogram(histogram);
  45.    find_peaks(histogram, &peak1, &peak2);
  46.    valley_high_low(histogram, peak1, peak2,
  47.                    &hi, &low);
  48.    threshold_image_array(the_image, out_image,
  49.                          hi, low, value);
  50.    if(segment == 1)
  51.       grow(out_image, value);
  52.    write_array_into_tiff_image(out_name, out_image,
  53.                                il, ie, ll, le);
  54.  
  55. }  /* ends valley_threshold_segmentation */
  56.  
  57.       /********************************************
  58.       *
  59.       *   valley_high_low(...
  60.       *
  61.       *   This function uses the histogram array
  62.       *   and the valleys to find the best high and
  63.       *   low threshold values for the threshold
  64.       *   function.  You want the hi and low values
  65.       *   so that you will threshold the image around
  66.       *   the smaller of the two "humps" in the
  67.       *   histogram.  This is because the smaller
  68.       *   hump represents the objects while the
  69.       *   larger hump represents the background.
  70.       *
  71.       *********************************************/
  72.  
  73. valley_high_low(histogram, peak1, peak2, hi, low)
  74.    int  peak1, peak2;
  75.    short *hi, *low;
  76.    unsigned long histogram[];
  77. {
  78.    int  i, valley_point;
  79.    unsigned long sum1 = 0, sum2 = 0;
  80.  
  81.    find_valley_point(histogram, peak1, peak2, 
  82.                      &valley_point);
  83.    /*printf("\nVHL> valley point is %d", 
  84.             valley_point);*/
  85.  
  86.    for(i=0; i<valley_point; i++)
  87.       sum1 = sum1 + histogram[i];
  88.    for(i=valley_point; i<=GRAY_LEVELS; i++)
  89.       sum2 = sum2 + histogram[i];
  90.  
  91.    if(sum1 >= sum2){
  92.       *low = valley_point;
  93.       *hi  = GRAY_LEVELS;
  94.    }
  95.    else{
  96.       *low = 0;
  97.       *hi  = valley_point;
  98.    }
  99.  
  100. }  /* ends valley_high_low */
  101.  
  102.    /********************************************
  103.    *
  104.    *   find_valley_point(...
  105.    *
  106.    *   This function finds the low point of
  107.    *   the valley between two peaks in a
  108.    *   histogram.  It starts at the lowest
  109.    *   peak and works its way up to the
  110.    *   highest peak.  Along the way, it looks
  111.    *   at each point in the histogram and inserts
  112.    *   them into a list of points.  When done,
  113.    *   it has the location of the smallest histogram
  114.    *   point - that is the valley point.
  115.    *
  116.    *   The deltas array holds the delta value
  117.    *   in the first place and its location in
  118.    *   the second place.
  119.    *
  120.    *********************************************/
  121.  
  122. find_valley_point(histogram, peak1, peak2, valley_point)
  123.    int  peak1, peak2, *valley_point;
  124.    unsigned long histogram[];
  125. {
  126.    int      deltas[PEAKS][2], delta_hist, i;
  127.    for(i=0; i<PEAKS; i++){
  128.       deltas[i][0] = 10000;
  129.       deltas[i][1] =    -1;
  130.    }
  131.  
  132.    if(peak1 < peak2){
  133.       for(i=peak1+1; i<peak2; i++){
  134.          delta_hist = (int)(histogram[i]);
  135.          insert_into_deltas(deltas, delta_hist, i);
  136.       }  /* ends loop over i */
  137.    }  /* ends if peak1 < peak2 */
  138.  
  139.    if(peak2 < peak1){
  140.       for(i=peak2+1; i<peak1; i++){
  141.          delta_hist = (int)(histogram[i]);
  142.          insert_into_deltas(deltas, delta_hist, i);
  143.       }  /* ends loop over i */
  144.    }  /* ends if peak2 < peak1 */
  145.  
  146.    *valley_point = deltas[0][1];
  147.  
  148. }  /* ends find_valley_point */
  149.  
  150.    /********************************************
  151.    *
  152.    *   insert_into_deltas(...
  153.    *
  154.    *   This function inserts histogram deltas
  155.    *   into a deltas array.  The smallest delta
  156.    *   will be at the top of the array.
  157.    *
  158.    *   The objective is to build a list of
  159.    *   histogram area deltas and thier locations.
  160.    *
  161.    *   The deltas array holds the delta value
  162.    *   in the first place and its location in
  163.    *   the second place.
  164.    *
  165.    *********************************************/
  166. insert_into_deltas(deltas, value, place)
  167.    int value, place, deltas[PEAKS][2];
  168. {
  169.    int i, j;
  170.       /* first case */
  171.    if(value < deltas[0][0]){
  172.       for(i=PEAKS-1; i>0; i--){
  173.          deltas[i][0] = deltas[i-1][0];
  174.          deltas[i][1] = deltas[i-1][1];
  175.       }
  176.       deltas[0][0] = value;
  177.       deltas[0][1] = place;
  178.    }  /* ends if */
  179.  
  180.       /* middle cases */
  181.    for(j=0; j<PEAKS-3; j++){
  182.       if(value > deltas[j][0]  && 
  183.          value < deltas[j+1][0]){
  184.          for(i=PEAKS-1; i>j+1; i--){
  185.             deltas[i][0] = deltas[i-1][0];
  186.             deltas[i][1] = deltas[i-1][1];
  187.          }
  188.          deltas[j+1][0] = value;
  189.          deltas[j+1][1] = place;
  190.       }  /* ends if */
  191.    }  /* ends loop over j */
  192.  
  193.       /* last case */
  194.    if(value > deltas[PEAKS-2][0]  && 
  195.       value < deltas[PEAKS-1][0]){
  196.       deltas[PEAKS-1][0] = value;
  197.       deltas[PEAKS-1][1] = place;
  198.    }  /* ends if */
  199.  
  200. }  /* ends insert_into_deltas */
  201.  
  202.