📄 cips7.c
字号:
char in_name[], out_name[];
int il, ie, ll, le;
short the_image[ROWS][COLS],
out_image[ROWS][COLS],
value;
{
int a, b, count, i, j, k,
length, width;
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
out_image[i][j] = 0;
/***************************
*
* Loop over image array
*
****************************/
printf("\n");
for(i=0; i<ROWS; i++){
if( (i%10) == 0) printf("%3d", i);
for(j=0; j<COLS; j++){
if(the_image[i][j] == value)
out_image[i][j] = mat_d(the_image,
i, j, value);
} /* ends loop over j */
} /* ends loop over i */
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends mat */
/*******************************************
*
* mat_d(..
*
* This function helps find the medial
* axis transform.
*
* This function measures the distances
* from the point to a zero pixel in all
* eight directions. Look for the two
* shortest distances in the eight distances.
* If the two shortest distances are
* equal, then the point in question
* is minimally distant to more than one
* boundary point. Therefore, it is
* on the medial axis so return a value.
* Otherwise, return zero.
*
*******************************************/
mat_d(the_image, a, b, value)
int a, b;
short the_image[ROWS][COLS], value;
{
int i, j, measuring;
short dist1 = 0,
dist2 = 0,
dist3 = 0,
dist4 = 0,
dist5 = 0,
dist6 = 0,
dist7 = 0,
dist8 = 0,
min1 = 255,
min2 = 255,
result = 0;
/* straight up */
measuring = 1;
i = a;
j = b;
while(measuring){
i--;
if(i >= 0){
if(the_image[i][j] == value)
dist1++;
else
measuring = 0;
}
else
measuring = 0;
} /* ends while measuring */
result = dist1;
min1 = dist1;
/* straight down */
measuring = 1;
i = a;
j = b;
while(measuring){
i++;
if(i <= ROWS-1){
if(the_image[i][j] == value)
dist2++;
else
measuring = 0;
}
else
measuring = 0;
} /* ends while measuring */
if(dist2 <= result)
result = dist2;
if(dist2 < min1){
min2 = min1;
min1 = dist2;
}
else
if(dist2 < min2)
min2 = dist2;
/* straight left */
measuring = 1;
i = a;
j = b;
while(measuring){
j--;
if(j >= 0){
if(the_image[i][j] == value)
dist3++;
else
measuring = 0;
}
else
measuring = 0;
} /* ends while measuring */
if(dist3 <= result)
result = dist3;
if(dist3 < min1){
min2 = min1;
min1 = dist3;
}
else
if(dist3 < min2)
min2 = dist3;
/* straight right */
measuring = 1;
i = a;
j = b;
while(measuring){
j++;
if(j <= COLS-1){
if(the_image[i][j] == value)
dist4++;
else
measuring = 0;
}
else
measuring = 0;
} /* ends while measuring */
if(dist4 <= result)
result = dist4;
if(dist4 < min1){
min2 = min1;
min1 = dist4;
}
else
if(dist4 < min2)
min2 = dist4;
/* left and up */
measuring = 1;
i = a;
j = b;
while(measuring){
j--;
i--;
if(j >= 0 && i>=0){
if(the_image[i][j] == value)
dist5++;
else
measuring = 0;
}
else
measuring = 0;
} /* ends while measuring */
dist5 = ((dist5*14)+7)/10;
if(dist5 <= result)
result = dist5;
if(dist5 < min1){
min2 = min1;
min1 = dist5;
}
else
if(dist5 < min2)
min2 = dist5;
/* right and up */
measuring = 1;
i = a;
j = b;
while(measuring){
j++;
i--;
if(j <=COLS-1 && i>=0){
if(the_image[i][j] == value)
dist6++;
else
measuring = 0;
}
else
measuring = 0;
} /* ends while measuring */
dist6 = ((dist6*14)+7)/10;
if(dist6 <= result)
result = dist6;
if(dist6 < min1){
min2 = min1;
min1 = dist6;
}
else
if(dist6 < min2)
min2 = dist6;
/* right and down */
measuring = 1;
i = a;
j = b;
while(measuring){
j++;
i++;
if(j <=COLS-1 && i<=ROWS-1){
if(the_image[i][j] == value)
dist7++;
else
measuring = 0;
}
else
measuring = 0;
} /* ends while measuring */
dist7 = ((dist7*14)+7)/10;
if(dist7 <= result)
result = dist7;
if(dist7 < min1){
min2 = min1;
min1 = dist7;
}
else
if(dist7 < min2)
min2 = dist7;
/* left and down */
measuring = 1;
i = a;
j = b;
while(measuring){
j--;
i++;
if(j >=0 && i<=ROWS-1){
if(the_image[i][j] == value)
dist8++;
else
measuring = 0;
}
else
measuring = 0;
} /* ends while measuring */
dist8 = ((dist8*14)+7)/10;
if(dist8 <= result)
result = dist8;
if(dist8 < min1){
min2 = min1;
min1 = dist8;
}
else
if(dist8 < min2)
min2 = dist8;
if(min1 == min2)
result = value;
else
result = 0;
if(min1 == 0)
result = 0;
return(result);
} /* ends mat_d */
/*********************************************
*
* 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
*
************************************************/
/**************************************************
*
* 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 */
/***********************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -