home *** CD-ROM | disk | FTP | other *** search
- /*
- * xcorr.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /*
- * XCORR(elate)
- *
- */
-
- #include "xcorr.h"
-
- #define BUFSZ 1024
- extern short tiffInput; /* flag=0 if no ImageIn to set tags; else =1 */
- extern char *optarg;
- extern int optind, opterr;
-
- /*
- * usage of routine
- */
- void
- usage (char *progname)
- {
- progname = last_bs (progname);
- printf ("USAGE: %s inimg templateimg outimg [-L]\n", progname);
- printf ("\n%s finds locations of a pattern within an image by convolving\n", progname);
- printf ("the input image with the pattern (template);\n");
- printf ("the pattern is defined in the template file\n\n");
- printf ("ARGUMENTS:\n");
- printf (" inimg: input image filename (TIF)\n");
- printf (" templateimg: image file containing template (TIF)\n");
- printf (" outimg: output image filename (TIF)\n\n");
- printf ("OPTIONS:\n");
- printf (" -L: print Software License for this module\n");
- exit (1);
- }
-
-
-
- Matrix *
- genTemplate (Image * templp)
- {
- int nx, ny;
- Matrix *retMatrix;
- float *ip, **ipp;
- int ix, iy;
-
- nx = templp->width;
- ny = templp->height;
- if ((retMatrix = (struct Matrix *) calloc (1, sizeof (struct Matrix))) == NULL) {
- printf ("\n...mem allocation for retMatrix failed\n");
- exit (1);
- }
- if ((ipp = (float **) calloc (ny, sizeof (float *))) == NULL) {
- printf ("\n...mem allocation for ipp failed\n");
- exit (1);
- }
- if ((ip = (float *) calloc (ny * nx, sizeof (float))) == NULL) {
- printf ("\n...mem allocation for ip failed\n");
- exit (1);
- }
- for (iy = 0; iy < ny; iy++)
- ipp[iy] = ip + (iy * nx);
- retMatrix->matrix = ipp;
- /*
- * Set template pixels for those values = 255
- */
- for (iy = 0; iy < ny; iy++) {
- for (ix = 0; ix < nx; ix++) {
- if (getpixel (ix, iy, templp))
- ipp[iy][ix] = (float) 1;
- else
- ipp[iy][ix] = (float) 0;
- }
- }
- retMatrix->nRows = ny;
- retMatrix->nCols = nx;
- return (retMatrix);
- }
-
- int
- main (int argc, char *argv[])
- {
-
- Image *imgIn, *imgTempl, *imgOut;
- Matrix *templp;
- int i_arg;
-
- /*
- * cmd line options:
- */
- static char *optstring = "f:g:L";
-
- /*
- * parse command line
- */
- optind = 4;
- opterr = ON; /* give error messages */
-
- if (argc < 4)
- usage (argv[0]);
-
- while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
- switch (i_arg) {
- case 'L':
- print_sos_lic ();
- exit (0);
- default:
- usage (argv[0]);
- break;
- }
- }
- /*
- * read input image
- */
- imgIn = ImageIn (argv[1]);
- if (imgIn->bps == 8 && imgIn->spp == 3) {
- printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
- exit (1);
- }
- /*
- * read template image
- */
- imgTempl = ImageIn (argv[2]);
- if (imgTempl->bps == 8 && imgIn->spp == 3) {
- printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
- exit (1);
- }
- if (imgTempl->width > imgIn->width || imgTempl->height > imgIn->height) {
- printf ("ERROR: Template image dimensions greater than input file!!!\nExiting...\n");
- exit (1);
- }
- templp = genTemplate (imgTempl);
-
- /* reset tiffInput so that we write a grayscale file (i.e tags are not copied) */
- tiffInput = 0;
- /*
- * Allocate memory for output image
- */
- imgOut = ImageAlloc (imgIn->height, imgIn->width, imgIn->bps);
-
- printf ("Doing template matching...\n");
- /*
- * Do the convolution
- */
- convolve (imgIn, imgOut, templp);
- /*
- * Write the output image
- */
- ImageOut (argv[3], imgOut);
- return (0);
- }
-