📄 new_neighbor.c
字号:
fprintf(stderr, "\nERROR: Input file %s cannot be opened\n\n", tfile); exit(2); } point = 0; while ( (fscanf(ftest, "%f", &a_point)) != EOF){ t_points[point] = a_point; point++; } fprintf(stdout, "Matching probe: %s ", line); fval = FLT_MAX; for (i=0; i < no_kfiles; i++){ if (do_bayesian) dist = bayesian_distance(t_points, kfaces[i].coeffs, total_no_points, &B_struct); else dist = find_distance(t_points+a, kfaces[i].coeffs+a, no_points); if (dist < fval) fval = dist; /* Create a match */ a_match = (Matchptr) calloc(1, sizeof(Match)); a_match->name = strdup(kfaces[i].name); a_match->distance = dist; a_match->next = NULL; /* Add to the list of matches of that particular face */ faces[file_no].similar = add_match(faces[file_no].similar, a_match, n); } fprintf(stdout, "\tD_min = %+1.6e \n", fval); fclose(ftest); /* Begin Hack for FERET test sept 16 1996 */ count = print_file(faces[file_no], n, out_file, thres, count); kill_list(faces[file_no].similar); /* End Hack for FERET test sept 16 1996 */ file_no++; } /* if ( (strncmp(line........... */ } /* while ( (fscanf(ftlist........ */ fclose(ftlist); /* Begin Hack for FERET test sept 16 1996 */ fprintf(stdout, "%f\n", (float) count/no_files); fprintf(stdout, "\n\nThe recognition rate is %f (%d/%d)\n", (float) count/no_files, count, no_files); /* Begin Hack for FERET test sept 16 1996 */ /* print_match(faces, n, no_files, out_file, thres, comline); */} /* Main */ float find_distance(float *test, float *known, int no){ int i; float sum=0; for (i=0; i<no; i++){ sum += ( (test[i] - known[i]) * (test[i]-known[i])); } return (sqrt(sum));}float bayesian_distance(float *test, float *known, int N, Bayesian *B){ register int i,j,k; static float C0[MAX_NDIM]; static float C1[MAX_NDIM]; static float x[MAX_NDIM]; static float y[MAX_NDIM]; float f0,f1; float L0,L1; /* compute the difference vector (my arrays start with 1) */ for (i=0; i<N; i++) x[i+1] = test[i] - known[i]; /* project onto intra space */ for (i=1; i<=N; i++) y[i] = x[i] - B->intra_kl[1][i]; for (j=B->a; j<=B->b; j++) { f0 = 0; for (i=1; i<=N; i++) f0 += (y[i] * B->intra_kl[1+j][i]); C0[j] = f0; } /* project onto extra space */ for (i=1; i<=N; i++) y[i] = x[i] - B->extra_kl[1][i]; for (j=B->a; j<=B->b; j++) { f1 = 0; for (i=1; i<=N; i++) f1 += (y[i] * B->extra_kl[1+j][i]); C1[j] = f1; } /* compute intra log-likelihood */ f0 = 0; for (i=B->a; i<=B->b; i++) f0 += (C0[i]*C0[i]/(B->intra_kl[B->N+2][i])); L0 = -0.5 * f0 - B->logD0; /* compute extra log-likelihood */ f1 = 0; for (i=B->a; i<=B->b; i++) f1 += (C1[i]*C1[i]/(B->extra_kl[B->N+2][i])); L1 = -0.5 * f1 - B->logD1; /* return NEGATIVE log-ratio as a distance */ return (L1 - L0 + B->logP1 - B->logP0);}Matchptr add_match(Matchptr root, Matchptr new, int n){ Matchptr temp, prev; int count = 0; if (root==NULL) return new; else { if (root->distance > new->distance){ new->next = root; return new; } } temp = root; prev = root; while ( ((temp != NULL) && (count != n)) && (temp->distance <= new->distance)){ prev = temp; temp = temp->next; count++; } new->next = temp; prev->next = new; return root;}/*char *strdup(char *s) { char *p = (char*)malloc(strlen(s) + 1); if (p==NULL) { fprintf(stderr, "Error: Memory cannot be allocated"); exit (5); } return(strcpy(p, s)); } */void print_match(Tface *faces, int n, int no_files, char out[], float thres, char comline[]){ /* What this function does is print out the results of the matching stage */ /* For each probe in Tfaces, it fines the neighbors from the linked list */ /* and prints out the results in a file and in a directory with a file */ /* hte same name as the probe */ int i, j,k, count, l, rec=0; float d, e; Matchptr temp; FILE *fp, *f_file; char test[MAX_CHARS], known[MAX_CHARS], outfile[MAX_CHARS]; sprintf(outfile,"%s%s", out, ".out"); if ( (fp = fopen(outfile, "w")) == NULL){ fprintf(stderr, "\nERROR: Input file %s cannot be opened\n\n", out); exit(2); } fprintf(stderr, "Command line : %s \n\n", comline); fprintf(fp, "Command Line: %s\n\n", comline); fprintf(fp, "<Probe number> <Match order> <Gallery Match Number> <Found Flag> <Confidence>\n"); /* for each probe, we get the matches from the linked list and then print out the results in a out file as well as in a file in the directory with the same name as the probe. */ /* For each probe */ for (i=0; i<no_files; i++){ temp = faces[i].similar; count = 1; fprintf(fp, "\n\n"); /* Extract the numerical part of the file name */ l = strlen(faces[i].name); for (k=0; k<l ; k++){ if ( faces[i].name[k] >= '0' && faces[i].name[k] <= '9'){ test[k] = faces[i].name[k]; } else break; } test[k]='\0'; /* open the file in the directory with the same name */ strcpy(outfile, ""); sprintf(outfile,"%s/%s.mit", out, test); if ( (f_file = fopen(outfile,"w")) == NULL){ fprintf(stderr, "\n ERROR: Input file %s cannot be opened\n", outfile); exit(2); } if (n==0){ while (temp != NULL){ l = strlen(temp->name); for (k=0; k<l; k++){ if ( temp->name[k] >= '0' && temp->name[k] <= '9'){ known[k] = temp->name[k]; } else break; } known[k] = '\0'; if (count ==1){ d = temp->distance; if ( strcmp(test, known)==0){ rec++; } } if (do_bayesian) e = 100.0/(1 + exp(sigmoid_scale * temp->distance)); else e = 100*log(conf_factor)/log(temp->distance + conf_factor);; fprintf(fp, "%4i %3i %4i %1d %3.2f \n", atoi(test), count, atoi(known),((e > thres) ? 1 : 0), e); fprintf(f_file, "%4d\t%4d\t%f\n", count, atoi(known), e); temp = temp->next; count++; } } else { for (j=0; j<n; j++){ l = strlen(temp->name); for (k=0; k<l; k++){ if (temp->name[k] >= '0' && temp->name[k] <= '9') known[k] = temp->name[k]; else break; } known[k] = '\0'; if (count == 1) { d = temp->distance; if ( strcmp(test, known)== 0){ rec++; } } if (do_bayesian) e = 100.0/(1 + exp(sigmoid_scale * temp->distance)); else e = 100*log(conf_factor)/log(temp->distance + conf_factor);; fprintf(fp, "%4i %3i %4i %1d %3.2f \n", atoi(test), count, atoi(known),((e > thres) ? 1 : 0), e); fprintf(f_file, "%4d\t%4d\t%f\n", count, atoi(known), e); temp = temp->next; count++; /* fprintf(stderr, "the value of e is %f\n", e); fprintf(stderr, "the value of d is %f\n", d); */ } } fclose(f_file); } fprintf(stdout, "%f\n", (float) rec/no_files); fprintf(stderr, "\n\nThe recognition rate is %f (%d/%d)\n", (float) rec/no_files, rec, no_files); fprintf(fp, "\n\nThe recognition rate is %f (%d/%d)\n", (float) rec/no_files, rec, no_files); fclose(fp);}int number_files(char list[]){ int no_files; char line[MAX_CHARS]; FILE *flist; /* Loop over input list file and calculate total no. of test files and create the array of tfaces */ if ( (flist = fopen(list, "r")) == NULL){ fprintf(stderr, "\nERROR: Input file %s cannot be opened \n\n", list); exit(2); } no_files = 0; while (fscanf(flist, "%s", line) != EOF){ if ( (strncmp(line, "#", 1) != 0) && (strlen(line) > 1)) no_files++; } fclose(flist); return (no_files);}int print_file(Tface tfaces, int n, char out[], float thres, int count){ int i, j, k, l, rec = 1; float d, e; Matchptr temp; FILE *f_file; char test[MAX_CHARS], known[MAX_CHARS], outfile[MAX_CHARS]; temp = tfaces.similar; l = strlen(tfaces.name); for (k=0; k<l ; k++){ if ( tfaces.name[k] >= '0' && tfaces.name[k] <= '9'){ test[k] = tfaces.name[k]; } else break; } test[k]='\0'; /* NEW FERET format/HACK */ strcpy(outfile,""); sprintf(outfile, "%s/%s.mit", out, test); if ( (f_file = fopen(outfile, "w")) == NULL){ fprintf(stderr, "\n ERROR: Input file %s cannot be opened \n", outfile); exit(2); } /* NEW FERET format/HACK */ for (j=0; j<n; j++){ l = strlen(temp->name); for (k=0; k<l; k++){ if (temp->name[k] >= '0' && temp->name[k] <= '9') known[k] = temp->name[k]; else break; } known[k] = '\0'; if (rec == 1){ d = temp->distance; if (strcmp(test, known) == 0){ count++; } } if (do_bayesian){ e = 100/(1+exp(sigmoid_scale * temp->distance)); } else { e = 100* log(conf_factor)/log(temp->distance + conf_factor); } fprintf(f_file, "%4d\t%4d\t%11.8f\n", rec, atoi(known), e); fflush(f_file); temp = temp->next; rec++; } fclose(f_file); return(count);}void kill_list(Matchptr list){ if (!empty_list(list)){ kill_list(list->next); free(list->name); free(list); }}int empty_list(Matchptr list){ if (list == NULL) return (1); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -