/* boundary4.c */ #include #include #include"mypgm.h" #define WHITE 255 #define BLACK 0 #define GRAY 128 void erode8( ) /* 8-neighbor erosion of binary image */ /* input image1[y][x] ===> output image2[y][x] */ { int counter; /* number of neighboring figure points */ int p_x, p_y; /* coordinates of neighboring points */ int x, y, m, n; /* loop variable */ x_size2 = x_size1; y_size2 = y_size1; for (y = 0; y < y_size2; y++) for (x = 0; x < x_size2; x++) image2[y][x] = image1[y][x]; for (y = 0; y < y_size1; y++) { for (x = 0; x < x_size1; x++) { if (image1[y][x] == BLACK) { counter = 0; for (m = -1; m < 2; m++) { for (n = -1; n < 2; n++) { p_x = x + n; p_y = y + m; if (p_x >= 0 && p_x < x_size1 && p_y >= 0 && p_y < y_size1 && image1[p_y][p_x] == WHITE) counter++; } } if (counter > 0) image2[y][x] = WHITE; } } } } void boundary4( ) /* 4-connectivity boundary extraction of binary image */ /* input image1[y][x] ===> output image2[y][x] */ { int x, y; /* Loop variable */ x_size2 = x_size1; y_size2 = y_size1; printf("Let's start 4-connectivity boundary extraction.\n"); printf("\n"); erode8(); /* 8-neighbor erosion */ /* boundary extraction by subtracting image2 from image1 */ for (y = 0; y < y_size2; y++) for (x = 0; x < x_size2; x++) if(image1[y][x] == BLACK) if (image2[y][x] == WHITE) image2[y][x] = BLACK; else image2[y][x] = GRAY; } main( ) { load_image_data( ); /* Input image1 */ boundary4( ); /* 4-connectivity boundary extraction */ save_image_data( ); /* Output image2 */ return 0; }