/* fuzzy_rule.c ** this program classify the two dimension ** floating (normalised from 0 to 1) image file ** using fuzzy rules. ** The training file and test file can be generated by using ** the fuzzy_train_test.c program. ** COPYRIGHT: Brandt Tso */ #include #include #include #define num_of_clas 2 void main(void) { FILE *fin1,*fin2, *fout; double s1,s2,u1,u2, ai[10],beta[10],beta_avg,beta_all,**w; double temp[10], max; double alpha[10],alpha_max[10]; unsigned int img_length,**c; unsigned int img_width, k,win; unsigned int num_of_train, total_pat,clas[10]; unsigned int count1, count2, count3, i, j; unsigned char *store, in_file[50], a[15],b[15],e[15],d[15]; printf("Please input image row:"); scanf("%d",&img_length); printf("Please input image column:"); scanf("%d",&img_width); printf("Please input the total number of training patterns:"); scanf("%d",&num_of_train); printf("Please input the maximal number of partitions over each anxi:"); scanf("%d",&k); store = (unsigned char *)calloc(24,sizeof(unsigned char)); c = (unsigned int **)calloc(2*k,sizeof(unsigned int *)); w = (double **)calloc(2*k,sizeof(double *)); for(i=0;i<=2*k;i++) { c[i] = (unsigned int *)calloc(2*k,sizeof(unsigned int)); w[i] = (double *)calloc(2*k,sizeof(double)); } printf("Please input training file name (in floating points format):"); scanf("%s",in_file); if((fin1 = fopen(in_file,"r")) == NULL)/*for training*/ { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("Please input file name to be classified(in floating points format):"); scanf("%s",in_file); if((fin2 = fopen(in_file,"r")) == NULL)/*for clasification*/ { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("Output classified image file name:"); scanf("%s",in_file); if((fout = fopen(in_file,"wb")) == NULL)/*output clasified image*/ { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("\nStart training process!\n"); for(i=1; i<=k; i++) for(j=1; j<=k; j++) { beta[1] = 0; beta[2]=0;/*set initial value for beta x*/ ai[1]=(double) (i-1)/(k-1); ai[2]=(double) (j-1)/(k-1); fseek(fin1,0,0); for(count1=0; count1 < num_of_train; count1++) { fscanf(fin1,"%s %s %s %s",a,b,e,d); fscanf(fin1,"%lf %lf",&s1,&s2); /*printf(" %f %f ",s1, s2); */ fscanf(fin1,"%s %s %s %s",a,b,e,d); fscanf(fin1,"%d %d",&clas[1],&clas[2]);/*printf(" %d %d \n",clas[1], clas[2]);*/ temp[1]=s1-ai[1]; temp[2]=s2-ai[2]; for(count3=1; count3 <=num_of_clas; count3++) { if(temp[count3] < 0) temp[count3] = -temp[count3]; } u1=(double) 1-(temp[1]*(k-1)); if(u1 < 0) u1 = 0; u2=(double) 1-(temp[2]*(k-1)); if(u2 < 0) u2 = 0; if(clas[1] != 0) beta[1] = beta[1] + u1*u2; else beta[2] = beta[2] + u1*u2;/*END procedure 1*/ } /* printf("beta %lf %lf\n",beta[1],beta[2]);*/ max=0; for(count1=1; count1 <= num_of_clas; count1++) { if(beta[count1]>max) { max = beta[count1]; win=count1; } } /*END procedure 2*/ c[i][j]=win; beta_avg = 0; beta_all = 0; for(count1=1; count1 <= num_of_clas; count1++) { if(count1 != win) beta_avg = beta_avg + beta[count1]; beta_all = beta_all + beta[count1]; } beta_avg = (double) beta_avg/(num_of_clas-1); if(beta_all == 0) w[i][j]=0; else w[i][j]=(double) (beta[win]-beta_avg)/beta_all; /*printf("%d %lf \n",c[i][j],w[i][j]);*/ } /*END procedure 3*/ printf("Training is completed! Start Classification!\n"); total_pat = img_length*img_width; for(count1=0; count1 < total_pat; count1++)/*start clasification*/ { fscanf(fin2,"%s %s %s %s",a,b,e,d); fscanf(fin2,"%lf %lf",&s1,&s2); alpha_max[1]=0; alpha_max[2]=0; for(count2=1; count2<=num_of_clas; count2++) { for(i=1; i<=k; i++) for(j=1; j<=k; j++) { ai[1]=(double) (i-1)/(k-1); ai[2]=(double) (j-1)/(k-1); temp[1]=s1-ai[1]; temp[2]=s2-ai[2]; for(count3=1; count3 <=num_of_clas; count3++) { if(temp[count3] < 0) temp[count3] = -temp[count3]; } u1=(double) 1-(temp[1]*(k-1)); if(u1 < 0) u1 = 0; u2=(double) 1-(temp[2]*(k-1)); if(u2 < 0) u2 = 0; if(c[i][j] == count2) { alpha[count2]=u1*u2*w[i][j]; if (alpha[count2] > alpha_max[count2]) alpha_max[count2]=alpha[count2]; } }/*End of i,j*/ } max=0; for(count2=1; count2<=num_of_clas; count2++) { if(alpha_max[count2]>max) { max = alpha_max[count2]; win = count2; } } if(alpha_max[win] != 0) fprintf(fout,"%c",win); else { win=0; fprintf(fout,"%c",win); } }/*end of total pattern clasification*/ printf("********************Done******************\n"); fclose(fin1); fclose(fin2); fclose(fout); /* for(i=0;i<=2*k;i++) { free(c[i]); free(w[i]); } free(c); free(w);*/ free(store); }