⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ms_eigsearch.c

📁 FERET人脸库的处理代码。内函预处理
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------PROGRAM: ms_eigsearch3.cDATE:    10/14/93AUTHOR:  Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------  Multiple-Scale Local feature search by eigentemplates  This routine looks for 2 (3) BF files in the datapath (defined below):  1. features.bf  which is an N-by-2 matrix defining the size (row,col)     of each of the N templates.  2. template[n].bf  where n=1...N is the eigenvector ROW matrix where     the 1st row is the mean feature, and the remaining rows     are the principal eigenvectors 1:M.      NOTE: The eigenvectors are stored in column-order (as in MATLAB)  3. variances.bf which is an N-by-M matrix, where each row represents     the rank-ordered eigenvalues associated with that feature     NOTE: The set of scales over which the search is to be performed           is provided in an ASCII file of the format:	   s1	   s2	   .	   .	   .	   sN----------------------------------------------------------------------   NOTE: In this version if unitmap'ing is selected it will use         the Mahalanobis mean/stddev in addition to the standard	 distance computation.------------------------------------------------------------------------     Configure this code using the following defines:          DO_VAR:     will set each pixel whose surrond's variance                 is < VAR_THRESHOLD to VAR_MAXVALUE----------------------------------------------------------------------   NOTE: Fixed the umap_musigma_score problem	       ---------------------------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <float.h>#include "util.h"#include "io.h"#include "matrix.h"#include "affine.h"/* ----------- CONFIGURES ---------------- */#define DO_VAR                  0     /*  variance check     */#define VAR_THRESHOLD           100   /*  threshold for low-var patches */#define VAR_MAXVALUE            1e7   /*  replacement in distance map   */#define MAX_NUM_TEMPLATES       5#define MAX_NUM_SCALES          32#define MAX_NUM_EIGENVECTORS    128   /* for static storage in dffs() */#define MAX_VECTOR_LENGTH       128    /* for static storage in mahalanobis_distance() */#define MAX_CHARS               512/* ----------- Command-Line Parsing Stuff ------- */extern int optind;extern char *optarg;char *progname;          /* used to store the name of the program  */char comline[170];       /* used to store the entire command line  */#define OPTIONS "i:l:o:d:n:p:a:b:k:s:mguec"char *usage = "\t-i indir -l list -s scalefile [-d datadir]\n\\t\t\t[-o outfile] [-a first_ev] [-b last_ev] [-k feature]\n\\t\t\t[-g] [-u] [-m] [-e] [-c]\n";char *help = "\Multiscale Eigentemplate Search\n\n\-i indir   \t input directory\n\-l listfile\t ASCII file listing indir files to process (one per line)\n\-s scalefile\t ASCII file of scales\n\-d datadir \t data dir holding eigentemplate BF files (default = ./)\n\-o outfile \t output file (default = ./ms_eigsearch.out)\n\-a first_ev\t first eigenvector (default = 1)\n\-b last_ev \t last eigenvector  (default = 5)\n\-k feature \t search for the k-th feature only\n\-g         \t pre-process with graymap\n\-u         \t pre-process with unitmap\n\-m         \t use Mahalanobis distance instead of DFFS\n\-c         \t use spatial priors\n\-e         \t output detection maps (in indir/Maps)\n";/* --------- Function Prototypes ---------- */float dffs(float *patch, int N, float **eigvectors, int first, int last);float mahalanobis(float *patch, int N, float **eigvectors, float *eigvalues,		  int first, int last);float graymap(float *patch, int N);float unitmap(float *patch, int N);void statistics(float *p, int N, float *mean, float *sigma);float umap_musigma_distance(float mean, float sigma, float **umap_musigma);float mahalanobis_distance(float *x, int N, float *Mu, float **inv_Sigma);/* -------- Globals ---------------- */float **template[MAX_NUM_TEMPLATES];float **errormap[MAX_NUM_TEMPLATES];float **variances;float **umap_musigma[MAX_NUM_TEMPLATES];float **spatial_musigma[MAX_NUM_TEMPLATES];/* ------- Command Line Defaults ------------- */int do_dmdumps = 0; /* default is NOT to do errormap dumps  */int do_graymap = 0; /* default is NOT to graymap each patch */int do_unitmap = 0; /* default is NOT to unitmap each patch */int do_mahalanobis = 0; /* default is NOT to do this computation */int do_spatial = 0; /* default is NOT to add [x,y] Mahalanobis dist */int eig_first = 1;  /* search using first 5 eigenvectors by default */int eig_last  = 5;/*----------------------------------------------------------------------*//* ---------------------------- MAIN ---------------------------------- */main(int argc, char *argv[]){  register int i,j,k,l,ii,jj;  int f,s,c,feature=1,nframe,nfeatures,sets, bytes_pixel;  int nrow, ncol, max_N, xc, yc;  char command[MAX_CHARS],indir[MAX_CHARS],infile[MAX_CHARS];  char listfile[MAX_CHARS], scalefile[MAX_CHARS], line[MAX_CHARS];  char datapath[MAX_CHARS],outdir[MAX_CHARS],filename[MAX_CHARS];  char outfile[MAX_CHARS];  float **image, **image_orig;  float fval1, fval2, fval, mean, sigma;  float maxerror;  FILE *fp, *fp2;         /* for output values dump */  int rowl[MAX_NUM_TEMPLATES],rowr[MAX_NUM_TEMPLATES];  int coll[MAX_NUM_TEMPLATES],colr[MAX_NUM_TEMPLATES];  int imin[MAX_NUM_TEMPLATES],imax[MAX_NUM_TEMPLATES];  int jmin[MAX_NUM_TEMPLATES],jmax[MAX_NUM_TEMPLATES];  int N_dim[MAX_NUM_TEMPLATES]; /* the dimensionality of each template */  int M_dim[MAX_NUM_TEMPLATES]; /* the # of eigenvectors for each template */  int numscales;  float scales[MAX_NUM_SCALES];  int rowm[MAX_NUM_SCALES][MAX_NUM_TEMPLATES];  int colm[MAX_NUM_SCALES][MAX_NUM_TEMPLATES];  float error[MAX_NUM_SCALES][MAX_NUM_TEMPLATES];  float **Warp;  float *my_vector;  int xdim, ydim;  int   best_scale[MAX_NUM_TEMPLATES];  float best_error[MAX_NUM_TEMPLATES];  int   best_rowm[MAX_NUM_TEMPLATES];  int   best_colm[MAX_NUM_TEMPLATES];    int **templatesize;  float *patch;  /* required input flags */    int errflag   = 0;  int inflag    = 0;  int scaleflag = 0;  int listflag  = 0;    /* command line defaults */  int singlefeature = 0;    int firstframe = 0;  int lastframe  = 0;  strcpy(datapath,".");               /* default datapath directory to cwd   */  /* setup program name and command line strings */    progname = argv[0];  for (i=0; i<argc; i++)    strcat(comline, argv[i]),strcat(comline, " ");  /* set up default outfile name */  sprintf(outfile,"%s.out",progname); /* default output file is progname.out */    /* ----------------------  Command Line Parse ------------------------ */    while ((c = getopt(argc, argv, OPTIONS)) != EOF)    switch (c) {          case 'i':      strcpy(indir, optarg);      inflag = 1;      break;          case 'l':      strcpy(listfile, optarg);      listflag = 1;      break;    case 'd':      strcpy(datapath, optarg);      break;    case 's':      strcpy(scalefile, optarg);      scaleflag = 1;      break;    case 'k':      feature = atoi(optarg);      singlefeature = 1;      break;    case 'o':      strcpy(outfile, optarg);      break;    case 'a':      eig_first  = atoi(optarg);      break;    case 'b':      eig_last = atoi(optarg);      break;    case 'g':      do_graymap = 1;      break;    case 'u':      do_unitmap = 1;      break;    case 'e':      do_dmdumps = 1;      break;    case 'm':      do_mahalanobis = 1;      break;    case 'c':      do_spatial = 1;      break;    case '?':      errflag = 1;      break;          }      /* command line error check */    if (errflag || !inflag || !scaleflag || !listflag) {    fprintf(stderr,"\nUSAGE: %s %s\n%s\n", progname, usage, help);    exit(1);  }  sprintf(outdir,"%s/Maps",indir);   /* default output dir is indir/Maps    */  /* -------- Load eigentemplate data --------------- */  sprintf(filename,"%s/features.bf",datapath);   {     float **m;    max_N = 0;    m = read_BIN(filename, &nfeatures, &ncol);    templatesize = imatrix(1, nfeatures, 1, 2);        for (i=1; i<=nfeatures; i++) {      for (j=1; j<=ncol; j++)	templatesize[i][j] = (int) m[i][j];      N_dim[i] = templatesize[i][1]*templatesize[i][2];      if (N_dim[i]>max_N) 	max_N = N_dim[i];      free_matrix(m, 1, nfeatures, 1, ncol);    }  }    for (i=1; i<=nfeatures; i++) {    sprintf(filename,"%s/template%d.bf",datapath,i);    template[i] = read_BIN(filename, &nrow, &ncol);    M_dim[i] = nrow;    if (ncol!=N_dim[i])      myerror("Template sizes in BF don't match those is definition file");  }  if (do_mahalanobis) {    sprintf(filename,"%s/variances.bf",datapath);    variances = read_BIN(filename, &nrow, &ncol);    if (nrow!=nfeatures) {      fprintf(stderr,	      "ERROR: variances.bf has %d rows! (must have %d)\n\n",	      nrow, nfeatures);      exit(1);    }    if (eig_last>ncol) {      fprintf(stderr,	      "ERROR: variances.bf has %d cols! (must have atleast %d)\n\n",	      ncol, eig_last);      exit(1);    }  }      if (do_unitmap) {    float **C, **invC;    int m, n;    C = matrix(1,2,1,2);    invC = matrix(1,2,1,2);    for (k=1; k<=nfeatures; k++) {      sprintf(filename,"%s/umap_musigma%d.bf",datapath,k);      umap_musigma[k] = read_BIN(filename, &m, &n );           /* invert the covariance matrix portion of umap_musigma[k] */      for (i=1; i<=2; i++)	for (j=1; j<=2; j++)	  C[i][j] = umap_musigma[k][i+1][j];      matrix_inverse(C, 2, invC);      for (i=1; i<=2; i++)	for (j=1; j<=2; j++)	  umap_musigma[k][i+1][j] = invC[i][j];      /* ----------- DEBUG -----------      for (i=1; i<=3; i++) {	for (j=1; j<=2; j++)	  printf("%f ",umap_musigma[k][i][j]);	printf("\n");      }      printf("\n\n");      C[1][1] = 10;      C[1][2] = 10;      printf("dist = %f \n", 	     mahalanobis_distance(C[1], 2, umap_musigma[k][1], &umap_musigma[k][1]));      printf("dist = %f \n",		    umap_musigma_distance(10, 10, umap_musigma[k]));      exit(1);      --------------- DEBUG ------------- */    }      free_matrix(C,1,2,1,2);    free_matrix(invC,1,2,1,2);  }     if (do_spatial) {    float **C, **invC;    int m, n;    C = matrix(1,2,1,2);    invC = matrix(1,2,1,2);    for (k=1; k<=nfeatures; k++) {      sprintf(filename,"%s/spatial_musigma%d.bf",datapath,k);      spatial_musigma[k] = read_BIN(filename, &m, &n );           /* invert the covariance matrix portion of umap_musigma[k] */      for (i=1; i<=2; i++)	for (j=1; j<=2; j++)	  C[i][j] = spatial_musigma[k][i+1][j];      matrix_inverse(C, 2, invC);      for (i=1; i<=2; i++)	for (j=1; j<=2; j++)	  spatial_musigma[k][i+1][j] = invC[i][j];      /* ----------- DEBUG -----------      for (i=1; i<=3; i++) {	for (j=1; j<=2; j++)	  printf("%f ",spatial_musigma[k][i][j]);	printf("\n");      }      printf("\n\n");      C[1][1] = 10;      C[1][2] = 10;      printf("dist = %f \n", 	     mahalanobis_distance(C[1], 2, spatial_musigma[k][1], &spatial_musigma[k][1]));      printf("dist = %f \n",		    spatial_musigma_distance(10, 10, spatial_musigma[k]));      exit(1);      --------------- DEBUG ------------- */    }      free_matrix(C,1,2,1,2);    free_matrix(invC,1,2,1,2);  }  /* ---- determine left/right boundaries for errormaps ----- */  for (i=1; i<=nfeatures; i++) {    rowl[i] = (int) templatesize[i][1]/2.0;    rowr[i] = templatesize[i][1] - rowl[i] - 1;    coll[i] = (int) templatesize[i][2]/2.0;    colr[i] = templatesize[i][2] - coll[i] - 1;  }  patch = vector(1, max_N);  my_vector = vector(1, MAX_VECTOR_LENGTH);  /* ----  read indir descriptor -------- */    read_descriptor(indir, &nframe, &sets, &bytes_pixel, &ncol, &nrow);  if (sets>1)     myerror("Input files must be single-set DAT files!");  image = matrix(1, nrow, 1, ncol);  image_orig = matrix(1, nrow, 1, ncol);  /* --- allocate errormap tensor and set window boundaries --- */  for (k=feature; k<=(singlefeature>0 ? feature:nfeatures); k++) {    errormap[k] = matrix(1, nrow, 1, ncol);    imin[k] = rowl[k] + 1;    imax[k] = nrow - rowr[k];    jmin[k] = coll[k] + 1;    jmax[k] = ncol - colr[k];  }    /* -------- Load scalefile -------------- */  sprintf(filename,"%s",scalefile);  if ((fp = fopen(filename, "r")) == NULL) {    fprintf(stderr,"ERROR Could not open scale file %s \n\n", filename);    exit(1);  }  i = 1;  while (fgets(line, MAX_CHARS, fp)) {    if (strncmp(line, "#", 1) != 0 && strlen(line)>1) {      sscanf(line, "%f", scales+i);      i++;    }  }  numscales = i-1;  fclose(fp);  Warp = matrix(1, 3, 1, 3);  /* ----  open output data file ---- */  sprintf(filename,"%s", outfile);  if ((fp2 = fopen(filename, "w")) == NULL) {    fprintf(stderr,"ERROR Could not open output file %s \n\n", filename);    exit(1);  }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -