📄 segments.c
字号:
/***************************
*
* segments.c
* COMPOSITE FILE COMPRISING:
* segment.c
* segment2.c
*
***************************\
/*********************************************
*
* file d:\cips\segment.c
*
* Functions: This file contains
* adaptive_threshold_segmentation
* append_stack_files
* copy_stack_files
* find_peaks
* find_valley_point
* grow
* insert_into_peaks
* insert_into_deltas
* label_and_check_neighbors
* manual_threshold_segmentation
* peak_threshold_segmentation
* peaks_high_low
* push_data_onto_stack_file
* pop_data_off_of_stack_file
* threshold_image_array
* valley_high_low
* valley_threshold_segmentation
* get_segmentation_options
*
* Purpose:
* These functions are part of histogram
* based image segmentation.
*
* External Calls:
* wtiff.c - round_off_image_size
* create_file_if_needed
* write_array_into_tiff_image
* tiff.c - read_tiff_header
* rtiff.c - read_tiff_image
* numcvrt.c - get_integer
* get_short
*
* Modifications:
* October 1992 - created
*
************************************************/
#include "cips.h"
/**************************************************
*
* manual_threshold_segmentation(...
*
* This function segments an image using thresholding
* given the hi and low values of the threshold
* by the calling routine. It reads in an image
* and writes the result to the output image.
*
* If the segment parameter is 0, you only
* threshold the array - you do not segment.
*
***************************************************/
manual_threshold_segmentation(in_name, out_name,
the_image, out_image,
il, ie, ll, le,
hi, low, value, segment)
char in_name[], out_name[];
int il, ie, ll, le, segment;
short hi, low, the_image[ROWS][COLS],
out_image[ROWS][COLS], value;
{
int length, width;
struct tiff_header_struct image_header;
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
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 manual_threshold_segmentation */
/************************************************
*
* peak_threshold_segmentation(...
*
* This function segments an image using
* thresholding. It uses the histogram peaks
* to find the hi and low values of the
* threshold.
*
* If the segment parameter is 0, you only
* threshold the array - you do not segment.
*
*************************************************/
peak_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 hi, low;
struct tiff_header_struct image_header;
unsigned long histogram[GRAY_LEVELS+1];
create_file_if_needed(in_name, out_name, out_image);
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_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 peak_threshold_segmentation */
/************************************************
*
* valley_threshold_segmentation(...
*
* This function segments an image using
* thresholding. It uses the histogram valleys
* to find the hi and low values of the
* threshold.
*
* If the segment parameter is 0, you only
* threshold the array - you do not segment.
*
*************************************************/
valley_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 hi, low;
struct tiff_header_struct image_header;
unsigned long histogram[GRAY_LEVELS+1];
create_file_if_needed(in_name, out_name, out_image);
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);
valley_high_low(histogram, peak1, peak2,
&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 valley_threshold_segmentation */
/************************************************
*
* 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];
create_file_if_needed(in_name, out_name, out_image);
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_image_array(...
*
* 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_image_array(in_image, out_image, hi, low, value)
short hi, low, in_image[ROWS][COLS],
out_image[ROWS][COLS], value;
{
int counter = 0, i, j;
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++;
}
else
out_image[i][j] = 0;
} /* ends loop over j */
} /* ends loop over i */
printf("\n\tTIA> set %d points", counter);
} /* ends threshold_image_array */
/**************************************************
*
* 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 */
/**********************************************
*
* grow(...
*
* This function is an object detector.
* Its input is an binary image array
* containing 0's and value's.
* It searches through the image and connects
* the adjacent values.
*
***********************************************/
grow(binary, value)
short binary[ROWS][COLS],
value;
{
char name[80];
int first_call,
i,
j,
object_found,
pointer,
pop_i,
pop_j,
stack_empty,
stack_file_in_use;
short g_label, stack[STACK_SIZE][2];
/*************************************
*
* Now begin the process of growing
* regions.
*
**************************************/
g_label = 2;
object_found = 0;
first_call = 1;
for(i=0; i<ROWS; i++){
for(j=0; j<COLS; j++){
stack_file_in_use = 0;
stack_empty = 1;
pointer = -1;
/**********************************
*
* Search for the first pixel of
* a region.
*
***********************************/
if(binary[i][j] == value){
label_and_check_neighbor(binary, stack,
g_label, &stack_empty, &pointer,
i, j, value, &stack_file_in_use,
&first_call);
object_found = 1;
} /* ends if binary[i]j] == value */
/*****************************
*
* If the stack is not empty,
* pop the coordinates of
* the pixel off the stack
* and check its 8 neighbors.
*
*******************************/
while(stack_empty == 0){
pop_i = stack[pointer][0]; /* POP */
pop_j = stack[pointer][1]; /* OPERATION */
--pointer;
if(pointer <= 0){
if(stack_file_in_use){
pop_data_off_of_stack_file(
stack,
&pointer,
&stack_file_in_use);
} /* ends if stack_file_in_use */
else{
pointer = 0;
stack_empty = 1;
} /* ends else stack file is
not in use */
} /* ends if point <= 0 */
label_and_check_neighbor(binary,
stack, g_label,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -