📄 feretio.c
字号:
#include <stdio.h>#include <string.h>#include <math.h>#include <unistd.h>#include <assert.h>/*======================================================================== This is a version of the facenorm.c program originally used to normalize FERET face images for the FERET 1996/97 studies. This code was originally used by Moon and Phillips to pre-process imagery for the FERET 1996/97 tests. It was subsequently updated by Patric Grother at NIST. float *data, float *data, float *data, At CSU we have disentangled this source code from the larger FERET code distribution. We have left many of the previous comments in place, although some are no longer accurate. Here is updated usage and compilation information. Usage: csu1face2norm [options] coordinates_file images_in_dir images_out_dir Options: -v verbose -h turn off histogram equalization -r turn off rotation normalization -s turn off standardization to zero mean and unit standard deviation -p write a preview of transformed image in pgm format -m maskfile.dat use "0" to not apply a mask with no option the default mask is m150x130.dat Compile: gcc -I. -I/usr/include -L/usr/lib -lm csu1face2norm.c -o csu1face2norm The changes we have made include: 1. We have shifted this code back to being ANSI C with no C++. There was indeed no real use of C++ in the sense of being object oriented. The primary shift to get the code back to straight C was to shift from new and delete back to malloc and free. 2. The input images are now read from pgm formatted images. This is much simpler than using JPEG, and the jpeg images that come with the FERET distribution are indeed functionally near equivalent to PGM, with the exeception that coding a reader is non-trivial. When the same two images are stored to disk in jpeg and pgm, the pgm is actually slightly smaller. 3. Added an optional facility to write a preview version of the raw data image in PGM format for easy viewing. do NOT use these as replacements for the actual "*.nrm" images generated by this normilization code. Ross Beveridge May 7, 2001************************************************************************/// Prior doc header from NIST./************************************************************** facenorm.c : program to rotate the face to horizontal, based on the (reye+leye)/2 and mouth coordinates and then rescale & crop to a fixed size. usage : facenorm filelist contains the listing of the files, plus the eye coordinates input : fileinfo.dat compile : gcc facenorm.c byte_swap.c -o face2 -lm -O4 Modified on: 5/11/98 Hyeonjoon Moon: unspecifed modifications x/11/99 Jonathon Phillips: modifed to run on Windows NT using Cygwin B20 1/20/00 Patrick Grother: modified to send error messages to stderr modified to detect failed sscanf calls removed leaks in resize removed unneeded mallocs before while loop added calls to free where necessary 1/24/00 Patrick Grother: replaced calloc by malloc in hist_equal where possible----------------------------------------------------------------- renamed Jan 30 1999 (the previous face_NT_Hist remain in a sister dir) facenorm : function is the same, only the image input are expected to be standard JPEG files. Modified on 1/30/00 Patrick Grother: replaced old plain byte reading code with call to jpeg reader in library 1/30/00 removed large character arrays for strings replaced #define with typed globals put masking read and ops in subroutines 2/12/00 fixed histogram bug 10/25/00 added command line switches for histogram equalization and intensity standardization ****************************************************************//*====================================================================*//* Begin Code *//*====================================================================*/typedef unsigned char UCH;static int min(const int a, const int b) { return (a < b) ? a : b; }/*------------------------------------------------------------------------ Function Prototypes ------------------------------------------------------------------------*/float *readferetraster(const char *fn,int numpix);float *writeferetraster(const char *fn, float *data, int numpix);UCH *readImagePGM(const char *filename, int *w, int *h, int verbose);void writeImagePGM(const char *fn, float *data, int numpix, int w, int h);int isMachineLittleEndian();void byteswap_4(void *data, const int numfourbyteelements);char **read_strings(const char *fn, int *numlines);void free_strings(char **x, const int n);FILE *fopen_with_error(const char *fn, const char *mode, const char *routine);FILE *fopen_anycase(const char *fn, const char *mode, const char *routine);void strnullchecks(const char *fn, const char *name, const char *routine);char *strconc(const char *s1, const char *s2);char *strclone(const char *si);char *strlower(char *x);char *strupper(char *x);char *newextlong(char **filename, const char *extension);static void bailer(const char *s1, const char *s2, const char *s3);void syserr(const char *funcname, const char *syscall, const char *msg);/*------------------------------------------------------------------------ Utilities integrated directly into this source from elsewhere in FERET code distribution. ------------------------------------------------------------------------*/float *readferetraster(const char *fn, int numpix){ const char *routine = "readferetraster"; FILE *fp; float* data; fp = (FILE*) fopen_with_error(fn, "rb", routine); data = (float*)malloc(sizeof(float)*numpix); if(!data){ syserr("readferetraster", "malloc of data failed", ""); } if (!fread(data, sizeof(float), numpix, fp)) syserr(routine, fn, "fread"); if (isMachineLittleEndian()) byteswap_4(data, numpix); fclose(fp); return data;}float *writeferetraster(const char *fn, float *data, int numpix){ const char *routine = "writeferetraster"; FILE *fp; fp = (FILE*) fopen_with_error(fn, "wb", routine); if (isMachineLittleEndian()) byteswap_4(data, numpix); if (numpix != fwrite(data, sizeof(float), numpix, fp)) syserr(routine, fn, "fwrite"); fclose(fp); return data;}int isMachineLittleEndian() { char magic[4] = {0x01,0x02,0x03,0x04}; unsigned long *longMagic = (unsigned long *) magic; if (*longMagic == 0x01020304) return 0; if (*longMagic == 0x04030201) return 1; fprintf(stderr,"Funky Byte Order, I give Up!!\n"); exit(0);}void byteswap_4(void *data, const int numfourbyteelements){ int i; int *d4 = (int *)data; UCH *d1 = (UCH *)data; if (!(isMachineLittleEndian())) // i.e. BIG_ENDIAN { for (i = 0 ; i < numfourbyteelements ; i++, d1 += 4 ) d4[i] = ((int)d1[3] << 24) | ((int)d1[2] << 16) | ((int)d1[1] << 8) | (int)d1[0]; } else // i.e. LITTLE_ENDIAN { for (i = 0 ; i < numfourbyteelements ; i++, d1 += 4 ) d4[i] = ((int)d1[0] << 24) | ((int)d1[1] << 16) | ((int)d1[2] << 8) | (int)d1[3]; }}UCH *readImagePGM(const char *filename, int *w, int *h, int verbose){ const char *routine = "readImagePGM()"; int width, height, max, i, sum; int val; char fchar; char line[100]; char ftype[16]; FILE *infile; UCH *im; /* Read first line, and test if it is propoer PNM type */ if (verbose) fprintf(stdout,"Going to open file %s\n", filename); infile = (FILE*) fopen_with_error(filename, "rb", routine); fgets(line,100,infile); sscanf(line," %s",&ftype); if (verbose) fprintf(stdout,"File Type is %s.\n", ftype); if (! (strcmp(ftype,"P5") == 0)) { syserr("face2norm", "Currenlty only binary pgm files, type P5, supported", ""); } /* Read lines, ignoring those starting with Comment Character, until the Image Dimensions are read. */ fchar = '#'; while (fchar == '#') { fgets(line,100,infile); sscanf(line, " %c", &fchar); } if (verbose) fprintf(stdout,"Second non-comment line of image file %s.\n", line); sscanf(line, " %d %d", &width, &height); *w = width; *h = height; if (verbose) fprintf(stdout,"The width, height and size are: %d %d %d\n", width, height, width * height); /* Read lines, ignoring those starting with Comment Character, until the maximum pixel value is read. */ fchar = '#'; while (fchar == '#') { fgets(line,100,infile); sscanf(line, " %c", &fchar); } sscanf(line, "%d", &max); if (verbose) fprintf(stdout,"The max value for the pixels is: %d\n", max); if (! (max == 255)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -