📄 scalewarp.c
字号:
/*----------------------------------------------------------------------PROGRAM: scalewarp.cDATE: 6/16/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII file of the format filename scale dontcare x y . . . and scales and translates the image so as to place the (x,y) point at (x_dest,y_dest) It outputs the transformed 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:m:o:s:x:y:r:c:v:"char *usage = "\t-i indir -l list -o outdir -x x_dest -y y_dest\n\\t\t\t[-s scalefactor] [-r out_nrow] [-c out_ncol]\n\\t\t\t[-v bgvalue]\n";char *help="\Scale/Translation Image Warping\n\n\-i indir \t input directory\n\-l list \t ASCII list of filenames and scale and translations\n\ \t with the following line format:\n\n\ \t filename scale dontcare x y\n\n\-o outdir \t output directory\n\-s scalefactor\t scale factor (multiplies above scale, default = 1)\n\-x x_dest \t destination row position of the point (x,y)\n\-y y_dest \t destination col position of the point (x,y)\n\-r out_nrow \t output dimension (1st n rows) of image\n\-c out_ncol \t output dimension (1st n columns) of 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 */ float x_dest, y_dest, x, y; float scalefactor = 1.0; float scale; /* required input flags */ int errflag = 0; int inflag = 0; int listflag = 0; int modelflag = 0; int outflag = 0; int y_flag = 0; int x_flag = 0; int r_flag = 0; int c_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 'm': strcpy(modelfile, optarg); modelflag = 1; break; case 'o': strcpy(outdir, optarg); outflag = 1; break; case 'r': out_nrow = atoi(optarg); r_flag = 1; break; case 'c': out_ncol = atoi(optarg); c_flag = 1; break; case 'x': x_dest = atof(optarg); x_flag = 1; break; case 'y': y_dest = atof(optarg); y_flag = 1; break; case 's': scalefactor = atof(optarg); break; case 'v': bgvalue = atof(optarg); break; case '?': errflag = 1; break; } /* command line error check */ if (errflag || !inflag || !listflag || !x_flag || !y_flag) { 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 (r_flag==0) out_nrow = nrow; if (c_flag==0) out_ncol = ncol; /* ---- set up 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 input file %s \n\n", listfile); exit(1); } nframe = 0; while (strlen(fgets(line, MAX_CHARS, fp))) { if (strncmp(line, "#", 1) != 0 && strlen(line)>1) { nframe++; /* --- read filename and reference coordinates --- */ strcpy(infile, strtok(line, " \t")); scale = atof(strtok(NULL, " \t")) * scalefactor; atoi(strtok(NULL, " \t")); x = atof(strtok(NULL, " \t")); y = atof(strtok(NULL, " \t")); for (i=1; i<=3; i++) for (j=1; j<=3; j++) W[i][j] = 0.0; W[1][1] = W[2][2] = scale; W[3][3] = 1.0; W[1][3] = x_dest - scale*x; W[2][3] = y_dest - scale*y; /* --- read file and apply warp --- */ 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,"Transform 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 transform ... %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 + -