📄 warp.c
字号:
/*----------------------------------------------------------------------PROGRAM: warp.cDATE: 3/31/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"/* ----------- 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:a:b:c:d:"char *usage = "-i indir -l list -m model.bf -o outdir\n \\t -a in_nrow -b in_ncol [-c out_nrow] [-d out_ncol] \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, char_nrow, char_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 a_flag = 0; int b_flag = 0; int c_flag = 0; int d_flag = 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 'a': nrow = atoi(optarg); a_flag = 1; break; case 'b': ncol = atoi(optarg); b_flag = 1; break; case 'c': out_nrow = atoi(optarg); c_flag = 1; break; case 'd': out_ncol = atoi(optarg); d_flag = 1; break; case '?': errflag = 1; break; } /* command line error check */ if (errflag || !inflag || !listflag || !modelflag || !a_flag || !b_flag) { fprintf(stderr,"\nUSAGE: %s %s\n\n", progname, usage); exit(1); } char_nrow = nrow; char_ncol = ncol; if (c_flag==1) char_nrow = out_nrow; if (d_flag==1) char_ncol = out_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); char_image = cmatrix(1, char_nrow, 1, char_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", infile); exit(1); } nframe = 0; while (strlen(fgets(line, MAX_CHARS, fp)) > 1) nframe++; fclose(fp); if ((fp = fopen(listfile, "r")) == NULL) { fprintf(stderr,"ERROR Could not open input file %s \n\n", infile); exit(1); } fprintf(stderr,"nframe = %d\n\n", nframe); for (f=1; f<=nframe; f++) { fgets(line, 255, fp); strcpy(infile, strtok(line, " ")); for (i=1; i<=nfeatures; i++) Q[1][i] = atof(strtok(NULL, " ")); for (i=1; i<=nfeatures; i++) Q[2][i] = atof(strtok(NULL, " ")); 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"); 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); sprintf(filename,"%s/%s", indir, infile); read_RAW(filename, image_in, nrow, ncol); fprintf(stdout,"Read %d-by-%d input image %s \n", nrow, ncol, infile); affine_warp(image_in, image_out, nrow, ncol, W); fprintf(stdout,"Applied affine warp \n"); for (i=1; i<=char_nrow; i++) for (j=1; j<=char_ncol; j++) char_image[i][j] = (int) image_out[i][j]; sprintf(filename,"%s/%s", outdir, infile); write_RAW(filename, char_image, char_nrow, char_ncol); fprintf(stdout,"Wrote %d-by-%d output file %s\n\n", char_nrow, char_ncol, filename); } 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); free_cmatrix(char_image, 1, char_nrow, 1, char_ncol); fclose(fp); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -