/* s_MAP_texture.c ** This program is used to generate texture using ** Maximise A Posterior - MAP. ** COPYRIGHT:Brandt Tso */ #include #include #include #define win_size 3 void main(void) { FILE *fout; int img_row; int img_col; int count[10],i; int num_of_class,mechanism; int classname,classname1,mark,centre,tot[10],tot1[10],perturb_z; unsigned char **temp_img,in_file[50]; double normal_threshold; double beta[10],delta,u_f,u_f1,T,alfa; printf("Please input the total number of texture classes:"); scanf("%d",&num_of_class); printf("Please input the values for neighbourhood coefficients (beta)\n"); for(count[1]=0;count[1]<4;count[1]++) { printf("beta %d = ",count[1]+1); scanf("%lf",&beta[count[1]]); } T=1; centre = 1; printf("Please choose texture forming mechanism\n"); printf("(0-->using '-beta' when wr is not equal to wr')\n"); printf("(1-->using 0 instead)\n"); scanf("%d",&mechanism); printf("Please input image row:"); scanf("%d",&img_row); img_row = img_row+2; printf("Please input image column:"); scanf("%d",&img_col); img_col = img_col+2; temp_img =(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)); } printf("Output texture image file:"); 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); } printf("Initialising Image\n"); for(count[5] = 0; count[5] < img_row; count[5]++) { for(count[6] = 0; count[6] < img_col; count[6]++) { classname1= (int) rand()%num_of_class+1; if(classname1 < 0) classname1=-classname1; temp_img[count[5]][count[6]] = classname1;/*class nnumber start from 0 to num_of_class-1*/ /*if( temp_img[count[5]][count[6]]>=3) printf(" %d ", temp_img[count[5]][count[6]]);*/ } } 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(count[7]=1; count[7] < 100; count[7]++)/*num of iterations running under temperture T*/ { for(count[5] = 1; count[5] < img_row-1; count[5]++) { for(count[6] = 1; count[6] < img_col-1; count[6]++) { mark = temp_img[count[5]][count[6]];/*find xt*/ perturb_z = (int) rand()%num_of_class+1; if(perturb_z<0) perturb_z = -perturb_z; for(count[3] = 0; count[3] < win_size; count[3]++) { for(count[4] = 0; count[4] < win_size; count[4]++) { if((count[3]==centre)&&(count[4] == centre)) count[4]++; classname = temp_img[count[5]-centre+count[3]][count[6]-centre+count[4]]; if(perturb_z==classname) { if((count[3]==0 && count[4]==0) || (count[3]==2 && count[4]==2)) tot[2]++; if((count[3]==0 && count[4]==1) || (count[3]==2 && count[4]==1)) tot[1]++; if((count[3]==0 && count[4]==2) || (count[3]==2 && count[4]==0)) tot[3]++; if((count[3]==1 && count[4]==0) || (count[3]==1 && count[4]==2)) tot[0]++; } else if(perturb_z!=classname) { if((count[3]==0 && count[4]==0) || (count[3]==2 && count[4]==2)) tot[6]++; if((count[3]==0 && count[4]==1) || (count[3]==2 && count[4]==1)) tot[5]++; if((count[3]==0 && count[4]==2) || (count[3]==2 && count[4]==0)) tot[7]++; if((count[3]==1 && count[4]==0) || (count[3]==1 && count[4]==2)) tot[4]++; } if(mark==classname) { if((count[3]==0 && count[4]==0) || (count[3]==2 && count[4]==2)) tot1[2]++; if((count[3]==0 && count[4]==1) || (count[3]==2 && count[4]==1)) tot1[1]++; if((count[3]==0 && count[4]==2) || (count[3]==2 && count[4]==0)) tot1[3]++; if((count[3]==1 && count[4]==0) || (count[3]==1 && count[4]==2)) tot1[0]++; } else if(mark!=classname) { if((count[3]==0 && count[4]==0) || (count[3]==2 && count[4]==2)) tot1[6]++; if((count[3]==0 && count[4]==1) || (count[3]==2 && count[4]==1)) tot1[5]++; if((count[3]==0 && count[4]==2) || (count[3]==2 && count[4]==0)) tot1[7]++; if((count[3]==1 && count[4]==0) || (count[3]==1 && count[4]==2)) tot1[4]++; } } } if(mechanism == 0) { u_f = beta[0]*tot[0]+beta[1]*tot[1]+beta[2]*tot[2]+beta[3]*tot[3] -beta[0]*tot[4]-beta[1]*tot[5]-beta[2]*tot[6]-beta[3]*tot[7]; u_f1= tot1[0]*beta[0]+beta[1]*tot1[1]+beta[2]*tot1[2]+beta[3]*tot1[3] -tot1[4]*beta[0]-beta[1]*tot1[5]-beta[2]*tot1[6]-beta[3]*tot1[7]; tot[0]=0; tot[1]=0; tot[2]=0; tot[3]=0;tot[4]=0; tot[5]=0; tot[6]=0; tot[7]=0; delta = u_f - u_f1; tot1[0]=0; tot1[1]=0; tot1[2]=0; tot1[3]=0;tot1[4]=0; tot1[5]=0; tot1[6]=0; tot1[7]=0; } else { u_f = beta[0]*tot[0]+beta[1]*tot[1]+beta[2]*tot[2]+beta[3]*tot[3]; tot[0]=0; tot[1]=0; tot[2]=0; tot[3]=0; delta = u_f - (tot1[0]*beta[0]+beta[1]*tot1[1]+beta[2]*tot1[2]+beta[3]*tot1[3]); tot1[0]=0; tot1[1]=0; tot1[2]=0; tot1[3]=0; } if(delta >= 0) temp_img[count[5]][count[6]] = perturb_z; else { alfa = (double) delta/T; alfa = exp(alfa); normal_threshold = rand(); if(normal_threshold<0) normal_threshold=-normal_threshold; while (normal_threshold > 1) normal_threshold =normal_threshold/10; if(alfa > normal_threshold) temp_img[count[5]][count[6]] = perturb_z; } }/*end of count[6]*/ }/*end of count[5]*/ printf("Iteration %d\n", count[7]); }/*end of count[7] for number iteratin running under temper T*/ for(count[1] = 1;count[1] < img_row-1;count[1]++) { for(count[2] = 1;count[2] < img_col-1;count[2]++) { fprintf(fout,"%c",temp_img[count[1]][count[2]]); } } printf("\nDone!!\n"); for(i = 0 ; i < img_row; i++) { free(temp_img[i]); } free(temp_img); fclose(fout); /*fclose(fout1);*/ }