/* fuzzy_rule1.c ** this program clasify the two dimension ** (normalised from 0 to 1) image file ** using hierarchical fuzzy rule base!!! ** the training file and test file can be generated ** using fuzzy_truth_train.c ** COPYRIGHT: Brandt Tso */ #include #include #include #define num_of_clas 2 #define partition_max 100 void main(void) { FILE *fin1,*fin2, *fout; double s1,s2,u1,u2, ai[10],beta[10],beta_avg,beta_all,**w[partition_max]; double temp[10], max; double alpha[10],alpha_max[10]; unsigned int img_length,**c[partition_max]; unsigned int img_width, k,win; unsigned int num_of_train, total_pat,clas[10]; unsigned int count1, count2, count3, i, j,l; 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",&l); store = (unsigned char *)calloc(24,sizeof(unsigned char)); for(i=0;i<=2*l;i++) { c[i] = (unsigned int **)calloc(2*l,sizeof(unsigned int *)); w[i] = (double **)calloc(2*l,sizeof(double *)); } for(i=0;i<=2*l;i++) for(j=0;j<=2*l;j++) { c[i][j] = (unsigned int *)calloc(2*l,sizeof(unsigned int)); w[i][j] = (double *)calloc(2*l,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("\nStarting training process!\n"); for(k=3; k<=l; k++) 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 %f %f\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[k][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[k][i][j]=0; else w[k][i][j]=(double) (beta[win]-beta_avg)/beta_all; /* printf("%d %f \n",c[k][i][j],w[k][i][j]);*/ } /*END procedure 3*/ printf("Training finished! 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(k=3; k<=l; k++) 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[k][i][j] == count2) { alpha[count2]=u1*u2*w[k][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 classification*/ printf("************************Done*********************\n"); /* for(i=0;i<=2*l;i++) for(j=0;j<=2*l;j++) { free(c[i][j]); free(w[i][j]); } for(i=0;i<=2*l;i++) { free(c[i]); free(w[i]); } free(store); */ fclose(fin1); fclose(fin2); fclose(fout); }