/* s_texture_restore.c ** this program is used to restore noise-fading texture image ** based on the concept of Markovian Random Fields ** COPY RIGHT:BRANDT TSO ** */ #include #include #include #include #define win_size 3 #define centre 1 void main(void) { FILE *fin, *fout; long int img_row; long int img_col; long int count1, count2, count3, count4, count5, count6,count8,i; long int num_of_class; long int classname,mark,tot[10],tot1[10]; unsigned char **temp_img,**temp_img1, in_file[50]; double x,s; double beta[10],u_f1, mark1; printf("Please input the number of classes:"); scanf("%d",&num_of_class); printf("Please input the beta values (Markovian neighbourhood coefficients)!\n"); for(count1=0; count1<4; count1++) { printf("Please input beta value %d:",count1+1); scanf("%lf",&beta[count1]); } printf("Input image row:"); scanf("%d",&img_row); printf("Input image col:"); scanf("%d",&img_col); temp_img =(unsigned char **)calloc(img_row,sizeof(unsigned char *)); temp_img1 =(unsigned char **)calloc(img_row,sizeof(unsigned char *)); for(i = 0 ; i < img_row; i++) { temp_img[i] = (unsigned char *)calloc(img_col,sizeof(unsigned char)); temp_img1[i] = (unsigned char *)calloc(img_col,sizeof(unsigned char)); } printf("Input noise fadding image file name:"); scanf("%s",in_file); if((fin = fopen(in_file,"rb")) == NULL) /*intput image*/ { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("Output image file name:"); scanf("%s",in_file); if((fout = fopen(in_file,"wb")) == NULL) /*output image*/ { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } for(count5 = 0; count5 < img_row; count5++) { for(count6 = 0; count6 < img_col; count6++) { fscanf(fin,"%c",&temp_img[count5][count6]); temp_img1[count5][count6]= temp_img[count5][count6]; } } printf("Reading finished, Start calculating!!!\n"); tot[0]=0;tot[1]=0; tot[2]=0; tot[3]=0; tot1[0]=0;tot1[1]=0; tot1[2]=0; tot1[3]=0; tot[4]=0;tot[5]=0; tot[6]=0; tot[7]=0; tot1[4]=0;tot1[5]=0; tot1[6]=0; tot1[7]=0; for(count8=1; count8 < 50; count8++)/*num of iterations running under temperture T*/ { for(count5 = 1; count5 < img_row-1; count5++) { for(count6 = 1; count6 < img_col-1; count6++) { mark = temp_img[count5][count6]; tot[0]=0;tot[1]=0; tot[2]=0; tot[3]=0; tot1[0]=0;tot1[1]=0; tot1[2]=0; tot1[3]=0; tot[4]=0;tot[5]=0; tot[6]=0; tot[7]=0; tot1[4]=0;tot1[5]=0; tot1[6]=0; tot1[7]=0; for(count3 = 0; count3 < win_size; count3++) { for(count4 = 0; count4 < win_size; count4++) { if((count3==centre)&&(count4 == centre)) count4++; classname = temp_img[count5-centre+count3][count6-centre+count4]; /*if((count6 == 131) && (count5==111)) printf(" (%d)", classname); */ if((count3==0 && count4==0) || (count3==2 && count4==2)) tot1[2]= (long) beta[2]*(mark-classname)+tot1[2];//(mark-classname); if((count3==0 && count4==1) || (count3==2 && count4==1)) tot1[1]=(long) beta[1]*(mark-classname)+tot1[1]; if((count3==0 && count4==2) || (count3==2 && count4==0)) tot1[3]=(long) beta[3]*(mark-classname)+tot1[3]; if((count3==1 && count4==0) || (count3==1 && count4==2)) tot1[0]=(long) beta[0]*(mark-classname)+tot1[0]; } } u_f1= (double) 10*(tot1[0]+tot1[1]+tot1[2]+tot1[3]); /* if((count6 == 131) && (count5==111)) printf(" (tot = %d)", tot1[0]+tot1[1]);*/ tot1[0]=0; tot1[1]=0; tot1[2]=0; tot1[3]=0;tot1[4]=0; tot1[5]=0; tot1[6]=0; tot1[7]=0; x = 1.0; s = (double) temp_img1[count5][count6]-mark; mark1= mark-0.01*(-2*s+u_f1); /*if((count6 == 131) && (count5==111)) printf(" (mark=%d), (s=%lf, mark1=%lf, uf1=%lf)",mark, s, mark1,u_f1);*/ temp_img[count5][count6]= (unsigned char) mark1; }/*end of count6*/ }/*end of count5*/ printf("Iteration %d\n", count8); }/*end of count8 for number iteratin running under temper T*/ for(count1 = 1;count1 < img_row-1;count1++) { for(count2 = 1;count2 < img_col-1;count2++) { fprintf(fout,"%c",temp_img[count1][count2]); } } printf("\nDone!!\n"); for(i = 0 ; i < img_row; i++) { free(temp_img[i]); } free(temp_img); free(temp_img1); fclose(fout); fclose(fin); }