/* fix_th.c */ #include #include #include "mypgm.h" #define BLACK 0 #define WHITE 255 void fixed_threshold() /* binarization by fixed threshold */ /* input image1[y][x] ===> output image2[y][x] */ { int min, max; /* maximum & minimum graylevels */ int mean; /* mean value of graylevels */ double sum; int x, y; /* Loop variables */ int threshold; /* threshold value for binarization */ /* detection of maximum, minimum & mean values of gray levels */ min = max = image1[0][0]; sum = 0.0; for (y = 0; y < y_size1; y++) { for (x = 0; x < x_size1; x++) { sum += image1[y][x]; if (image1[y][x] < min) min = image1[y][x]; if (image1[y][x] > max) max = image1[y][x]; } } mean = (int)(sum / (x_size1 * y_size1)); printf("Minimum graylevel = %d --- Maximum graylevel = %d\n", min, max); printf("Mean graylevel = %d\n", mean); printf("\nEnter threshold value of binarization = "); scanf("%d", &threshold); /* result of binarization based on fixed threshold stored into image2[y][x] */ x_size2 = x_size1; y_size2 = y_size1; for (y = 0; y < y_size1; y++) { for (x = 0; x < x_size1; x++) { if (image1[y][x] > threshold) { image2[y][x] = WHITE; } else { image2[y][x] = BLACK; } } } } main() { load_image_data(); /* input image1 */ fixed_threshold(); /* binarization by fixed threshold */ save_image_data(); /* output image2 */ return 0; }