📄 parmwarp.c
字号:
/*----------------------------------------------------------------------PROGRAM: parmwarp.cDATE: 5/2/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII file of the format filename a b c d e f . . . and warps the images according to the affine transform | a b e | | | A = | c d f | | | | 0 0 1 | It outputs the warped 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"#include "matrix.h"#include "affine.h"#define MAX_CHARS 512/* ----------- Command-Line Parsing Stuff ------- */extern int optind;extern char *optarg;char *progname; /* used to store the name of the program */char comline[MAX_CHARS]; /* used to store the entire command line */#define OPTIONS "i:l:o:x:y:v:"char *usage = "\t-i indir -l list -o outdir\n\\t\t\t[-x out_xdim] [-y out_ydim] [-v bgvalue]\n";char *help="\Parametric Affine Warping\n\n\-i indir \t input directory\n\-l list \t ASCII list of filenames and their warp parameters\n\ \t with the following line format:\n\n\ \t filename a b c d e f\n\n\ \t where {a,b,c,d,e,f} define the affine transform:\n\ \t\n\ \t | a b e | \n\ \t | | \n\ \t A = | c d f | \n\ \t | | \n\ \t | 0 0 1 | \n\ \t\n\-o outdir \t output directory\n\-x out_xdim \t output x dimension (1st n columns) of warped image\n\-y out_ydim \t output y dimension (1st n rows) of warped image\n\-v bgvalue \t pixel value in void regions (default = 0)\n"; /*----------------------------------------------------------------------*/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, M, N; char command[MAX_CHARS],indir[MAX_CHARS],listfile[MAX_CHARS], \ infile[MAX_CHARS], modelfile[MAX_CHARS],outdir[MAX_CHARS], \ filename[MAX_CHARS], line[MAX_CHARS]; float **W; 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 outflag = 0; int y_flag = 0; int x_flag = 0; /* command-line defaults */ float bgvalue = 0.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 'o': strcpy(outdir, optarg); outflag = 1; break; case 'y': out_nrow = atoi(optarg); y_flag = 1; break; case 'x': out_ncol = atoi(optarg); x_flag = 1; break; case 'v': bgvalue = atof(optarg); break; case '?': errflag = 1; break; } /* command line error check */ if (errflag || !inflag || !listflag) { 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 (y_flag==0) out_nrow = nrow; if (x_flag==0) out_ncol = ncol; /* ---- allocate warp/image matrices ---- */ W = matrix(1, 3, 1, 3); image_in = matrix(1, nrow, 1, ncol); image_out = matrix(1, nrow, 1, ncol); if (bytes_pixel == 1) char_image = cmatrix(1, out_nrow, 1, out_ncol); /* ---- loop over input list file and warp ------- */ if ((fp = fopen(listfile, "r")) == NULL) { fprintf(stderr,"ERROR: Could not open list 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 filename and and warp parameters --- */ strcpy(infile, strtok(line, " \t")); for (i=1; i<=2; i++) W[1][i] = atof(strtok(NULL, " \t")); for (i=1; i<=2; i++) W[2][i] = atof(strtok(NULL, " \t")); for (i=1; i<=2; i++) W[i][3] = atof(strtok(NULL, " \t")); W[3][1] = W[3][2] = 0.0; W[3][3] = 1.0; 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 file %s\n", nrow, ncol, (bytes_pixel==4 ? "float":"uchar"), infile); fprintf(stdout,"Affine matrix: \n"); for (i=1; i<=3; i++) { for (j=1; j<=3; j++) fprintf(stdout,"%+3.3f ", W[i][j]); fprintf(stdout,"\n"); } /* --- apply warp to image --- */ i = affine_warp(image_in, image_out, nrow, ncol, W, bgvalue); fprintf(stdout,"Applied affine warp ... %d lowpass iterations\n", i); /* --- write output image --- */ if (bytes_pixel == 1) for (i=1; i<=out_nrow; i++) for (j=1; j<=out_ncol; j++) char_image[i][j] = (int) image_out[i][j]; 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 dir descriptor --- */ write_descriptor(outdir, nframe, out_ncol, out_nrow, bytes_pixel, comline); free_matrix(W, 1, 3, 1, 3); free_matrix(image_in, 1, nrow, 1, ncol); free_matrix(image_out, 1, nrow, 1, ncol); if (bytes_pixel == 1) free_cmatrix(char_image, 1, out_nrow, 1, out_ncol); fclose(fp); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -