?? mask.c
字號:
/*----------------------------------------------------------------------PROGRAM: mask.cDATE: 6/2/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII file of the format filename . . . and masks the images according to the BF file provided It outputs the masked files in an output directory with the same names ---------------------------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <float.h>#include "util.h"#include "io.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:m:o:"char *usage = "-i indir -l list -m mask.bf -o outdir \n";char *help="\Applies a floating-point mask to an image\n\n\-i indir \t input directory\n\-l list \t ASCII file listing indir files to process (one per line)\n\-m mask.bf \t BF-format mask file\n\-o outdir \t output directory\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], maskfile[MAX_CHARS],outdir[MAX_CHARS], \ filename[MAX_CHARS], line[MAX_CHARS]; float **image, **mask; unsigned char **char_image; FILE *fp; /* required input flags */ int errflag = 0; int inflag = 0; int listflag = 0; int maskflag = 0; int outflag = 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 'm': strcpy(maskfile, optarg); maskflag = 1; break; case 'o': strcpy(outdir, optarg); outflag = 1; break; case '?': errflag = 1; break; } /* --- command line error check --- */ if (errflag || !inflag || !listflag || !maskflag) { fprintf(stderr,"\nUSAGE: %s %s\n%s\n", progname, usage, help); exit(1); } /* ---- read indir descriptor ---- */ read_descriptor(indir, &nframe, &sets, &bytes_pixel, &ncol, &nrow); if (sets>1) myerror("Input files must be single-set DAT files!"); /* ---- read mask file ---- */ mask = read_BIN(maskfile, &M, &N); if (M!=nrow || N!=ncol) { fprintf(stderr, "ERROR: Input image dimension %d-by-%d doesn't match mask dimensions %d-by-%d\n", i, j, nrow, ncol); exit(1); } image = matrix(1, nrow, 1, ncol); if (bytes_pixel==1) char_image = cmatrix(1, nrow, 1, ncol); /* ---- loop over input list file and mask ------- */ 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 next image ---- */ sscanf(line,"%s",infile); sprintf(filename,"%s/%s", indir, infile); if (bytes_pixel == 4) read_RAW_float(filename, image, nrow, ncol); else read_RAW(filename, image, nrow, ncol); fprintf(stdout,"Read %d-by-%d %s input file %s\n", nrow, ncol, (bytes_pixel==4 ? "float":"uchar"), filename); /* --- apply mask --- */ if (bytes_pixel == 1) for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) char_image[i][j] = (int) (image[i][j] * mask[i][j]); if (bytes_pixel == 4) for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) image[i][j] *= mask[i][j]; fprintf(stdout,"Applied mask ...\n"); /* --- write output file --- */ sprintf(filename,"%s/%s", outdir, infile); if (bytes_pixel==4) write_RAW_float(filename, image, nrow, ncol); else write_RAW(filename, char_image, nrow, ncol); fprintf(stdout,"Wrote %d-by-%d %s output file %s\n\n", nrow, ncol, (bytes_pixel==4 ? "float":"uchar"), filename); } } /* --- write output descriptor ------ */ write_descriptor(outdir, nframe, ncol, nrow, bytes_pixel, comline); free_matrix(image, 1, nrow, 1, ncol); free_matrix(mask, 1, nrow, 1, ncol); if (bytes_pixel==1) free_cmatrix(char_image, 1, nrow, 1, ncol); fclose(fp); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -