📄 crop.c
字号:
/*----------------------------------------------------------------------PROGRAM: crop.cDATE: 11/20/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII list file of the format filename . . . from the input directory and crops each image in the indir. It outputs the cropped 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:x:y:w:h:o:"char *usage = "-i indir -l list -x row -y col -h height -w width -o outdir \n";char *help="\Crops Input Images \n\n\-i indir \t input directory\n\-l list \t ASCII file listing indir files to process (one per line)\n\-x x \t x-coordinate (row #) of upper left corner\n\-y y \t y-coordinate (col #) of upper left corner\n\-h height \t height of cropped region (# of rows)\n\-w width \t width of cropped region (# of cols)\n\-o outdir \t output directory\n";#define MAX_CHARS 256/* ---------- Function prototypes --------- */void flip_horizontal(float **image_in, float**image_out, int nrow, int ncol);void flip_vertical(float **image_in, float**image_out, int nrow, int ncol);/*----------------------------------------------------------------------*/main(int argc, char **argv){ register int i,j,k,l,ii,jj; int f, c, nframe, nfeatures, sets, bytes_pixel; int nrow, ncol, out_nrow, out_ncol, x, y, width, height, 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], flip[MAX_CHARS]; float **image_in, **image_out; unsigned char **char_image; FILE *fp; /* required input flags */ int errflag = 0; int inflag = 0; int listflag = 0; int x_flag = 0; int y_flag = 0; int w_flag = 0; int h_flag = 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 'x': x = atoi(optarg); x_flag = 1; break; case 'y': y = atoi(optarg); y_flag = 1; break; case 'w': width = atoi(optarg); w_flag = 1; break; case 'h': height = atoi(optarg); h_flag = 1; break; case 'o': strcpy(outdir, optarg); outflag = 1; break; case '?': errflag = 1; break; } /* --- command line error check --- */ if (errflag || !inflag || !listflag || !(x_flag && y_flag && w_flag && h_flag)) { 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!"); image_in = matrix(1, nrow, 1, ncol); out_nrow = height; out_ncol = width; image_out = matrix(1, out_nrow, 1, out_ncol); if (bytes_pixel==1) char_image = cmatrix(1, out_nrow, 1, out_ncol); /* --- check crop parameters for validity --- */ if ((x+out_nrow>nrow) || (y+out_ncol>ncol)) { fprintf(stderr,"\nERROR: crop regions extends beyond image boundaries\n"); exit(2); } /* ---- loop over input list file and flip ------- */ 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_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"), filename); /* --- crop the image image --- */ for (i=1; i<=out_nrow; i++) for (j=1; j<=out_ncol; j++) image_out[i][j] = image_in[x+i-1][y+j-1]; if (bytes_pixel==1) for (i=1; i<=out_nrow; i++) for (j=1; j<=out_ncol; j++) char_image[i][j] = (unsigned char) image_out[i][j]; /* --- write output file --- */ sprintf(filename,"%s/%s", outdir, infile); if (bytes_pixel==4) write_RAW_float(filename, image_out, out_nrow, out_ncol); else write_RAW(filename, char_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 ------ */ write_descriptor(outdir, nframe, out_ncol, out_nrow, bytes_pixel, comline); /* --- free up image matrices ---- */ free_matrix(image_in, 1, nrow, 1, ncol); free_matrix(image_out, 1, out_nrow, 1, out_ncol); if (bytes_pixel==1) free_cmatrix(char_image, 1, out_nrow, 1, out_ncol); fclose(fp); return 0;}/* ====================================================================== */void flip_horizontal(float **image_in, float **image_out, int nrow, int ncol){ register int i,j; for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) image_out[i][j] = image_in[i][ncol-j+1];}/* ---------------------------------------------------------------------- */void flip_vertical(float **image_in, float **image_out, int nrow, int ncol){ register int i,j; for (j=1; j<=ncol; j++) for (i=1; i<=nrow; i++) image_out[i][j] = image_in[nrow-i+1][j];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -