home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_3.8 / XCORR / XCORR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  3.4 KB  |  156 lines

  1. /* 
  2.  * xcorr.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * XCORR(elate) 
  11.  *
  12.  */
  13.  
  14. #include "xcorr.h"
  15.  
  16. #define BUFSZ 1024
  17. extern short tiffInput;         /* flag=0 if no ImageIn to set tags; else =1 */
  18. extern char *optarg;
  19. extern int optind, opterr;
  20.  
  21. /*
  22.  * usage of routine
  23.  */
  24. void
  25. usage (char *progname)
  26. {
  27.   progname = last_bs (progname);
  28.   printf ("USAGE: %s inimg templateimg outimg [-L]\n", progname);
  29.   printf ("\n%s finds locations of a pattern within an image by convolving\n", progname);
  30.   printf ("the input image with the pattern (template);\n");
  31.   printf ("the pattern is defined in the template file\n\n");
  32.   printf ("ARGUMENTS:\n");
  33.   printf ("       inimg: input image filename (TIF)\n");
  34.   printf (" templateimg: image file containing template (TIF)\n");
  35.   printf ("      outimg: output image filename (TIF)\n\n");
  36.   printf ("OPTIONS:\n");
  37.   printf ("          -L: print Software License for this module\n");
  38.   exit (1);
  39. }
  40.  
  41.  
  42.  
  43. Matrix *
  44. genTemplate (Image * templp)
  45. {
  46.   int nx, ny;
  47.   Matrix *retMatrix;
  48.   float *ip, **ipp;
  49.   int ix, iy;
  50.  
  51.   nx = templp->width;
  52.   ny = templp->height;
  53.   if ((retMatrix = (struct Matrix *) calloc (1, sizeof (struct Matrix))) == NULL) {
  54.     printf ("\n...mem allocation for retMatrix failed\n");
  55.     exit (1);
  56.   }
  57.   if ((ipp = (float **) calloc (ny, sizeof (float *))) == NULL) {
  58.     printf ("\n...mem allocation for ipp failed\n");
  59.     exit (1);
  60.   }
  61.   if ((ip = (float *) calloc (ny * nx, sizeof (float))) == NULL) {
  62.     printf ("\n...mem allocation for ip failed\n");
  63.     exit (1);
  64.   }
  65.   for (iy = 0; iy < ny; iy++)
  66.     ipp[iy] = ip + (iy * nx);
  67.   retMatrix->matrix = ipp;
  68. /*
  69.  * Set template pixels for those values = 255
  70.  */
  71.   for (iy = 0; iy < ny; iy++) {
  72.     for (ix = 0; ix < nx; ix++) {
  73.       if (getpixel (ix, iy, templp))
  74.         ipp[iy][ix] = (float) 1;
  75.       else
  76.         ipp[iy][ix] = (float) 0;
  77.     }
  78.   }
  79.   retMatrix->nRows = ny;
  80.   retMatrix->nCols = nx;
  81.   return (retMatrix);
  82. }
  83.  
  84. int
  85. main (int argc, char *argv[])
  86. {
  87.  
  88.   Image *imgIn, *imgTempl, *imgOut;
  89.   Matrix *templp;
  90.   int i_arg;
  91.  
  92. /* 
  93.  * cmd line options:
  94.  */
  95.   static char *optstring = "f:g:L";
  96.  
  97. /*
  98.  * parse command line
  99.  */
  100.   optind = 4;
  101.   opterr = ON;                  /* give error messages */
  102.  
  103.   if (argc < 4)
  104.     usage (argv[0]);
  105.  
  106.   while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
  107.     switch (i_arg) {
  108.     case 'L':
  109.       print_sos_lic ();
  110.       exit (0);
  111.     default:
  112.       usage (argv[0]);
  113.       break;
  114.     }
  115.   }
  116. /*
  117.  * read input image
  118.  */
  119.   imgIn = ImageIn (argv[1]);
  120.   if (imgIn->bps == 8 && imgIn->spp == 3) {
  121.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  122.     exit (1);
  123.   }
  124. /*
  125.  * read template image
  126.  */
  127.   imgTempl = ImageIn (argv[2]);
  128.   if (imgTempl->bps == 8 && imgIn->spp == 3) {
  129.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  130.     exit (1);
  131.   }
  132.   if (imgTempl->width > imgIn->width || imgTempl->height > imgIn->height) {
  133.     printf ("ERROR: Template image dimensions greater than input file!!!\nExiting...\n");
  134.     exit (1);
  135.   }
  136.   templp = genTemplate (imgTempl);
  137.  
  138.   /* reset tiffInput so that we write a grayscale file (i.e tags are not copied) */
  139.   tiffInput = 0;
  140. /*
  141.  * Allocate memory for output image
  142.  */
  143.   imgOut = ImageAlloc (imgIn->height, imgIn->width, imgIn->bps);
  144.  
  145.   printf ("Doing template matching...\n");
  146. /*
  147.  * Do the convolution
  148.  */
  149.   convolve (imgIn, imgOut, templp);
  150. /*
  151.  * Write the output image
  152.  */
  153.   ImageOut (argv[3], imgOut);
  154.   return (0);
  155. }
  156.