📄 skeleton.c
字号:
* dilations to perform before doing one
* erosion.
*
*******************************************/
special_closing(the_image, out_image,
value, threshold, number,
rows, cols)
int number;
short **the_image,
**out_image,
threshold, value;
long cols, rows;
{
int a, b, count, i, j, k;
dilate_not_join(the_image, out_image,
value, threshold,
rows, cols);
if(number > 1){
count = 1;
while(count < number){
count++;
dilate_not_join(the_image, out_image,
value, threshold,
rows, cols);
} /* ends while */
} /* ends if number > 1 */
erosion(the_image, out_image,
value, threshold,
rows, cols);
} /* ends special_closing */
/*******************************************
*
* dilate_not_join(...
*
* Use a variation of the grass fire
* wave front approach.
*
* Raster scan the image left to right
* and examine and dilate the left edge pixels
* (a value to 0 transition). Process them
* normally and "save" the result. Next,
* raster scan the image right to left and
* save. Raster scan top to bottom and save.
* Raster scan bottom to top and save.
*
* That is one complete pass.
*
*******************************************/
dilate_not_join(the_image, out_image,
value, threshold,
rows, cols)
short **the_image,
**out_image,
threshold, value;
long cols, rows;
{
int a, b, count, i, j, k;
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
out_image[i][j] = the_image[i][j];
/***************************
*
* Scan left to right
* Look for value-0 transition
*
****************************/
printf("\n");
for(i=1; i<rows-1; i++){
if( (i%10) == 0) printf("%3d", i);
for(j=1; j<cols-1; j++){
if(the_image[i][j-1] == value &&
the_image[i][j] == 0){
count = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
if(the_image[i+a][j+b]==value)
count++;
} /* ends loop over b */
} /* ends loop over a */
if(count > threshold){
if(can_dilate(the_image,i,j,value)){
out_image[i][j] = value;
} /* ends if can_dilate */
} /* ends if count > threshold */
} /* ends if the_image == value */
} /* ends loop over j */
} /* ends loop over i */
/**************************************
*
* Copy the output back to the input.
*
**************************************/
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
the_image[i][j] = out_image[i][j];
/***************************
*
* Scan right to left
* Do this by scanning left
* to right and look for
* 0-value transition.
*
****************************/
printf("\n");
for(i=1; i<rows-1; i++){
if( (i%10) == 0) printf("%3d", i);
for(j=1; j<cols-1; j++){
if(the_image[i][j+1] == value &&
the_image[i][j] == 0){
count = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
if(the_image[i+a][j+b]==value)
count++;
} /* ends loop over b */
} /* ends loop over a */
if(count > threshold){
if(can_dilate(the_image,i,j,value)){
out_image[i][j] = value;
} /* ends if can_dilate */
} /* ends if count > threshold */
} /* ends if the_image == value */
} /* ends loop over j */
} /* ends loop over i */
/**************************************
*
* Copy the output back to the input.
*
**************************************/
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
the_image[i][j] = out_image[i][j];
/***************************
*
* Scan top to bottom
* Look for value-0 transition
*
****************************/
printf("\n");
for(j=1; j<cols-1; j++){
if( (j%10) == 0) printf("%3d", j);
for(i=1; i<rows-1; i++){
if(the_image[i-1][j] == value &&
the_image[i][j] == 0){
count = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
if(the_image[i+a][j+b]==value)
count++;
} /* ends loop over b */
} /* ends loop over a */
if(count > threshold){
if(can_dilate(the_image,i,j,value)){
out_image[i][j] = value;
} /* ends if can_dilate */
} /* ends if count > threshold */
} /* ends if the_image == value */
} /* ends loop over i */
} /* ends loop over j */
/**************************************
*
* Copy the output back to the input.
*
**************************************/
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
the_image[i][j] = out_image[i][j];
/***************************
*
* Scan bottom to top
* Do this by scanning top
* to bottom and look for
* 0-value transition.
*
****************************/
printf("\n");
for(j=1; j<cols-1; j++){
if( (j%10) == 0) printf("%3d", j);
for(i=1; i<rows-1; i++){
if(the_image[i+1][j] == value &&
the_image[i][j] == 0){
count = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
if(the_image[i+a][j+b]==value)
count++;
} /* ends loop over b */
} /* ends loop over a */
if(count > threshold){
if(can_dilate(the_image,i,j,value)){
out_image[i][j] = value;
} /* ends if can_dilate */
} /* ends if count > threshold */
} /* ends if the_image == value */
} /* ends loop over i */
} /* ends loop over j */
/**************************************
*
* Copy the output back to the input.
*
**************************************/
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
the_image[i][j] = out_image[i][j];
/****
fix_edges(out_image, 3, rows, cols);
*****/
} /* ends dilate_not_join */
/*******************************************
*
* can_dilate(...
*
* This function decides if you can dilate
* (set to value) a pixel without joining
* two separate objects in a 3x3 area.
*
* First, you grow regions inside the 3x3
* area. Next, check if the center pixel
* has neighbors with differing values.
* If it does, you cannot dilate it because
* that would join two separate objects.
*
*******************************************/
can_dilate(the_image, i, j, value)
int i, j;
short **the_image, value;
{
int a, b, c, d, count, found=0,
no_neighbor,
stack_pointer=-1,
stack_empty=1,
stack[12][2],
pop_a, pop_b,
one=1,
zero=0;
short first_value, label = 2, temp[3][3];
/**************************************
*
* Copy the center pixel and its
* neighbors to the temp array.
*
***************************************/
for(a=-1; a<2; a++)
for(b=-1; b<2; b++)
temp[a+1][b+1] = the_image[i+a][b+j];
/**************************************
*
* Grow objects inside the temp array.
*
***************************************/
for(a=0; a<3; a++){
for(b=0; b<3; b++){
stack_empty = 1;
stack_pointer = -1;
if(temp[a][b] == value){
little_label_and_check(temp, stack, label,
&stack_empty,
&stack_pointer,
a, b, value);
found = 1;
} /* ends if temp == value */
while(stack_empty == 0){
pop_a = stack[stack_pointer][0]; /* POP */
pop_b = stack[stack_pointer][1]; /* POP */
--stack_pointer;
if(stack_pointer <= 0){
stack_pointer = 0;
stack_empty = 1;
} /* ends if stack_pointer */
little_label_and_check(temp, stack, label,
&stack_empty,
&stack_pointer,
pop_a, pop_b, value);
} /* ends while stack_empty == 0 */
if(found){
found = 0;
label++;
} /* ends if object_found */
} /* ends loop over b */
} /* ends loop over a */
/**************************************
*
* Look at the center pixel. If it
* has two non-zero neigbors whose
* pixels are not the same, then
* you cannot dilate.
*
***************************************/
first_value = -1;
for(a=0; a<3; a++){
for(b=0; b<3; b++){
if(temp[a][b] != 0 &&
first_value == -1){
first_value = temp[a][b];
}
if(temp[a][b] != 0 &&
first_value != -1){
if(temp[a][b] != first_value){
return(zero);
}
}
} /* ends loop over b */
} /* ends loop over a */
return(one);
} /* ends can_dilate */
/*******************************************
*
* little_label_and_check(...
*
* This function labels the objects in
* in a 3x3 area.
*
*******************************************/
little_label_and_check(temp, stack, label, stack_empty,
stack_pointer, a, b, value)
int a, b, stack[12][2],
*stack_empty, *stack_pointer;
short temp[3][3], label, value;
{
int c, d;
temp[a][b] = label;
for(c=a-1; c<=a+1; c++){
for(d=b-1; d<=b+1; d++){
if(c >= 0 &&
c <= 2 &&
d >= 0 &&
d <= 2)
if(temp[c][d] == value){ /* PUSH */
*stack_pointer = *stack_pointer + 1;
stack[*stack_pointer][0] = c;
stack[*stack_pointer][1] = d;
*stack_empty = 0;
} /* ends if temp == value */
} /* ends loop over d */
} /* ends loop over c */
} /* ends little_label_and_check */
/*******************************************
*
* edm(..
*
* This function calculates the Euclidean
* distance measure for objects in an image.
* It calculates the distance from any
* pixel=value to the nearest zero pixel
*
*******************************************/
edm(the_image, out_image,
value, rows, cols)
short **the_image,
**out_image,
value;
long cols, rows;
{
int a, b, count, i, j, k;
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] = distance_8(the_image,
i, j,
value,
rows, cols);
} /* ends loop over j */
} /* ends loop over i */
} /* ends edm */
/*******************************************
*
* distance_8(..
*
* This function finds the distance from
* a pixel to the nearest zero pixel.
* It search in all eight directions.
*
*******************************************/
distance_8(the_image, a, b, value, rows, cols)
int a, b;
short **the_image, value;
long cols, rows;
{
int i, j, measuring;
short dist1 = 0,
dist2 = 0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -