📄 diffuse.c
字号:
/*----------------------------------------------------------------------PROGRAM: diffuse.cDATE: 11/16/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII file of the format filename . . . and applies iterative relaxation using a kernel to diffuse the input image the resulting images are written to the output directory under 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 "convolve.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:f:n:o:t:"char *usage = "\t-i indir -l list -o outdir\n\\t\t[-f filter.bf] [-n iters] [-t out_datatype] \n";char *help="\Image diffusion of a set of images\n\n\-i indir \t input directory\n\-l listfile \t ASCII list of filename in indir to process (one per line)\n\-f filter.bf \t BF-format 2d filter file (default = 3x3 heat equation)\n\-n iterations \t number of iterations (default = 1)\n\-t out_datatype\t output data type: uchar or float (default is same as input)\n\n\This routine will diffuse the input image with the kernel filter.bf\n\using the non-zero pixels as a boundary condition."; #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, M, N; char command[MAX_CHARS],indir[MAX_CHARS],listfile[MAX_CHARS], \ infile[MAX_CHARS], filterfile[MAX_CHARS],outdir[MAX_CHARS], \ filename[MAX_CHARS], line[MAX_CHARS]; char out_datatype[MAX_CHARS]; int Frow, Fcol; float **F; float **image_in, **image_out1, **image_out2; 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 filterflag = 0; int outflag = 0; /* command line defaults */ int in_datatype_float_flag = 0; int out_datatype_float_flag = 0; int out_datatype_select = 0; int num_iterations = 1; 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 'f': strcpy(filterfile, optarg); filterflag = 1; break; case 'o': strcpy(outdir, optarg); outflag = 1; break; case 'n': num_iterations = atoi(optarg); break; case 't': strcpy(out_datatype, optarg); out_datatype_select = 1; break; case '?': errflag = 1; break; } /* command line error check */ if (errflag || !inflag || !outflag || !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 (bytes_pixel==4) out_datatype_float_flag = in_datatype_float_flag = 1; /* default = input */ if (out_datatype_select ==1) if (strncmp(out_datatype, "uchar", 5)==0) out_datatype_float_flag = 0; /* ---- read filter file (or setup default) ---- */ if (filterflag) F = read_BIN(filterfile, &Frow, &Fcol); else { /* setup default Heat Equation kernel */ Frow = Fcol = 3; F = matrix(1, Frow, 1, Fcol); for (i=1; i<=Frow; i++) for (j=1; j<=Fcol; j++) F[i][j] = 0.0; F[1][2] = F[2][1] = F[2][3] = F[3][2] = 0.25; } image_in = matrix(1, nrow, 1, ncol); image_out1 = matrix(1, nrow, 1, ncol); image_out2 = matrix(1, nrow, 1, ncol); if (out_datatype_float_flag == 0) char_image = cmatrix(1, nrow, 1, 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 (fgets(line, MAX_CHARS, fp)) { if (strncmp(line, "#", 1) != 0 && strlen(line)>1) { nframe++; /* ------- read image ------- */ sscanf(line,"%s",infile); sprintf(filename,"%s/%s", indir, infile); if (in_datatype_float_flag>0) read_RAW_float(filename, image_in, nrow, ncol); else read_RAW(filename, image_in, nrow, ncol); fprintf(stdout,"Read %d-by-%d %s input image %s\n", nrow, ncol, (in_datatype_float_flag>0 ? "float":"uchar"), filename); /* ----- diffuse the image iteratively ------ */ for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) image_out1[i][j] = image_in[i][j]; for (k=1; k<=num_iterations; k++) { conv2d(image_out1, image_out2, nrow, ncol, F, Frow, Fcol); /* clean up the borders */ for (i=1; i<=nrow; i++) image_out2[i][1] = image_out2[i][ncol] = 0.0; for (j=1; j<=ncol; j++) image_out2[1][j] = image_out2[nrow][j] = 0.0; /* reset boundary conditions */ for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) { image_out1[i][j] = image_out2[i][j]; if (image_in[i][j] != 0) image_out1[i][j] = image_in[i][j]; } } fprintf(stdout,"Diffused image %s %d times\n",filename, num_iterations); /* ------ process output and write to disk ------ */ sprintf(filename,"%s/%s", outdir, infile); if (out_datatype_float_flag>0) write_RAW_float(filename, image_out1, nrow, ncol); else { for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) char_image[i][j] = (int) image_out1[i][j] + 0.5; write_RAW(filename, char_image, nrow, ncol); } fprintf(stdout,"Wrote %d-by-%d %s output file %s\n\n", nrow, ncol, (out_datatype_float_flag>0 ? "float":"uchar"), filename); } } /* --- write output descriptor file in outdir ------ */ write_descriptor(outdir, nframe, ncol, nrow, (out_datatype_float_flag>0 ? 4:1), comline); fclose(fp); free_matrix(F, 1, Frow, 1, Fcol); free_matrix(image_in, 1, nrow, 1, ncol); free_matrix(image_out1, 1, nrow, 1, ncol); free_matrix(image_out2, 1, nrow, 1, ncol); if (out_datatype_float_flag==0) free_cmatrix(char_image, 1, nrow, 1, ncol); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -