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

  1.    /************************************************
  2.    *
  3.    *   adaptive_threshold_segmentation(...
  4.    *
  5.    *   This function segments an image using 
  6.    *   thresholding.  It uses two passes 
  7.    *   to find the hi and low values of the 
  8.    *   threshold.  The first pass uses the peaks
  9.    *   of the histogram to find the hi and low
  10.    *   threshold values.  It thresholds the image
  11.    *   using these hi lows and calculates the means
  12.    *   of the object and background.  Then we use
  13.    *   these means as new peaks to calculate new
  14.    *   hi and low values.  Finally, we threshold
  15.    *   the image again using these second hi low
  16.    *   hi low values.
  17.    *
  18.    *   If the segment parameter is 0, you only
  19.    *   threshold the array - you do not segment.
  20.    *
  21.    *************************************************/
  22.  
  23. adaptive_threshold_segmentation(in_name, out_name,
  24.                                 the_image, out_image,
  25.                                 il, ie, ll, le,
  26.                                 value, segment)
  27.    char   in_name[], out_name[];
  28.    int    il, ie, ll, le, segment;
  29.    short  the_image[ROWS][COLS],
  30.           out_image[ROWS][COLS], value;
  31. {
  32.    int      length, peak1, peak2, width;
  33.    short    background, hi, low, object;
  34.    struct   tiff_header_struct image_header;
  35.    unsigned long histogram[GRAY_LEVELS+1];
  36.  
  37.    if(does_not_exist(out_name)){
  38.       printf("\n\nATS> output file does not exist %s",
  39.               out_name);
  40.       read_tiff_header(in_name, &image_header);
  41.       round_off_image_size(&image_header,
  42.                            &length, &width);
  43.       image_header.image_length = length*ROWS;
  44.       image_header.image_width  = width*COLS;
  45.       create_allocate_tiff_file(out_name, &image_header,
  46.                                 out_image);
  47.    }  /* ends if does_not_exist */
  48.  
  49.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  50.    zero_histogram(histogram);
  51.    calculate_histogram(the_image, histogram);
  52.    smooth_histogram(histogram);
  53.    find_peaks(histogram, &peak1, &peak2);
  54.    peaks_high_low(histogram, peak1, peak2,
  55.                   &hi, &low);
  56.    threshold_and_find_means(the_image, out_image,
  57.                             hi, low, value,
  58.                             &object, &background);
  59.    peaks_high_low(histogram, object, background,
  60.                   &hi, &low);
  61.    threshold_image_array(the_image, out_image,
  62.                          hi, low, value);
  63.    if(segment == 1)
  64.       grow(out_image, value);
  65.    write_array_into_tiff_image(out_name, out_image,
  66.                                il, ie, ll, le);
  67.  
  68. }  /* ends adaptive_threshold_segmentation */
  69.  
  70.    /**************************************************
  71.    *
  72.    *   threshold_and_find_means(...
  73.    *
  74.    *   This function thresholds an input image array
  75.    *   and produces a binary output image array.
  76.    *   If the pixel in the input array is between
  77.    *   the hi and low values, then it is set to value.
  78.    *   Otherwise, it is set to 0.
  79.    *
  80.    ***************************************************/
  81.  
  82. threshold_and_find_means(in_image, out_image, hi, 
  83.                          low, value, object_mean, 
  84.                          background_mean)
  85.    short *background_mean, hi, low,
  86.          in_image[ROWS][COLS], *object_mean,
  87.          out_image[ROWS][COLS], value;
  88. {
  89.    int      counter = 0,
  90.             i,
  91.             j;
  92.    unsigned long object     = 0,
  93.                  background = 0;
  94.  
  95.    for(i=0; i<ROWS; i++){
  96.       for(j=0; j<COLS; j++){
  97.          if(in_image[i][j] >= low  &&
  98.             in_image[i][j] <= hi){
  99.             out_image[i][j] = value;
  100.             counter++;
  101.             object = object + in_image[i][j];
  102.          }
  103.          else{
  104.             out_image[i][j] = 0;
  105.             background      = background + in_image[i][j];
  106.          }
  107.       }  /* ends loop over j */
  108.    }  /* ends loop over i */
  109.    object     = object/counter;
  110.    background = background/((ROWS*COLS)-counter);
  111.    *object_mean     = (short)(object);
  112.    *background_mean = (short)(background);
  113.    printf("\n\tTAFM> set %d points", counter);
  114.    printf("\n\tTAFM> object=%d background=%d",
  115.           *object_mean, *background_mean);
  116. }  /* ends threshold_and_find_means */
  117.  
  118.