⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver.c

📁 两幅图像匹配算法
💻 C
字号:
/* * * $Header: /usr/u/wjr/vision/scale-h/RCS/driver.c,v 1.20 1994/09/01 20:43:31 wjr Exp $ * * Copyright (c) 1990, 1991, 1992, 1993 Cornell University.  All Rights * Reserved.   *   * Copyright (c) 1991, 1992 Xerox Corporation.  All Rights Reserved.   *   * Use, reproduction, preparation of derivative works, and distribution * of this software is permitted.  Any copy of this software or of any * derivative work must include both the above copyright notices of * Cornell University and Xerox Corporation and this paragraph.  Any * distribution of this software or derivative works must comply with all * applicable United States export control laws.  This software is made * available AS IS, and XEROX CORPORATION DISCLAIMS ALL WARRANTIES, * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, * AND NOTWITHSTANDING ANY OTHER PROVISION CONTAINED HEREIN, ANY * LIABILITY FOR DAMAGES RESULTING FROM THE SOFTWARE OR ITS USE IS * EXPRESSLY DISCLAIMED, WHETHER ARISING IN CONTRACT, TORT (INCLUDING * NEGLIGENCE) OR STRICT LIABILITY, EVEN IF XEROX CORPORATION IS ADVISED * OF THE POSSIBILITY OF SUCH DAMAGES. */static char rcsid[] = "@(#)$Header: /usr/u/wjr/vision/scale-h/RCS/driver.c,v 1.20 1994/09/01 20:43:31 wjr Exp $";/* * This file drives the scale-h code: it reads in options, images, models * etc, runs the code, and prints out the matches. */#include <stdio.h>#include "misc.h"#include "scale-h.h"#include "list.h"#include <values.h>#define	LINELEN	256static char usage[] = "usage: %s [-onematch | -bd | -bf | -evaluate]\n\	[-allborders] [-metric] [-hillclimb]\n\	[-forward thresh frac] [-reverse thresh frac]\n\	[-scale minxscale minyscale maxskew]\n\	[-params scale offset]\n\	[-Scalerange maxxscale maxyscale]\n\	imagepoints [modelpoints...] [< matches]\n";intmain(int argc, char **argv){    char *image_fname;    char *model_fname;    FILE *image_file;    FILE *model_file;    simage_info image;    smodel_info *models;    unsigned nmodels;    ListNode ln;    stransval *t;    stransval tin;    int i;    char *av0 = argv[0];    double image_frac = 1.;    double model_frac = 1.;    long image_thresh = 100;    long model_thresh = 100;    double minscalex = 0.3;    double minscaley = 0.3;    double maxscalex = 1.0;    double maxscaley = 1.0;    double maxskew = 3.;    boolean onematch = FALSE;    boolean bestdist = FALSE;    boolean bestfrac = FALSE;    boolean hillclimb = FALSE;    boolean gotparams = FALSE;    boolean useborders = FALSE;    boolean realmetric = FALSE;    boolean doeval = FALSE;    int evalcount = 0;    boolean sawfirst = FALSE;    char line[LINELEN];    double reverse_scale = 0.;    double reverse_offset = 0.;    int forwstyle = 0;    int revstyle = 0;    /* Parse the command line */    while ((argc >= 2) && (argv[1][0] == '-')) {	switch(argv[1][1]) {	  case 'o': {	    /* Only find a single match */	    onematch = TRUE;	    argc--;	    argv++;	    break;	    }	  case 'b': {	    /* Optimise in some way */	    if (argv[1][2] == 'd') {		bestdist = TRUE;		}	    else if (argv[1][2] == 'f') {		bestfrac = TRUE;		}	    else {		fprintf(stderr, usage, av0);		exit(1);		}	    argc--;	    argv++;	    break;	    }	  case 'a': {	    /* Enable borders */	    useborders = TRUE;	    argc--;	    argv++;	    break;	    }	  case 'h': {	    /* Enable hillclimbing */	    hillclimb = TRUE;	    argc--;	    argv++;	    break;	    }	  case 'm': {	    /* Use the real metric */	    realmetric = TRUE;	    argc--;	    argv++;	    break;	    }	  case 'f': {	    /* Forward parameters */	    if (argc < 4) {		fprintf(stderr, usage, av0);		exit(1);		}	    model_thresh = atoi(argv[2]);	    model_frac = atof(argv[3]);	    argc -= 3;	    argv += 3;	    break;	    }	  case 'r': {	    /* Reverse parameters */	    if (argc < 4) {		fprintf(stderr, usage, av0);		exit(1);		}	    image_thresh = atoi(argv[2]);	    image_frac = atof(argv[3]);	    argc -= 3;	    argv += 3;	    break;	    }	  case 's': {	    /* Scale parameters */	    if (argc < 5) {		fprintf(stderr, usage, av0);		exit(1);		}	    minscalex = atof(argv[2]);	    minscaley = atof(argv[3]);	    maxskew = atof(argv[4]);	    argc -= 4;	    argv += 4;	    break;	    }	  case 'S': {	    /* Max scale parameters */	    if (argc < 4) {		fprintf(stderr, usage, av0);		exit(1);		}	    maxscalex = atof(argv[2]);	    maxscaley = atof(argv[3]);	    argc -= 3;	    argv += 3;	    break;	    }	  case 'p': {	    /* Adaptive reverse parameters */	    if (argc < 4) {		fprintf(stderr, usage, av0);		exit(1);		}	    gotparams = TRUE;	    reverse_scale = atof(argv[2]);	    reverse_offset = atof(argv[3]);	    argc -= 3;	    argv += 3;	    break;	    }	  case 'e': {	    /* Evaluate matches presented on stdin */	    doeval = TRUE;	    argc--;	    argv++;	    break;	    }	  	  default: {	    fprintf(stderr, usage, av0);	    exit(1);	    break;	    }	    	    }	}    if (argc < 2) {	fprintf(stderr, usage, av0);	exit(1);	}    image_fname = argv[1];    nmodels = argc - 2;    image_file = fopen(image_fname, "r");    if (image_file == (FILE *)NULL) {	perror(image_fname);	exit(1);	}    image.pts = read_pts(image_file, &image.xsize, &image.ysize, &image.npts,			 &image.im);    if (image.pts == (point *)NULL) {	fprintf(stderr, "can't read data from %s\n", image_fname);	exit(1);	}    (void)fclose(image_file);    clear_siminfo(&image);    models = (smodel_info *)malloc(nmodels * sizeof(smodel_info));    if (models == (smodel_info *)NULL) {	fprintf(stderr, "can't allocate model info\n");	exit(1);	}    /*     * For best* modes, the offset should default to the affected parameter,     * since in that case the initial image_thresh or image_frac is ignored.     */    if ((!gotparams) && bestdist) {	reverse_offset = image_thresh;	}    else if ((!gotparams) && bestfrac) {	reverse_offset = image_frac;	}    else if (gotparams) {	if ((!bestdist) && (!bestfrac)) {	    fprintf(stderr, "adaptive parameters only apply to best* modes\n");	    exit(1);	    }	}    for (i = 0; i < nmodels; i++) {	model_fname = argv[i + 2];	model_file = fopen(model_fname, "r");	if (model_file == (FILE *)NULL) {	    perror(model_fname);	    exit(1);	    }	models[i].pts =	    read_pts(model_file, &(models[i].xsize), &(models[i].ysize),		     &(models[i].npts), &(models[i].im));	if (models[i].pts == (point *)NULL) {	    fprintf(stderr, "can't read data from %s\n", model_fname);	    exit(1);	    }	(void)fclose(model_file);	shuffle_pts(models[i].pts, models[i].npts);	models[i].model_frac = model_frac;	models[i].model_thresh = model_thresh;	models[i].image_frac = image_frac;	models[i].image_thresh = image_thresh;	models[i].mintransx = models[i].mintransy = -MAXINT;	models[i].maxtransx = models[i].maxtransy = MAXINT;	models[i].minscalex = minscalex;	models[i].minscaley = minscaley;	models[i].maxscalex = maxscalex;	models[i].maxscaley = maxscaley;	models[i].maxskew = maxskew;	models[i].reverse_scale = reverse_scale;	models[i].reverse_offset = reverse_offset;	if (useborders) {	    models[i].leftborder = models[i].rightborder = models[i].xsize - 1;	    models[i].topborder = models[i].bottomborder = models[i].ysize - 1;	    }	else {	    models[i].leftborder = models[i].rightborder = 0;	    models[i].topborder = models[i].bottomborder = 0;	    }	models[i].stepx = models[i].stepy = 1;	clear_smodinfo(&models[i]);	}    if (model_thresh < 0) {	bestdist = TRUE;	}    if (model_frac < 0) {	bestfrac = TRUE;	}    /* Figure out the forward matching style */    if (bestdist) {	if (onematch || bestfrac || hillclimb || doeval) {	    fprintf(stderr, usage, av0);	    exit(1);	    }	forwstyle = FORWARD_BESTDIST;	}    else if (bestfrac) {	if (onematch || hillclimb || doeval) {	    fprintf(stderr, usage, av0);	    exit(1);	    }	forwstyle = FORWARD_BESTFRAC;	}    else if (onematch) {	if (hillclimb || doeval) {	    fprintf(stderr, usage, av0);	    exit(1);	    }	forwstyle = FORWARD_ONE;	}    else {	forwstyle = FORWARD_ALL;	}    if (hillclimb) {	forwstyle |= FORWARD_HILLCLIMB;	}    /* Figure out the reverse matching style */    if (image_thresh < 0) {	revstyle = REVERSE_NONE;	if (realmetric) {	    fprintf(stderr, "can't use both -metric and reverse deactivated\n");	    exit(1);	    }	}    else {	if (realmetric) {	    revstyle = REVERSE_ALLIMAGE;	    }	else {	    revstyle = REVERSE_BOX;	    }	}    /* Are we evaluating matches from stdin? */    if (doeval) {	/*	 * Read them in. Lines starting "Trans" switch to the next file.	 * Exception: if the first line starts that way, ignore it (this means	 * that if we're only doing a single eval, we don't need to stick	 * that line in).	 */	while (fgets(line, LINELEN, stdin) != (char *)NULL) {	    evalcount = 0;	    if (!strncmp(line, "Trans", 5)) {		if ((evalcount == 0) && (!sawfirst)) {		    sawfirst = TRUE;		    }		else {		    evalcount++;		    if (evalcount >= nmodels) {			fprintf(stderr, "more evaluation blocks than models\n");			exit(1);			}		    }		}	    else {		sscanf(line, "(%ld, %ld) (%lf, %lf)", &tin.transpos.x,		       &tin.transpos.y, &tin.scalex, &tin.scaley);		t = malloc_strans();		if (t == (stransval *)NULL) {		    fprintf(stderr, "can't alloc strans\n");		    exit(1);		    }		*t = tin;				if (models[evalcount].trans == NULLLIST) {		    models[evalcount].trans = liNew();		    if (models[evalcount].trans == NULLLIST) {			fprintf(stderr, "can't alloc list\n");			exit(1);			}		    }		ln = liAdd(models[evalcount].trans, (void *)t);		if (ln == NULLLISTNODE) {		    fprintf(stderr, "can't alloc listnode\n");		    exit(1);		    }		}	    }	}    findSTransAllModels(&image, models, nmodels, forwstyle, revstyle);    for (i = 0; i < nmodels; i++) {	if (models[i].trans == NULLLIST) {	    fprintf(stderr, "can't generate trans for \"%s\"\n", argv[i + 2]);	    exit(1);	    }	printf("Translations for \"%s\"\n", argv[i + 2]);	foreach (ln, models[i].trans) {	    t = (stransval *)liGet(ln);	    printf("(%4ld, %4ld) (%f, %f) = (%ld, %g), (%ld, %g)\n",		   t->transpos.x, t->transpos.y, t->scalex, t->scaley,		   t->forward_val, t->forward_frac,		   t->reverse_val, t->reverse_frac);	    }	free_smodinfo_bits(&models[i]);	free((void *)models[i].pts);	imFree(models[i].im);	}    free((void *)models);    free_siminfo_bits(&image);    free((void *)image.pts);    imFree(image.im);    return(0);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -