📄 filter.c
字号:
/*----------------------------------------------------------------------PROGRAM: filter.cDATE: 5/5/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII file of the format filename . . . and applies the filter to each image in the input directory, the resulting filtered images are written to the output directory under the same names. ---------------------------------------------------------------------- */#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 "convolve.h"/* ----------- Command-Line Parsing Stuff ------- */extern int optind;extern char *optarg;char *progname; /* used to store the name of the program */char comline[256]; /* used to store the entire command line */#define OPTIONS "i:l:f:o:t:"char *usage = "-i indir -l list -f filter.bf -o outdir [-t out_datatype] \n";char *help="\2D Filtering of a set of images\n\n\-i indir \t input directory\n\-l listfile \t ASCII list of filename in indir to process (one per line)\n\-f filter.bf \t BF-format 2d filter file\n\-t out_datatype\t output data type: uchar or float (default is same as input)\n\n"; #define MAX_CHARS 256/*----------------------------------------------------------------------*/main(int argc, char **argv){ register int i,j,k,l,ii,jj; int f, c, nframe, nfeatures, sets, bytes_pixel; int nrow, ncol, M, N; char command[MAX_CHARS],indir[MAX_CHARS],listfile[MAX_CHARS], \ infile[MAX_CHARS], filterfile[MAX_CHARS],outdir[MAX_CHARS], \ filename[MAX_CHARS], line[MAX_CHARS]; char out_datatype[MAX_CHARS]; int Frow, Fcol; float **F; float **image_in, **image_out; unsigned char **char_image; float fval1, fval2, fval; FILE *fp, *fp2; /* for output values dump */ /* required input flags */ int errflag = 0; int inflag = 0; int listflag = 0; int filterflag = 0; int outflag = 0; /* command line defaults */ int in_datatype_float_flag = 0; int out_datatype_float_flag = 0; int out_datatype_select = 0; 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(indir, optarg); inflag = 1; break; case 'l': strcpy(listfile, optarg); listflag = 1; break; case 'f': strcpy(filterfile, optarg); filterflag = 1; break; case 'o': strcpy(outdir, optarg); outflag = 1; break; case 't': strcpy(out_datatype, optarg); out_datatype_select = 1; break; case '?': errflag = 1; break; } /* command line error check */ if (errflag || !inflag || !outflag || !listflag || !filterflag) { fprintf(stderr,"\nUSAGE: %s %s\n%s\n", progname, usage, help); exit(1); } /* ---- read indir descriptor file -------- */ read_descriptor(indir, &nframe, &sets, &bytes_pixel, &ncol, &nrow); if (sets>1) myerror("Input files must be single-set DAT files!"); if (bytes_pixel==4) in_datatype_float_flag = 1; out_datatype_float_flag = in_datatype_float_flag; /* default = input type */ if (out_datatype_select ==1) if (strncmp(out_datatype, "uchar", 5)==0) out_datatype_float_flag = 0; /* ---- read filter file ---- */ F = read_BIN(filterfile, &Frow, &Fcol); image_in = matrix(1, nrow, 1, ncol); image_out = matrix(1, nrow, 1, ncol); if (out_datatype_float_flag == 0) char_image = cmatrix(1, nrow, 1, ncol); /* ---- loop over input list file and warp ------- */ if ((fp = fopen(listfile, "r")) == NULL) { fprintf(stderr,"ERROR: Could not open input file %s \n\n", listfile); exit(1); } nframe = 0; while (fgets(line, MAX_CHARS, fp)) { if (strncmp(line, "#", 1) != 0 && strlen(line)>1) { nframe++; /* ------- read image ------- */ sscanf(line,"%s",infile); sprintf(filename,"%s/%s", indir, infile); if (in_datatype_float_flag>0) read_RAW_float(filename, image_in, nrow, ncol); else read_RAW(filename, image_in, nrow, ncol); fprintf(stdout,"Read %d-by-%d %s input image %s\n", nrow, ncol, (in_datatype_float_flag>0 ? "float":"uchar"), filename); /* ----- filter the image ------ */ conv2d(image_in, image_out, nrow, ncol, F, Frow, Fcol); fprintf(stdout,"Applied filter %s \n",filterfile); /* ------ process output and write to disk ------ */ sprintf(filename,"%s/%s", outdir, infile); if (out_datatype_float_flag>0) write_RAW_float(filename, image_out, nrow, ncol); else { for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) char_image[i][j] = (int) image_out[i][j] + 0.5; write_RAW(filename, char_image, nrow, ncol); } fprintf(stdout,"Wrote %d-by-%d %s output file %s\n\n", nrow, ncol, (out_datatype_float_flag>0 ? "float":"uchar"), filename); } } /* --- write output descriptor file in outdir ------ */ write_descriptor(outdir, nframe, ncol, nrow, (out_datatype_float_flag>0 ? 4:1), comline); fclose(fp); free_matrix(F, 1, Frow, 1, Fcol); free_matrix(image_in, 1, nrow, 1, ncol); free_matrix(image_out, 1, nrow, 1, ncol); if (out_datatype_float_flag==0) free_cmatrix(char_image, 1, nrow, 1, ncol); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -