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 / det.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  8.8 KB  |  418 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.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <malloc.h>
  12. #include "edge_finder.h"
  13.  
  14.  
  15. extern struct image *my_image;
  16.  
  17. void
  18. image_detx ()
  19. {
  20.   register int *dP, *gP;
  21.   register int ix, iy;
  22.   register int nx, ny, nxM1;
  23.  
  24.   nx = my_image->nx;
  25.   nxM1 = nx - 1;
  26.   ny = my_image->ny;
  27.  
  28.   if ((my_image->idx = (int *) malloc (ny * nx * sizeof (int))) == 0) {
  29.     fprintf (stderr, "error: cannot allocate idx array\n");
  30.     exit (1);
  31.   }
  32.  
  33.   /*
  34.    * Left Col/side
  35.    */
  36.   dP = my_image->idx;
  37.   gP = my_image->gaussian;
  38.   for (iy = 0; iy < ny; iy++) {
  39. /*              d[iy*nx] = g[iy*nx] - g[iy*nx+1]; */
  40.     *dP = *(gP + 1) - *(gP);
  41.     dP += nx;
  42.     gP += nx;
  43.   }
  44.  
  45.   /*
  46.    *  Middle Cols
  47.    */
  48.   for (iy = 0; iy < ny; iy++) {
  49.     dP = my_image->idx + iy * nx + 1;
  50.     gP = my_image->gaussian + iy * nx + 2;
  51.     for (ix = 1; ix < nxM1; ix++) {
  52. /*                      d[ix] = g[ix+1] - g[ix-1]; */
  53.       *dP = *gP - *(gP - 2);
  54.       dP++;
  55.       gP++;
  56.     }
  57.   }
  58.  
  59.   /*
  60.    * Right Col/Side
  61.    */
  62.   dP = my_image->idx + nxM1;
  63.   gP = my_image->gaussian + nxM1;
  64.   for (iy = 0; iy < ny; iy++) {
  65. /*              d[iy*nx+nx-1] = g[iy*nx+nx-1] - g[iy*nx+nx-2]; */
  66.     *dP = *gP - *(gP - 1);
  67.     dP += nx;
  68.     gP += nx;
  69.   }
  70.  
  71.  
  72. #ifdef DEBUG
  73.   image_Write_int ("detx", my_image->idx);
  74. #endif
  75. }
  76.  
  77. void
  78. image_dety ()
  79. {
  80.   register int *dP, *gP;
  81.   register int ix, iy, nx, ny, nyM1, nxPnx;
  82.  
  83.   nx = my_image->nx;
  84.   nxPnx = nx + nx;
  85.   ny = my_image->ny;
  86.   nyM1 = ny - 1;
  87.  
  88.   if ((my_image->idy = (int *) malloc (ny * nx * sizeof (int))) == 0) {
  89.     fprintf (stderr, "error: cannot allocate idy array\n");
  90.     exit (1);
  91.   }
  92.  
  93.  
  94.   /*
  95.    * Top Row
  96.    */
  97.   dP = my_image->idy;
  98.   gP = my_image->gaussian;
  99.   for (ix = 0; ix < nx; ix++) {
  100.     dP[ix] = gP[ix + nx] - gP[ix];
  101. /*              *dP = *gP - *(gP+nx);   */
  102. /*              dP++; gP++;     */
  103.   }
  104.  
  105.   /*
  106.    * Middle Rows
  107.    */
  108.   for (ix = 0; ix < nx; ix++) {
  109.     dP = my_image->idy + ix;
  110.     gP = my_image->gaussian + ix;
  111.     for (iy = 1; iy < nyM1; iy++) {
  112.       dP += nx;
  113.       *dP = *(gP + nxPnx) - *gP;
  114.       gP += nx;
  115.     }
  116.   }
  117.  
  118.   /*
  119.    * Bottom Row
  120.    */
  121.   dP = my_image->idy + (ny - 1) * nx;
  122.   gP = my_image->gaussian + (ny - 1) * nx;
  123.   for (ix = 0; ix < nx; ix++) {
  124.     dP[ix] = gP[ix] - gP[ix - nx];
  125. /*              *dP = *gP - *(gP-nx);   */
  126. /*              dP++; gP++; */
  127.   }
  128.  
  129. #ifdef DEBUG
  130.   image_Write_int ("dety", my_image->idy);
  131. #endif
  132. }
  133.  
  134.  
  135.  
  136. void
  137. image_det45 ()
  138. {
  139.   register int *dP, *gP;
  140.   register int i, ix, iy, nx, index, ny;
  141.   register nxP1, nxM1, nyM1;
  142.  
  143.   nx = my_image->nx;
  144.   nxM1 = nx - 1;
  145.   nxP1 = nx + 1;
  146.   ny = my_image->ny;
  147.   nyM1 = ny - 1;
  148.  
  149.   if ((my_image->id45 = (int *) malloc (ny * nx * sizeof (int))) == 0) {
  150.     fprintf (stderr, "error: cannot allocate id45 array\n");
  151.     exit (1);
  152.   }
  153.  
  154.   /*
  155.    * Top row
  156.    */
  157.   dP = my_image->id45 + nx - 2;
  158.   gP = my_image->gaussian + nx - 2;
  159.   for (ix = nx - 2; ix > 0; ix--) {
  160. /*              d[ix] = g[ix] - g[ix+nx+1]; */
  161.     *dP = *(gP + nxM1) - *(gP + 1);
  162.     dP--;
  163.     gP--;
  164.   }
  165.  
  166.   /*
  167.    * Upper Diagonal
  168.    */
  169.   dP = my_image->id45;
  170.   gP = my_image->gaussian;
  171.   for (i = nx - 2; i > 0; i--) {
  172.     ix = i;
  173.     iy = 1;
  174.     index = nx + ix;
  175.     while (ix > -1 && iy < nyM1) {
  176. /*                      dP[iy*nx+ix] = gP[(iy+1)*nx+ix-1]-gP[(iy-1)*nx+ix+1]; */
  177. /*                      dP[iy*nx+ix] = gP[iy*nx+ix+nx-1] -gP[iy*nx+ix-nx+1];  */
  178.       dP[index] = gP[index + nxM1] - gP[index - nxM1];
  179.       index += nxM1;
  180.       ix--;
  181.       iy++;
  182.     }
  183.   }
  184.  
  185.   /*
  186.    * Lower Diagonal
  187.    */
  188.   for (i = 1; i < ny; i++) {
  189.     ix = nx - 2;
  190.     iy = i;
  191.     index = iy * nx + ix;
  192.     while (ix > 0 && iy < nyM1) {
  193. /*                      dP[iy*nx+ix] = gP[(iy+1)*nx+ix-1]-gP[(iy-1)*nx+ix+1]; */
  194. /*                      dP[iy*nx+ix] = gP[iy*nx+ix+nx-1] -gP[iy*nx+ix-nx+1];  */
  195.       dP[index] = gP[index + nxM1] - gP[index - nxM1];
  196.       index += nxM1;
  197.       ix--;
  198.       iy++;
  199.     }
  200.   }
  201.  
  202.  
  203.   /*
  204.    * Left Middle Side
  205.    */
  206.   dP = my_image->id45 + nx + nx;
  207.   gP = my_image->gaussian + nx + nx;
  208.   for (iy = 1; iy < nyM1; iy++) {
  209.     *dP = *(gP + nx) - *(gP - nxM1);
  210.   }
  211.  
  212.  
  213.   /*
  214.    * Right Middle Side
  215.    */
  216.   dP = my_image->id45 + nx + nx - 1;
  217.   gP = my_image->gaussian + nx + nx - 1;
  218.   for (iy = 1; iy < nyM1; iy++) {
  219. /*              d[iy*nx+nx-1] = g[iy*nx+nx-1] - g[(iy-1)*nx+nx-2]; */
  220. /*              d[iy*nx+nx-1] = g[iy*nx+nx-1] - g[iy*nx+nx-1-nx-1]; */
  221.     *dP = *(gP + nxM1) - *(gP - nx);
  222.     dP += nx;
  223.     gP += nx;
  224.   }
  225.  
  226.   /*
  227.    * Bottom Row
  228.    */
  229.   dP = my_image->id45 + (ny - 1) * nx;
  230.   gP = my_image->gaussian + (ny - 1) * nx;
  231.   for (ix = 1; ix < nxM1; ix++) {
  232. /*              dP[(ny-1)*nx+ix] = gP[(ny-1)*nx+ix] - gP[(ny-2)*nx+ix+1];    */
  233. /*              dP[(ny-1)*nx+ix] = gP[(ny-1)*nx+ix] - gP[(ny-1)*nx+ix-nx+1]; */
  234.     *dP = *(gP - 1) - *(gP - nxM1);
  235.     dP++;
  236.     gP++;
  237.   }
  238.  
  239.   /*
  240.    * Now the top left Corner
  241.    */
  242.   dP = my_image->id45;
  243.   gP = my_image->gaussian;
  244.   *dP = *(gP + nx) - *(gP + 1);
  245.  
  246.   /*
  247.    * Now th bottom left corner
  248.    */
  249.   dP = my_image->id45 + (ny - 1) * nx;
  250.   gP = my_image->gaussian + (ny - 1) * nx;
  251. /*      dP[(ny-1)*nx+nx-1] = gP[(ny-1)*nx+nx-1] - gP[(ny-2)*nx+nx-2];      */
  252. /*      dP[(ny-1)*nx+nx-1] = gP[(ny-1)*nx+nx-1] - gP[(ny-1)*nx+nx-1-nx-1]; */
  253.   *dP = *(gP) - *(gP - nxM1);
  254.  
  255.   /*
  256.    * Now the bottom right corner
  257.    */
  258.   dP[nxM1] = *(gP + nxM1 - 1) - *(gP - 1);
  259.  
  260.  
  261.   /*
  262.    * Now the top right Corner
  263.    */
  264.   dP = my_image->id45 + nxM1;
  265.   gP = my_image->gaussian + nxM1;
  266.   *dP = *(gP + nxM1) - *(gP);
  267.  
  268. #ifdef DEBUG
  269.   image_Write_int ("det45", my_image->id45);
  270. #endif
  271. }
  272.  
  273.  
  274.  
  275.  
  276. void
  277. image_det135 ()
  278. {
  279.   register int *dP, *gP;
  280.   register int i, ix, iy, nx, index, ny;
  281.   register nxP1, nxM1, nyM1, nxM2;
  282.  
  283.   nx = my_image->nx;
  284.   nxM1 = nx - 1;
  285.   nxP1 = nx + 1;
  286.   nxM2 = nx - 2;
  287.   ny = my_image->ny;
  288.   nyM1 = ny - 1;
  289.  
  290.   if ((my_image->id135 = (int *) malloc (ny * nx * sizeof (int))) == 0) {
  291.     fprintf (stderr, "error: cannot allocate id135 array\n");
  292.     exit (1);
  293.   }
  294.  
  295.   /*
  296.    * Top Middle Row
  297.    */
  298.   dP = my_image->id135 + 1;
  299.   gP = my_image->gaussian + 1;
  300.   for (ix = 1; ix < nxM1; ix++) {
  301. /*              d[ix] = g[ix] - g[ix+nx-1]; */
  302.     *dP = *(gP + nxP1) - *(gP - 1);
  303.     dP++;
  304.     gP++;
  305.   }
  306.  
  307.   /*
  308.    * Upper Middle Diagonal
  309.    */
  310.   dP = my_image->id135;
  311.   gP = my_image->gaussian;
  312.   for (i = 1; i < nxM1; i++) {
  313.     ix = i;
  314.     iy = 1;
  315.     index = nx + ix;
  316.     while (ix < nxM1 && iy < nyM1) {
  317. /*                      dP[iy*nx+ix] = gP[(iy+1)*nx+ix+1]-gP[(iy-1)*nx+ix-1]; */
  318. /*                      dP[iy*nx+ix] = gP[iy*nx+ix+nx+1]-gP[iy*nx+ix-nx-1];  */
  319.       dP[index] = gP[index + nxP1] - gP[index - nxP1];
  320.       index += nxP1;
  321.       ix++;
  322.       iy++;
  323.     }
  324.   }
  325.  
  326.  
  327.   /*
  328.    * Lower Middle Diagonal
  329.    */
  330.   for (i = 1; i < ny; i++) {
  331.     ix = 1;
  332.     iy = i;
  333.     index = iy * nx + ix;
  334.     while (ix < nxM1 && iy < nyM1) {
  335. /*                      dP[iy*nx+ix] = gP[(iy+1)*nx+ix+1]- gP[(iy-1)*nx+ix-1]; */
  336. /*                      dP[iy*nx+ix] = gP[iy*nx+ix+nx+1] - gP[iy*nx+ix-nx-1]; */
  337.       dP[index] = gP[index + nxP1] - gP[index - nxP1];
  338.       index += nxP1;
  339.       ix++;
  340.       iy++;
  341.     }
  342.   }
  343.  
  344.   /*
  345.    * Left Middle Side
  346.    */
  347.   dP = my_image->id135 + nx;
  348.   gP = my_image->gaussian + nx;
  349.   for (iy = 1; iy < nyM1; iy++) {
  350. /*              d[iy*nx] = g[iy*nx] - g[(iy-1)*nx+2]; */
  351. /*              d[iy*nx] = g[iy*nx] - g[iy*nx-nx+2]; */
  352.     *dP = *(gP + nxP1) - *(gP - nx);
  353.     dP += nx;
  354.     gP += nx;
  355.   }
  356.  
  357.   /*
  358.    * Right Middle Side
  359.    */
  360.   dP = my_image->id135 + nx + nx - 1;
  361.   gP = my_image->gaussian + nx + nx - 1;
  362.   for (iy = 1; iy < nyM1; iy++) {
  363. /*              d[iy*nx+nx-1] = g[iy*nx+nx-1] - g[(iy-1)*nx+nx-2];  */
  364. /*              d[iy*nx+nx-1] = g[iy*nx+nx-1] - g[iy*nx+nx-1-nx-1]; */
  365.     *dP = *(gP + nx) - *(gP - nxP1);
  366.     dP += nx;
  367.     gP += nx;
  368.   }
  369.  
  370.   /*
  371.    * Bottom Middle Row
  372.    */
  373.   dP = my_image->id135 + (ny - 1) * nx + 1;
  374.   gP = my_image->gaussian + (ny - 1) * nx + 1;
  375.   for (ix = 1; ix < nxM1; ix++) {
  376. /*              d[(ny-1)*nx+ix] = g[(ny-1)*nx+ix] - g[(ny-2)*nx+ix-1];    */
  377. /*              d[(ny-1)*nx+ix] = g[(ny-1)*nx+ix] - g[(ny-1)*nx+ix-nx-1]; */
  378.     *dP = *(gP + 1) - *(gP - nxP1);
  379.     dP++;
  380.     gP++;
  381.   }
  382.  
  383.  
  384.   /*
  385.    * Now the top left Corner
  386.    */
  387.   dP = my_image->id45;
  388.   gP = my_image->gaussian;
  389.   *dP = *(gP + nxP1) - *(gP);
  390.  
  391.   /*
  392.    * Now th bottom left corner
  393.    */
  394.   dP = my_image->id45 + (ny - 1) * nx;
  395.   gP = my_image->gaussian + (ny - 1) * nx;
  396. /*      dP[(ny-1)*nx+nx-1] = gP[(ny-1)*nx+nx-1] - gP[(ny-2)*nx+nx-2];      */
  397. /*      dP[(ny-1)*nx+nx-1] = gP[(ny-1)*nx+nx-1] - gP[(ny-1)*nx+nx-1-nx-1]; */
  398.   *dP = *(gP + 1) - *(gP - nx);
  399.  
  400.   /*
  401.    * Now the bottom right corner
  402.    */
  403.   dP[nxM1] = *(gP + nxM1) - *(gP - 2);
  404.  
  405.  
  406.   /*
  407.    * Now the top right Corner
  408.    */
  409.   dP = my_image->id45 + nxM1;
  410.   gP = my_image->gaussian + nxM1;
  411.   *dP = *(gP + nx) - *(gP - 1);
  412.  
  413.  
  414. #ifdef DEBUG
  415.   image_Write_int ("det135", my_image->id135);
  416. #endif
  417. }
  418.