📄 db_eigsearch.c
字号:
/*----------------------------------------------------------------------PROGRAM: db_eigsearch.cDATE: 10/14/93AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ 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---------------------------------------------------------------------- The input facelist should be a text file with the following format > N > f1 > f2 > . > . > . > fN where the N entries fi are the 4-digit code of the face images in the data directory /d/modeling/face-database Alternatively, one can process a single face image using the flag -n (4-digit code)------------------------------------------------------------------------ Configure this code using the following defines: DO_VAR: will set each pixel whose surrond's variance is < VAR_THRESHOLD to VAR_MAXVALUE ---------------------------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <float.h>#include "util.h"#include "io.h"/* ----------- CONFIGURES ---------------- */#define DBASE_PATH "/d/modeling/face-database"#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_EIGENVECTORS 128 /* for static storage in dffs() */#define I_MIN 40 /* default search limits */#define I_MAX 110#define J_MIN 40#define J_MAX 100/* ----------- 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:o:d:n:f:l:k:gume"char *usage = "-i facelist [-n faceindex] [-d datadir] \n \t\t \ [-o outdir] [-f first_eig] [-l last_eig] [-k feature#] \n \t\t \ [-graymap] [-unitmap] [-mahalanobis] [-errormapdump]";/* --------- 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);/* -------- Globals ---------------- */float **template[MAX_NUM_TEMPLATES];float **errormap[MAX_NUM_TEMPLATES];float **variances;/* ------- Command Line Defaults ------------- */int framespec = 0;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 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,c,feature=1,frame,nframe,nfeatures,sets, bytes_pixel; int nrow, ncol, max_N, xc, yc; char command[80],infile[80],datapath[80],outdir[80],filename[80]; int *facelist; float **image; float fval1, fval2, fval; float maxerror; FILE *fp; /* for output values dump */ int rowl[MAX_NUM_TEMPLATES],rowr[MAX_NUM_TEMPLATES]; int coll[MAX_NUM_TEMPLATES],colr[MAX_NUM_TEMPLATES]; int rowm[MAX_NUM_TEMPLATES],colm[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 */ float error[MAX_NUM_TEMPLATES]; int **templatesize; float *patch; /* required input flags */ int errflag = 0; int inflag = 0; int outflag = 0; int singlefeature = 0; /* command line defaults */ int writeerrormap = 0; /* default is not to produce an errormap */ int singleframe = 0; strcpy(outdir,"."); /* default output dir is . */ 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, " "); /* ---------------------- Command Line Parse ------------------------ */ while ((c = getopt(argc, argv, OPTIONS)) != EOF) switch (c) { case 'i': strcpy(infile, optarg); inflag = 1; break; case 'd': strcpy(datapath, optarg); break; case 'n': frame = atoi(optarg); singleframe = 1; inflag = 1; break; case 'k': feature = atoi(optarg); singlefeature = 1; break; case 'o': strcpy(outdir, optarg); outflag = 1; break; case 'f': eig_first = atoi(optarg); break; case 'l': 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 '?': errflag = 1; break; } /* command line error check */ if (errflag || !inflag) { fprintf(stderr,"\nInput directory is required! \n"); fprintf(stderr,"\nUSAGE: %s %s\n\n", progname, usage); exit(1); } /* -------- 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); } } 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); /* ---- read DAT-file parameters -------- */ read_descriptor(DBASE_PATH, &nframe, &sets, &bytes_pixel, &ncol, &nrow); if (bytes_pixel>1 || sets>1) myerror("Input files must be single-byte single-set DAT files!"); image = matrix(1, nrow, 1, ncol); 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]; if (imin[k]<I_MIN) imin[k]=I_MIN; if (imax[k]>I_MAX) imax[k]=I_MAX; if (jmin[k]<J_MIN) jmin[k]=J_MIN; if (jmax[k]>J_MAX) jmax[k]=J_MAX; } /* ---- open input list file and read in file indices ---- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -