Page 128 -
P. 128
102 Chapter 3 ■ Digital Morphology
/* Apply a erosion step on one pixel of IM, result to RES */
void erode_apply (IMAGE im, SE p, int ii, int jj, IMAGE res)
{
int i,j, is,js, ie, je, k, r;
/* 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 pixels in the image agree. */
r= 1;
for (i=is; i<ie; i++)
for (j=js; j<je; j++)
{
if (range(im,i,j))
{
k = p->data[i-is][j-js];
if (k == 1 && im->data[i][j]==0) r = 0;
} else if (p->data[i-is][j-js] != 0) r = 0;
}
res->data[ii][jj] = r;
} }
int bin_erode (IMAGE im, SE p)
{
IMAGE tmp;
int i,j;
/* Create a result image */
tmp = newimage (im->info->nr, im->info->nc);
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++)
erode_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.10: Slightly shortened version of the C source code for bin_erode. (See the
website for the complete version.) Some error checking has been removed for brevity.