/* eql_hst.c */ #include #include #include "mypgm.h" #define FINAL_LEVEL 64 /* No. of final gray levels */ void equal_histogram( ) /* Input: image1 -------- Output: image2 */ { int hist1[GRAYLEVEL], hist2[GRAYLEVEL]; int trans_table[GRAYLEVEL]; /* Table of gray level transformation */ int i, x, y; /* Loop variable */ int target_value; /* Target occurrences after transformation */ int gray; double gray_step; /* Descriptive gray level interval */ int min, max; printf("Histogram equalization is now processing.\n"); /* Generation of gray level histogram of input image */ for (i = 0; i < GRAYLEVEL; i++) hist1[i] = 0; for (y = 0; y < y_size1; y++) { for (x = 0; x < x_size1; x++) { hist1[image1[y][x]]++; } } /* Generation of transformation table */ target_value = (int)((double)(x_size1 * y_size1) / FINAL_LEVEL); for (i = 0; i < FINAL_LEVEL; i++) hist2[i] = 0; gray = 0; for (i = 0; i < GRAYLEVEL; i++) { if (abs(target_value - hist2[gray]) < abs(target_value - (hist2[gray] + hist1[i]))) { gray++; if (gray >= FINAL_LEVEL) gray = FINAL_LEVEL - 1; } trans_table[i] = gray; hist2[gray] = hist2[gray] + hist1[i]; } /* Output of image2 subject to histogram equalization */ x_size2 = x_size1; y_size2 = y_size1; gray_step = (double)GRAYLEVEL / FINAL_LEVEL; for (y = 0; y < y_size2; y++) { for (x = 0; x < x_size2; x++) { image2[y][x] = (unsigned char) (trans_table[image1[y][x]] * gray_step); } } /* linear transformation of gray level histogram of output image */ max = min = image2[0][0]; for (y = 0; y < y_size2; y++) { for (x = 0; x < x_size2; x++) { if (min > image2[y][x]) min = image2[y][x]; if (max < image2[y][x]) max = image2[y][x]; } } if ((max - min) == 0) { printf("Nothing exists!!!\n"); exit(1); } for (y = 0; y < y_size2; y++) { for (x = 0; x < x_size2; x++) { image2[y][x] = (unsigned char)((double)(image2[y][x] - min) / (max - min) * MAX_BRIGHTNESS); } } } main( ) { load_image_data( ); /* Input of image1 */ equal_histogram( ); /* Histogram equalization is applied to image1 */ save_image_data( ); /* Output of image2*/ return 0; }