/* ch4_truth_test.c ** this program is used to generate test image ** and training file for fuzzy rule classification. ** This programme is used to support fuzzy_rule.c and ** fuzzy_rule_hierachy.c. ** COPYRIGHT: Brandt Tso */ #include #include #include #define num_of_training 400 void main(void) { FILE *fout,*fout1,*fout2; double s1, s2, f; unsigned int img_width, img_length,cate[5]; unsigned int count1, count2, i, j, clas; unsigned char in_file[50]; printf("Please input image row:"); scanf("%d",&img_length); printf("Please input image column:"); scanf("%d",&img_width); printf("Output the test image name:"); scanf("%s",in_file); if((fout = fopen(in_file,"wb")) == NULL)/*output test image*/ { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("Output the training file name (in floating points format):"); scanf("%s",in_file); if((fout1 = fopen(in_file,"w")) == NULL)/*output training image*/ { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("Output the test file name (in floating points format):"); scanf("%s",in_file); if((fout2 = fopen(in_file,"w")) == NULL)/*output test file*/ { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } i=0; for(count1 = 1; count1 <= img_length; count1++) { for(count2 = 1; count2 <= img_width; count2++) { s1 = (double) count1/img_length; /*row*/ s2 = (double) count2/img_width; /*column*/ fprintf(fout2,"# Input pattern %d:\n",i++); fprintf(fout2,"%lf %lf\n",s1, s2); f = (double) -0.2*sin(3.1415*2*s1)+s2-0.5; if (f >=0) { clas = 1; fprintf(fout,"%c",clas); } else { clas = 2; fprintf(fout,"%c",clas); } } } for(count1 = 1; count1 <= num_of_training; count1++) { i = (int) rand()%img_length; j = (int) rand()%img_width; s1 = (double) i/img_length; /*row*/ s2 = (double) j/img_width; /*column*/ f = (double) -0.2*sin(3.1415*2*s1)+s2-0.5; fprintf(fout1,"# Input pattern %d:\n",count1); fprintf(fout1,"%lf %lf\n",s1, s2); fprintf(fout1,"# output pattern %d:\n",count1); if(f >=0) { cate[1]=1; cate[2]=0; } else { cate[1]=0; cate[2]=1; } fprintf(fout1,"%d %d\n",cate[1], cate[2]); } printf("********************Done********************\n"); fclose(fout); fclose(fout1); fclose(fout2); }