home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_08 / 1108064a < prev    next >
Text File  |  1993-06-07  |  6KB  |  236 lines

  1. short edmask1[3][3] = {{0, 1, 0},
  2.                        {0, 1, 0},
  3.                        {0, 1, 0}};
  4.  
  5. short edmask2[3][3] = {{0, 0, 0},
  6.                        {1, 1, 1},
  7.                        {0, 0, 0}};
  8.  
  9. short edmask3[3][3] = {{0, 1, 0},
  10.                        {1, 1, 1},
  11.                        {0, 1, 0}};
  12.  
  13. short edmask4[3][3] = {{1, 1, 1},
  14.                        {1, 1, 1},
  15.                        {1, 1, 1}};
  16.  
  17.  
  18.  
  19.  
  20.      /*******************************************
  21.      *
  22.      *   mask_dilation(...
  23.      *
  24.      *   This function performs the dilation
  25.      *   operation using the erosion-dilation
  26.      *   3x3 masks given above.  It works on
  27.      *   0-value images.
  28.      *
  29.      *******************************************/
  30.  
  31. mask_dilation(in_name, out_name, the_image, out_image,
  32.               il, ie, ll, le, value, mask_type)
  33.    char   in_name[], out_name[];
  34.    int    il, ie, ll, le;
  35.    short  the_image[ROWS][COLS],
  36.           out_image[ROWS][COLS],
  37.           mask_type, value;
  38. {
  39.    int    a, b, count, i, j, k;
  40.    int    length, width;
  41.    struct tiff_header_struct image_header;
  42.    short  mask[3][3], max;
  43.  
  44.       /**************************************
  45.       *
  46.       *   Copy the 3x3 erosion-dilation mask
  47.       *   specified by the mask_type.
  48.       *
  49.       ***************************************/
  50.  
  51.    switch(mask_type){
  52.       case 1:
  53.          copy_3_x_3(mask, edmask1);
  54.          break;
  55.       case 2:
  56.          copy_3_x_3(mask, edmask2);
  57.          break;
  58.       case 3:
  59.          copy_3_x_3(mask, edmask3);
  60.          break;
  61.       case 4:
  62.          copy_3_x_3(mask, edmask4);
  63.          break;
  64.       default:
  65.          printf("\nInvalid mask type, using mask 4");
  66.          copy_3_x_3(mask, edmask4);
  67.          break;
  68.    }
  69.  
  70.    if(does_not_exist(out_name)){
  71.       printf("\n\n output file does not exist %s", out_name);
  72.       read_tiff_header(in_name, &image_header);
  73.       round_off_image_size(&image_header,
  74.                            &length, &width);
  75.       image_header.image_length = length*ROWS;
  76.       image_header.image_width  = width*COLS;
  77.       create_allocate_tiff_file(out_name, &image_header,
  78.                                 out_image);
  79.    }  /* ends if does_not_exist */
  80.  
  81.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  82.  
  83.       /***************************
  84.       *
  85.       *   Loop over image array
  86.       *
  87.       ****************************/
  88.  
  89.    printf("\n");
  90.  
  91.    for(i=1; i<ROWS-1; i++){
  92.       if( (i%10) == 0) printf("%3d", i);
  93.       for(j=1; j<COLS-1; j++){
  94.          max = 0;
  95.          for(a=-1; a<=1; a++){
  96.              for(b=-1; b<=1; b++){
  97.                 if(mask[a+1][b+1] == 1){
  98.                    if(the_image[i+a][j+b] > max)
  99.                       max = the_image[i+a][j+b];
  100.                 }  /* ends if mask == 1 */
  101.              }  /*  ends loop over b */
  102.          }  /* ends loop over a */
  103.          out_image[i][j] = max;
  104.       }  /* ends loop over j */
  105.    }  /* ends loop over i */
  106.  
  107.    fix_edges(out_image, 3);
  108.  
  109.    write_array_into_tiff_image(out_name, out_image,
  110.                                il, ie, ll, le);
  111.  
  112. }  /* ends mask_dilation */
  113.  
  114.  
  115.  
  116.  
  117.  
  118.      /*******************************************
  119.      *
  120.      *   mask_erosion(...
  121.      *
  122.      *   This function performs the erosion
  123.      *   operation using the erosion-dilation
  124.      *   3x3 masks given above.  It works on
  125.      *   0-value images.
  126.      *
  127.      *******************************************/
  128.  
  129. mask_erosion(in_name, out_name, the_image, out_image,
  130.              il, ie, ll, le, value, mask_type)
  131.    char   in_name[], out_name[];
  132.    int    il, ie, ll, le;
  133.    short  the_image[ROWS][COLS],
  134.           out_image[ROWS][COLS],
  135.           mask_type, value;
  136. {
  137.    int    a, b, count, i, j, k;
  138.    int    length, width;
  139.    struct tiff_header_struct image_header;
  140.    short  mask[3][3], min;
  141.  
  142.       /**************************************
  143.       *
  144.       *   Copy the 3x3 erosion-dilation mask
  145.       *   specified by the mask_type.
  146.       *
  147.       ***************************************/
  148.  
  149.    switch(mask_type){
  150.       case 1:
  151.          copy_3_x_3(mask, edmask1);
  152.          break;
  153.       case 2:
  154.          copy_3_x_3(mask, edmask2);
  155.          break;
  156.       case 3:
  157.          copy_3_x_3(mask, edmask3);
  158.          break;
  159.       case 4:
  160.          copy_3_x_3(mask, edmask4);
  161.          break;
  162.       default:
  163.          printf("\nInvalid mask type, using mask 4");
  164.          copy_3_x_3(mask, edmask4);
  165.          break;
  166.    }
  167.  
  168.    if(does_not_exist(out_name)){
  169.       printf("\n\n output file does not exist %s", out_name);
  170.       read_tiff_header(in_name, &image_header);
  171.       round_off_image_size(&image_header,
  172.                            &length, &width);
  173.       image_header.image_length = length*ROWS;
  174.       image_header.image_width  = width*COLS;
  175.       create_allocate_tiff_file(out_name, &image_header,
  176.                                 out_image);
  177.    }  /* ends if does_not_exist */
  178.  
  179.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  180.  
  181.       /***************************
  182.       *
  183.       *   Loop over image array
  184.       *
  185.       ****************************/
  186.  
  187.    printf("\n");
  188.  
  189.    for(i=1; i<ROWS-1; i++){
  190.       if( (i%10) == 0) printf("%3d", i);
  191.       for(j=1; j<COLS-1; j++){
  192.          min = value;
  193.          for(a=-1; a<=1; a++){
  194.              for(b=-1; b<=1; b++){
  195.                 if(mask[a+1][b+1] == 1){
  196.                    if(the_image[i+a][j+b] < min)
  197.                       min = the_image[i+a][j+b];
  198.                 }  /* ends if mask == 1 */
  199.              }  /*  ends loop over b */
  200.          }  /* ends loop over a */
  201.          out_image[i][j] = min;
  202.       }  /* ends loop over j */
  203.    }  /* ends loop over i */
  204.  
  205.    fix_edges(out_image, 3);
  206.  
  207.    write_array_into_tiff_image(out_name, out_image,
  208.                                il, ie, ll, le);
  209.  
  210. }  /* ends mask_erosion */
  211.  
  212.  
  213.  
  214.  
  215.  
  216.      /***********************************************
  217.      *
  218.      *   copy_3_x_3(a, b)
  219.      *
  220.      *   This function copies a 3x3 array of shorts
  221.      *   from one array to another.  It copies array
  222.      *   b into array a.
  223.      *
  224.      ***********************************************/
  225.  
  226. copy_3_x_3(a, b)
  227.    short a[3][3], b[3][3];
  228. {
  229.    int i, j;
  230.    for(i=0; i<3; i++)
  231.       for(j=0; j<3; j++)
  232.          a[i][j] = b[i][j];
  233. }  /* ends copy_3_x_3 */
  234.  
  235.  
  236.