home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_3.5 / bcd / conv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  5.2 KB  |  203 lines

  1. /*
  2.  * This program is used with permission from I. Cox.
  3.  * Please reference:
  4.  * R. A. Boie, I. Cox, Proc. IEEE 1st Int. Conf. Computer Vision,
  5.  * London, 1987, pp. 450-456.
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <malloc.h>
  11. #include "edge_finder.h"
  12.  
  13. extern struct image *my_image;
  14. static int iklim, nf, nf2;
  15. static int iy, ny;
  16. static int nxX2M1, ikXnx, nyX2M1Xnx, nfXnx;
  17. static int iyMnf2Xnx, iy0, iy0Xnx, it;
  18. static int *temp_image, *filter;
  19. static unsigned char *tpicP;
  20.  
  21. void
  22. image_convolve (unsigned char *pic)
  23. {
  24.   register int result, ik, ix, nx;
  25.   register unsigned char *picP;
  26.   register int *filtP, *tmpP, *gaussianP;
  27.  
  28.   nx = my_image->nx;
  29.   ny = my_image->ny;
  30.   nf = my_image->nf;
  31.   nfXnx = nf * nx;
  32.   nf2 = my_image->nf2;
  33.   nxX2M1 = nx + nx - 1;
  34.   nyX2M1Xnx = (ny + ny - 1) * nx;
  35.   filter = my_image->filter;
  36.  
  37. /*
  38.  *    create temp and output arrays
  39.  */
  40.  
  41.   if ((temp_image = (int *) malloc (ny * nx * sizeof (int))) == 0) {
  42.     fprintf (stderr, "error: cannot allocate temp array\n");
  43.     exit (1);
  44.   }
  45.  
  46.   if ((my_image->gaussian = (int *) malloc (ny * nx * sizeof (int))) == 0) {
  47.     fprintf (stderr, "error: cannot allocate gaussian array\n");
  48.     exit (1);
  49.   }
  50. /*
  51.  *    first convolve in x-dirn
  52.  *              do left side first
  53.  *              then middle
  54.  *              then right side
  55.  */
  56.  
  57.  
  58.   tmpP = &temp_image[0];
  59.   picP = pic;
  60.   for (iy = 0; iy < ny; iy++, tmpP += nx, picP += nx) {
  61.     for (ix = 0; ix < nf2; ix++) {
  62.       filtP = filter;
  63.       result = 0;
  64.       ik = ix - nf2;
  65.       iklim = ik + nf;
  66.       for (; ik < iklim; ik++)
  67.         result += (*filtP++) * picP[abs (ik)];
  68.  
  69.       *(tmpP + ix) = result;
  70.     }
  71.   }
  72.  
  73.   tmpP = &temp_image[0];
  74.   tpicP = pic;
  75.   for (iy = 0; iy < ny; iy++, tmpP += nx, tpicP += nx) {
  76.     int xlim = nx - nf2 - 1;
  77.     for (ix = nf2; ix < xlim; ix++) {
  78.       picP = tpicP + ix - nf2;
  79.       filtP = filter;
  80.       result = 0;
  81.       ik = nf;
  82.       while (ik--)
  83.         result += (*filtP++) * (*picP++);
  84.  
  85.       *(tmpP + ix) = result;
  86.     }
  87.   }
  88.  
  89.   tmpP = &temp_image[0];
  90.   picP = pic;
  91.   for (iy = 0; iy < ny; iy++, tmpP += nx, picP += nx) {
  92.     for (ix = nx - nf2 - 1; ix < nx; ix++) {
  93.       filtP = filter;
  94.       result = 0;
  95.       ik = ix - nf2;
  96.       iklim = ik + nf;
  97.       for (; ik < iklim; ik++) {
  98.         /*                      register int index = ix-nf2+ik;
  99.          *                    if(index>=nx) index = nx+nx-index-1;
  100.          *                      result += (*filtP++) * picP[index];
  101.          */
  102.         if (ik >= nx)           /* nxX2M1 = nx+nx-ik-1; */
  103.           result += *filtP * picP[nxX2M1 - ik];
  104.         else
  105.           result += *filtP * picP[ik];
  106.         filtP++;
  107.       }
  108.       *(tmpP + ix) = result;
  109.     }
  110.   }
  111.  
  112. /*
  113.  * then in y-dirn
  114.  *              first top
  115.  *              then middle
  116.  *              then bottom
  117.  */
  118.   it = -nf2 * nx;
  119.   for (ix = 0; ix < nx; ix++) {
  120.     gaussianP = &my_image->gaussian[ix];
  121.     tmpP = &temp_image[ix];
  122.     iy = 0;
  123.     iyMnf2Xnx = it;             /* = (iy - nf2)*nx; */
  124.     for (; iy < nf2; iy++, iyMnf2Xnx += nx) {
  125.       filtP = filter;
  126.       result = 0;
  127.       /*              ik = iy - nf2;
  128.        *            iklim = ik + nf;
  129.        *              for(; ik<iklim; ik++)
  130.        *                      result += (*filtP++) * tmpP[abs(ik)*nx];
  131.        */
  132.       ik = iyMnf2Xnx;
  133.       iklim = ik + nfXnx;
  134.       for (; ik < iklim; ik += nx)
  135.         result += (*filtP++) * tmpP[abs (ik)];
  136.  
  137.       /*              my_image->gaussian[iy*nx+ix] = result/my_image->norm; */
  138.       *(gaussianP) = result / my_image->norm;
  139.       gaussianP += nx;
  140.     }
  141.   }
  142.  
  143.   iy0 = nf2;
  144.   iy0Xnx = iy0 * nx;
  145.   for (ix = 0; ix < nx; ix++) {
  146.     int *col_tmpP = &temp_image[ix];
  147.     int ylim = ny - nf2 - 1;
  148.     iy = iy0;
  149.     gaussianP = &my_image->gaussian[iy0Xnx + ix];
  150.     iyMnf2Xnx = 0;              /* = (iy-nf2)*nx; */
  151.     for (; iy < ylim; iy++, iyMnf2Xnx += nx) {
  152.       filtP = filter;
  153.       tmpP = col_tmpP + iyMnf2Xnx;
  154.       result = 0;
  155.       ik = nf;
  156.       while (ik--) {
  157.         result += (*filtP++) * (*tmpP);
  158.         tmpP += nx;
  159.       }
  160.       /*              my_image->gaussian[iy*nx+ix] = result/my_image->norm; */
  161.       *(gaussianP) = result / my_image->norm;
  162.       gaussianP += nx;
  163.     }
  164.   }
  165.  
  166.   iy0 = ny - nf2 - 1;
  167.   iy0Xnx = iy0 * nx;
  168.   it = (iy0 - nf2) * nx;
  169.   for (ix = 0; ix < nx; ix++) {
  170.     tmpP = &temp_image[ix];
  171.     iy = iy0;
  172.     gaussianP = &my_image->gaussian[iy0Xnx + ix];
  173.     iyMnf2Xnx = it;             /* = (iy-nf2)*nx; */
  174.     for (; iy < ny; iy++, iyMnf2Xnx += nx) {
  175.       filtP = filter;
  176.       result = 0;
  177.       ik = iy - nf2;
  178.       iklim = ik + nf;
  179.       ikXnx = iyMnf2Xnx;
  180.       for (; ik < iklim; ik++) {
  181.         /*              register int index = iyMnf2+ik;
  182.          *            if(index>=ny) index = ny+ny-index-1;
  183.          *              result += (*filtP++) * tmpP[index*nx];
  184.          */
  185.         if (ik >= ny) {         /* nyX2M1Xnx = (ny+ny-1)*nx; */
  186.           result += *filtP * tmpP[nyX2M1Xnx - ikXnx];
  187.         }
  188.         else
  189.           result += *filtP * tmpP[ikXnx];
  190.         filtP++;
  191.         ikXnx += nx;
  192.       }
  193.       /*              my_image->gaussian[iy*nx+ix] = result/my_image->norm; */
  194.       *(gaussianP) = result / my_image->norm;
  195.       gaussianP += nx;
  196.     }
  197.   }
  198.   free (temp_image);
  199. #ifdef DEBUG
  200.   image_Write_int ("gauss", my_image->gaussian);
  201. #endif
  202. }
  203.