/* equal_probability.c ** this program is used to normalize ** pixel value of input images for textural ** feature test, so as to avoid the unequal ** brightness and contrast ** from:IEEE Trans. on systems, man, and ** cybernetics. vol. SMC-3, no.6, nov. 1973 ** pp. 611-621. */ #include #include #include #define gray_level 256 void main(void) { FILE *fin, *fout; unsigned int num_pix,gray[gray_level]; unsigned int num_lev,gray_value,start,sum; unsigned int count1, count2,temp_num; double probability; unsigned char store[2],*look_up,in_file[50]; printf("Please input the total number of image pixels:"); scanf("%d",&num_pix); printf("Please input the gray levels of input image:"); scanf("%d",&num_lev); look_up = (unsigned char *)calloc(num_lev+1,sizeof(unsigned char)); printf("Please input image file name:"); scanf("%s",in_file); if((fin = fopen(in_file,"rb")) == NULL) { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("Ouput image file name:"); scanf("%s",in_file); if((fout = fopen(in_file,"wb")) == NULL) { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } for(count1 = 0;count1 < gray_level;count1++) gray[count1] = 0; for(count1 = 0;count1 < num_pix; count1++) { fread(store,1,1,fin); gray_value = store[0]; gray[gray_value]++; } fseek(fin,0,0); start = 0; sum = 0; count1 = 1; count2 = 0; temp_num = num_pix; while(temp_num != 0) { probability = (double) temp_num/(num_lev-count1+1); sum = 0; while(sum < probability) { sum = sum + gray[count2]; count2++; } look_up[count1] = count2 - 1; count1++; temp_num = temp_num - sum; } for(count1 = 0;count1 < num_pix; count1++) { count2 = 1; fread(store,1,1,fin); while(look_up[count2] < store[0]) { count2++; } store[0] = count2-1; fwrite(store,1,1,fout); } fclose(fin); fclose(fout); free(look_up); }