/************************************************
*
* adaptive_threshold_segmentation(...
*
* This function segments an image using
* thresholding. It uses two passes
* to find the hi and low values of the
* threshold. The first pass uses the peaks
* of the histogram to find the hi and low
* threshold values. It thresholds the image
* using these hi lows and calculates the means
* of the object and background. Then we use
* these means as new peaks to calculate new
* hi and low values. Finally, we threshold
* the image again using these second hi low
* hi low values.
*
* If the segment parameter is 0, you only
* threshold the array - you do not segment.
*
**************************************************/
adaptive_threshold_segmentation(in_name, out_name,
the_image, out_image,
il, ie, ll, le,
value, segment)
char in_name[], out_name[];
int il, ie, ll, le, segment;
short the_image[ROWS] [COLS], out_image[ROWS] [COLS], value;
{
int length, peak1, peak2, width;
short background, hi, low, object;
struct tiff_header_struct image_header;
unsigned long-histogram[GRAY_LEVELS+1];
if(does_not exist(out_name)){
printf("\n\nATS> output file does not exist %s", out_name);
read_tiff_header(in_name, &image_header);
round_off_image_size(&image_header, &length, &width);
image_header.image_length = length*ROWS;
image_header.image_width = width*COLS;
create_allocate_tiff_file(out_name, &image_header, out_image);
} /* ends if does not exist */
read_tiff_image(in_name, the_image, il, ie, ll, le);
zero_histogram(histogram);
calculate_histogram(the_image, histogram);
smooth_histogram(histogram);
find_peaks(histogram, &peak1, &peak2);
peaks_high_low(histogram, peak1, peak2, &hi, &low);
threshold_and_find_means(the_image, out_image,
hi, low, value,
&object, &background);
peaks_high_low(histogram, object, background,
&hi, &low);
threshold_image_array(the_image, out_image, hi, low, value);
if(segment == 1)
grow(out_image, value);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends adaptive_threshold_segmentation */
/************************************************
*
* threshold_and_find_means(...
*
* This function thresholds an input image array
* and produces a binary output image array.
* If the pixel in the input array is between
* the hi and low values, then it is set to value.
* Otherwise, it is set to 0.
*
************************************************/
threshold_and_find_means(in_image, out_image, hi,
low, value, object_mean,
background_mean)
short *background_mean, hi, low,
in_image[ROWS][COLS], *object_mean,
out_image[ROWS][COLS], value;
{
int counter = 0,
i,
j;
unsigned long object = 0,
background = 0;
for(i=0; i<ROWS; i++){
for(j=0; j<COLS; j++){
if(in_image[i][j] >= low && in_image[i][j] <= hi){
out_image[i][j] = value;
counter++;
object = object + in_image[i][j];
}
else{
out_image[i][j] : 0;
background = background + in_image[i][j];
}
} /* ends loop over j */
} /* ends loop over i */
object = object/counter;
background = background/((ROWS*COLS)-counter);
*object_mean = (short)(object);
*background mean = (short)(background);
printf("\n\tTAFM> set %d points", counter);
printf("\n\tTAFM> object=%d background=%d",
*object_mean, *background_mean);
} /* ends threshold_and_find_means */
/* End of File */