Page 121 -
P. 121

Chapter 3 ■ Digital Morphology    95



                                 void dil_apply (IMAGE im, SE p, int ii, int jj, IMAGE res)
                                 {
                                         int i,j, is,js, ie, je, k;
                                 /* Find start and end pixel in IM */
                                        is = ii - p->oi; js = jj - p->oj;  ie = is + p->nr; je = js + p->nc;

                                 /* Place SE over the image from (is,js) to (ie,je). Set pixels
                                 in RES if the corresponding SE pixel is 1; else do nothing. */
                                         for (i=is; i<ie; i++)
                                                for (j=js; j<je; j++)
                                                {
                                                        if (range(im,i,j))
                                                        {
                                                                k = (p->data[i-is][j-js] == 1);
                                                                if (k>=0) res->data[i][j] |= k;
                                 }               }        }
                                 int bin_dilate (IMAGE im, SE p)
                                 {
                                         IMAGE tmp;
                                         int i,j;

                                 /* Source image empty? */
                                         if (im==0)
                                         {
                                                printf ( ˝ Bad image in BIN_DILATE\n˝);
                                                return 0;
                                         }
                                  /* Create a result image */
                                         tmp = newimage (im->info->nr, im->info->nc);
                                         if (tmp == 0)
                                         {
                                                printf ( ˝ Out of memory in BIN_DILATE.\n˝);
                                                return 0;
                                         }
                                         for (i=0; i<tmp->info->nr; i++)
                                                for (j=0; j<tmp->info->nc; j++)
                                                        tmp->data[i][j] = 0;
                                  /* Apply the SE to each black pixel of the input */
                                         for (i=0; i<im->info->nr; i++)
                                                for (j=0; j<im->info->nc; j++)
                                                        if (im->data[i][j] == WHITE)
                                         dil_apply (im, p, i, j, tmp);
                                  /* Copy result over the input */
                                         for (i=0; i<im->info->nr; i++)
                                                for (j=0; j<im->info->nc; j++)
                                                        im->data[i][j] = tmp->data[i][j];
                                         /* Free the result image - it was a temp */
                                         freeimage (tmp);
                                         return 1;
                                         }

                               Figure 3.6: C source code for the dilation of a binary image by a binary structuring element.
   116   117   118   119   120   121   122   123   124   125   126