📄 decimate.c
字号:
/*----------------------------------------------------------------------PROGRAM: decimate.cDATE: 5/5/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII file of the format filename . . . and applies a single-level Gaussian Pyramid REDUCE operation to each file. The resulting output files 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:o:a:"char *usage = "-i indir -l list -o outdir [-a parameter] \n";char *help = "2D Image Decimation:\n\n\-i indir \t input directory\n\-l listfile \t ASCII list of filenames in indir to process (one per line)\n\-o outdir \t output directory\n\-a parameter \t filter parameter:\n\ \t 0.4 -> Gaussian Pyramid filter (default)\n\ \t 0.5 -> triangular filter\n\ \t 0.0 -> NO pre-filtering (subsample only)\n";#define MAX_CHARS 256/*----------------------------------------------------------------------*/main(int argc, char **argv){ register int i,j,k,l,ii,jj; int f, c, nframe, sets, bytes_pixel; int nrow, ncol, out_nrow, out_ncol, M, N; char command[MAX_CHARS],indir[MAX_CHARS],listfile[MAX_CHARS], \ infile[MAX_CHARS], outdir[MAX_CHARS], \ filename[MAX_CHARS], line[MAX_CHARS]; float **image_in, **image_out; float *sep_filter; unsigned char **out_uchar_image; float **out_float_image; float fval1, fval2, fval; FILE *fp, *fp2; /* for output values dump */ /* required input flags */ int err_flag = 0; int in_flag = 0; int list_flag = 0; int out_flag = 0; /* command-line defaults */ float a_parameter = 0.4; 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); in_flag = 1; break; case 'l': strcpy(listfile, optarg); list_flag = 1; break; case 'o': strcpy(outdir, optarg); out_flag = 1; break; case 'a': a_parameter = atof(optarg); break; case '?': err_flag = 1; break; } /* command line error check */ if (err_flag || !in_flag || !list_flag || !out_flag) { fprintf(stderr,"\nUSAGE: %s %s\n%s\n", progname, usage, help); exit(1); } /* ---- setup separable 5-tap filters ---- */ sep_filter = vector(1, 5); if (a_parameter>0) { sep_filter[1] = sep_filter[5] = 0.25 - a_parameter/2.0; sep_filter[2] = sep_filter[4] = 0.25; sep_filter[3] = a_parameter; } /* ---- read DAT-file parameters and Images -------- */ read_descriptor(indir, &nframe, &sets, &bytes_pixel, &ncol, &nrow); if (sets>1) myerror("Input files must be single-set DAT files!"); /* ----- allocate memory for image matrices ------ */ image_in = matrix(1, nrow, 1, ncol); image_out = matrix(1, nrow, 1, ncol); out_nrow = (int) nrow/2.0; out_ncol = (int) ncol/2.0; if (bytes_pixel == 4) out_float_image = matrix(1, out_nrow, 1, out_ncol); else out_uchar_image = cmatrix(1, out_nrow, 1, out_ncol); /* ---- loop over input list file and decimate each image ------- */ 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 input image -------------- */ sscanf(line,"%s",infile); sprintf(filename,"%s/%s", indir, infile); if (bytes_pixel == 4) read_RAW_float(filename, image_in, nrow, ncol); else read_RAW(filename, image_in, nrow, ncol); fprintf(stdout,"Read %d-by-%d %s input file %s\n", nrow, ncol, (bytes_pixel==4 ? "float":"uchar"), infile); /* ------ filter the image ----------------- */ if (a_parameter>0) { conv2d_sep(image_in, image_out, nrow, ncol, sep_filter, 5, sep_filter, 5); fprintf(stdout,"Applied filter, parameter = %1.3f\n", a_parameter); } else { for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) image_out[i][j] = image_in[i][j]; } /* ------ subsample the image -------------- */ if (bytes_pixel == 4) for (i=1; i<=out_nrow; i++) for (j=1; j<=out_ncol; j++) out_float_image[i][j] = image_out[2*(i-1)+2][2*(j-1)+2]; else for (i=1; i<=out_nrow; i++) for (j=1; j<=out_ncol; j++) out_uchar_image[i][j] = (int) image_out[2*(i-1)+2][2*(j-1)+2] + 0.5; /* ---------- write reduced image to disk -------------- */ sprintf(filename,"%s/%s", outdir, infile); if (bytes_pixel == 4) write_RAW_float(filename, out_float_image, out_nrow, out_ncol); else write_RAW(filename, out_uchar_image, out_nrow, out_ncol); fprintf(stdout,"Wrote %d-by-%d %s output file %s\n\n", out_nrow, out_ncol, (bytes_pixel==4 ? "float":"uchar"), filename); } } /* ---- write output descriptor file ------- */ write_descriptor(outdir, nframe, out_ncol, out_nrow, bytes_pixel, comline); free_vector(sep_filter, 1, 5); free_matrix(image_in, 1, nrow, 1, ncol); free_matrix(image_out, 1, nrow, 1, ncol); if (bytes_pixel == 4) free_matrix(out_float_image, 1, out_nrow, 1, out_ncol); else free_cmatrix(out_uchar_image, 1, out_nrow, 1, out_ncol); fclose(fp); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -