home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / libsrc / h_bclean2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  3.8 KB  |  152 lines

  1.  
  2. /*  bclean.c                                      Brian Tierney,  LBL  3/90
  3.  *
  4.  *  removes small 8-connected objects from binary images.
  5.  *
  6.  *  Note: only recursive version has been converted to HIPS2, should
  7.  *   also convert the non-recursive version, because it is faster. -bt
  8.  */
  9.  
  10. /*   This program is copyright (C) 1990, Regents  of  the
  11. University  of  California.   Anyone may reproduce this software,
  12. in whole or in part, provided that:
  13. (1)  Any copy  or  redistribution  must  show  the
  14.      Regents  of  the  University of California, through its
  15.      Lawrence Berkeley Laboratory, as the source,  and  must
  16.      include this notice;
  17. (2)  Any use of this software must reference this  distribu-
  18.      tion,  state that the software copyright is held by the
  19.      Regents of the University of California, and  that  the
  20.      software is used by their permission.
  21.  
  22.      It is acknowledged that the U.S. Government has  rights
  23. to this software under  Contract DE-AC03-765F00098 between the U.S.
  24. Department of Energy and the University of California.
  25.  
  26.      This software is provided as a professional  academic  contribu-
  27. tion  for  joint exchange.  Thus it is experimental, is pro-
  28. vided ``as is'', with no warranties of any kind  whatsoever,
  29. no  support,  promise  of updates, or printed documentation.
  30. Bug reports or fixes may be sent to the author, who may or may
  31. not act on them as he desires.
  32. */
  33.  
  34. /*   Author:  Brian L. Tierney
  35.  *            Lawrence Berkeley Laboratory
  36.  *            Imaging and Distributed Computing Group
  37.  *            email: bltierney@lbl.gov
  38.  *
  39.  *  converted to HIPS2
  40. */
  41.  
  42. #define Calloc(x,y) (y *)calloc((unsigned)(x), sizeof(y))
  43.  
  44. #include <stdio.h>
  45. #include <hipl_format.h>
  46.  
  47. extern int min_size;
  48.  
  49. byte     *image;
  50. byte     *grid;
  51.  
  52. int       y_begin;
  53. int       y_end;
  54. int       x_begin;
  55. int       x_end;
  56. int       ocol;
  57.  
  58. int       count;
  59.  
  60. int      *str_x;
  61. int      *str_y;
  62.  
  63. h_bclean(hdo)
  64.     struct header *hdo;
  65. {
  66.     int       x, y;
  67.     int       i;
  68.  
  69.     image = hdo->image;
  70.     grid = Calloc(hdo->numpix, byte);
  71.  
  72.     y_begin = hdo->frow;
  73.     y_end = y_begin + hdo->rows;
  74.  
  75.     x_begin = hdo->fcol;
  76.     x_end = x_begin + hdo->cols;
  77.  
  78.     ocol = hdo->ocols;
  79.  
  80.     str_x = Calloc(min_size, int);
  81.     str_y = Calloc(min_size, int);
  82.  
  83.     for (y = y_begin; y < y_end; y++) {
  84.     for (x = x_begin; x < x_end; x++) {
  85.         if (grid[y * ocol + x] == 0 && image[y * ocol + x] > 0) {
  86.         count = 0;
  87.         get_size(x, y);
  88.  
  89.         if (count < min_size)    /* clear a connected area     */
  90.             for (i = 0; i < count; i++)
  91.             image[str_y[i] * ocol + str_x[i]] = 0;
  92.         }
  93.     }
  94.     }
  95.  
  96. }                /* end of  h_bclean (hdo)     */
  97.  
  98. get_size(x, y)
  99.     int       x, y;
  100. {
  101.     int       ax, ay;
  102.  
  103.     grid[y * ocol + x] = 1;
  104.  
  105.     if (count < min_size) {
  106.     str_x[count] = x;
  107.     str_y[count] = y;
  108.     count++;
  109.     }
  110.     ay = y + 1;
  111.     if (ay < y_end && grid[ay * ocol + x] == 0 && image[ay * ocol + x] > 0)
  112.     get_size(x, ay);
  113.  
  114.     ay = y - 1;
  115.     if (ay >= y_begin && grid[ay * ocol + x] == 0 && image[ay * ocol + x] > 0)
  116.     get_size(x, ay);
  117.  
  118.     ax = x + 1;
  119.     if (ax < x_end && grid[y * ocol + ax] == 0 && image[y * ocol + ax] > 0)
  120.     get_size(ax, y);
  121.  
  122.     ax = x - 1;
  123.     if (ax >= x_begin && grid[y * ocol + ax] == 0 && image[y * ocol + ax] > 0)
  124.     get_size(ax, y);
  125.  
  126.     /* diagonals */
  127.     ax = x + 1;
  128.     ay = y + 1;
  129.     if (ay < y_end && ax < x_end && grid[ay * ocol + ax] == 0 && 
  130.     image[ay * ocol + ax] > 0)
  131.     get_size(ax, ay);
  132.  
  133.     ax = x + 1;
  134.     ay = y - 1;
  135.     if (ay >= y_begin && ax < x_end && grid[ay * ocol + ax] == 0 && 
  136.     image[ay * ocol + ax] > 0)
  137.     get_size(ax, ay);
  138.  
  139.     ax = x - 1;
  140.     ay = y + 1;
  141.     if (ay < y_end && ax >= x_begin && grid[ay * ocol + ax] == 0 && 
  142.     image[ay * ocol + ax] > 0)
  143.     get_size(ax, ay);
  144.  
  145.     ax = x - 1;
  146.     ay = y - 1;
  147.     if (ay >= y_begin && ax >= x_begin && grid[ay * ocol + ax] == 0 && 
  148.     image[ay * ocol + ax] > 0)
  149.     get_size(ax, ay);
  150.  
  151. }                /* end of  get_size (x, y)     */
  152.