📄 modelwarp.c
字号:
/*----------------------------------------------------------------------PROGRAM: modelwarp.cDATE: 6/2/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII file of the format filename x1 x2 x3 x4 y1 y2 y3 y4 . . . and warps the images according to the affine transform between the feature locations indicated and those in the model.bf file It outputs the warped files in an output directory with the same names The input images must be uchar---------------------------------------------------------------------- */#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:x:y:ra:b:v:"char *usage = "\t-i indir -l list -m model.bf -o outdir\n\\t\t\t[-r] [-a pt_1] [-b pt_2] [-x out_xdim] [-y out_ydim]\n\\t\t\t[-v bgvalue]\n";char *help="\Correspondance-Based Affine Warping\n\n\-i indir \t input directory\n\-l list \t ASCII list of filenames and their features locations\n\ \t with the following line format:\n\n\ \t filename i1 i2 i3 ... iN j1 j2 j3 ... jN\n\n\-m model.bf \t BF-format file of reference point coordinates\n\ \t with the following 2-by-N matrix format:\n\n\ \t i1 i2 i3 ... iN\n\ \t j1 j2 j3 ... jN\n\n\-o outdir \t output directory\n\-r \t apply rigid transform (based on default correspondances)\n\-a rigid_1 \t rigid transform feature #1 (default = 1)\n\-b rigid_2 \t rigid transform feature #2 (default = 2)\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 **P, **Q, **W, **Winv; 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 modelflag = 0; int outflag = 0; int y_flag = 0; int x_flag = 0; /*--- command line defaults ---- */ int rigid_select = 0; int rigid_pt_1 = 1; int rigid_pt_2 = 2; 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 'y': out_nrow = atoi(optarg); y_flag = 1; break; case 'x': out_ncol = atoi(optarg); x_flag = 1; break; case 'r': rigid_select = 1; break; case 'a': rigid_pt_1 = atoi(optarg); rigid_select = 1; break; case 'b': rigid_pt_2 = atoi(optarg); rigid_select = 1; break; case 'v': bgvalue = atof(optarg); break; case '?': errflag = 1; break; } /* command line error check */ if (errflag || !inflag || !listflag || !modelflag) { 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; /* ---- read model file ---- */ P = read_BIN(modelfile, &M, &nfeatures); Q = matrix(1, M, 1, nfeatures); W = matrix(1, 3, 1, 3); Winv = 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); printf("\nNo. of model features = %d\n\n", nfeatures); for (i=1; i<=M; i++) { for (j=1; j<=nfeatures; j++) printf("%3d ", (int) P[i][j]); printf("\n"); } printf("\n"); /* ---- 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")); for (i=1; i<=nfeatures; i++) Q[1][i] = atof(strtok(NULL, " \t")); for (i=1; i<=nfeatures; i++) Q[2][i] = atof(strtok(NULL, " \t")); fprintf(stdout, "%s ", infile); for (i=1; i<=nfeatures; i++) fprintf(stdout,"%3d ", (int) Q[1][i]); for (i=1; i<=nfeatures; i++) fprintf(stdout,"%3d ", (int) Q[2][i]); fprintf(stdout,"\n"); fprintf(stdout,"Affine matrix: \n"); if (rigid_select == 1) rigid(Q, P, nfeatures, W, rigid_pt_1, rigid_pt_2); else affine(Q, P, nfeatures, W); for (i=1; i<=3; i++) { for (j=1; j<=3; j++) fprintf(stdout,"%+3.1f ", W[i][j]); fprintf(stdout,"\n"); } matrix_inverse(W, 3, Winv); /* --- 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,"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(P, 1, M, 1, nfeatures); free_matrix(Q, 1, M, 1, nfeatures); free_matrix(W, 1, 3, 1, 3); free_matrix(Winv, 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 + -