Listing 1 Erosion and dilation routines using the threshold technique

    /*****************************************************
    *   erosion(...
    *
    *   This function performs the erosion operation. if
    *   a value pixel has more than the threshold number
    *   of 0 neighbors, you erode it by setting it to 0.
    ******************************************************/

erosion(in_name, out_name, the_image, out_image,
       il, ie, ll, value, threshold)
   char   in_name[], out_name[];
   int    il, ie, ll,  le;
   short  the_image[ROWS][COLS],
         out_image[ROWS][COLS],
         threshold, value;
{
   int    a, b, count, i, j, k;
   int    length, width;
   struct tiff_header_struct image_header;

   if(does_not_exist(out_name)){
      printf{"n\n output file does not exist %s", out_name);
      read_tiff_header(in_name, &image_header};
      round_off_image_size(&image_header, &length, &width);
      image_header.image_length = length*ROWS;
      image_header.image_width = width*COLS;
      create_allocate_tiff_file(out_name, &image_header, out_image);
   }  /* ends if does_not_exist */

   read_tiff_image(in_name, the_image, il, ie, ll, le);

      /***************************
      *   Loop over image array
      ***************************/

   for(i-0; i<ROWS; i++)
      for(j=0; j<COLS; j++)
         out_image[i][j] = the_image[i][j];

   printf("\n");
   for(i=1; i<ROWS-1; i++){
      if( (i%10) == 0 printf("%3d", i);
      for(j-1; j<COLS-1; j++){
         if(the_image[i][j] == value){
            count = 0;
            for(a=-1; a<=-1; a++){
                for(b=-1; b<=l; b++){
                    if(the_image[i+a][j+b] == 0)
                      count++;
               }  /* ends loop over b */
            }  /* ends loop over a */
            if(count > threshold) out_image[i][j] = 0;
         }  /* ends if the_image == value */
      }  /* ends loop over j */
   }  /* ends loop over i */

   fix_edges(out_image, 3);

   write_array_into_tiff_image(out_name, out_image, il, ie, ll, le);

}  /* ends erosion */

    /*******************************************************
    *   dilation(...
    *
    *   This function performs the dilation operation. If
    *   a 0 pixel has more than threshold number of value
    *   neighbors, you dilate it by setting it to value.
    *******************************************************/

dilation(in_name, out_name, the_image, out_image,
        il, ie, ll, le, value, threshold)
   char   in_name[], out_name[];
   int    il, ie, ll, le;
   short  the_image[ROWS][COLS],
         out_image[ROWS][COLS],
         threshold, value;
{
   int    a, b, count, i, j, k;
   int    length, width;
   struct tiff_header_struct image_header;

   if(does_not_exist(outname)){
      printf("\n\n output file does not exist %s", out_name);
      read_tiff_header(in_name, &image_header);
      round_off_image_size(&image_header, &length, &width);
      image_header.image_length = length*ROWS;
      image_header.image_width = width*COLS;

      create_allocate_tiff_file(out_name, &image_header, out_image);
   }  /* ends if does_not_exist */

   read_tiff_image(in_name, the_image, il, ie, ll, le);

      /************************
      *   Loop over image array
      ************************/

   printf("\n");

   for(i=1; i<ROWS-1; i++){
      if( (i%10) == 0) printf("%3d", i);
      for(j=1; j<COLS-1; j++){
         out_image[i][j] = the_image[i][j];
         if(The_image[i][j] == 0){
            count = 0;
            for(a=-1; a<=1; a++){
                for(b=-1; b<=1; b++){
                  if(a!=0 && b!=0){
                     if(the_image[i+a][j+b] == value)
                        count++;
                  }  /* ends avoid the center pixel */
                    }  /* ends loop over b */
            }  /* ends loop over a */
            if(count > threshold)
               out_image[i][j] = value;
         }  /* ends if the_image == 0 */
      }  /* ends loop over j */
   }  /* ends loop over i */

   fix_edges(out_image, 3);

   write_array_into_tiff_image(out_name, out_image, il, ie, ll, le);

}  /* ends dilation */

/* End of File */