Perhaps the best way to look at this is to create an output extension pixel. For the corresponding pixel in the image, align the structuring element so that the beginning of the structuring element is on that pixel of the image. If there is any overlap, set the output pixel of the extension in this place to 1, otherwise set it to 0.
Thus, this can be done by simply recounting each pixel in the image and checking if the element with the correctly shifted structuring overlaps the image. This means that you will likely have 4 nested loops: x img, y img, x se, y se. Therefore, for each pixel in the image, you iterate over the pixels of the structuring element and see if there is any overlap. This may not be the most efficient algorithm, but it is probably the simplest.
Also, I think your example is incorrect. Dilation depends on the origin of the structuring element. If the origin ...
in the upper left zero: you need to shift the image (-1, -1), (-1,0) and (0, -1), giving:
1 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0
in the lower right corner: you need to shift the image (0,0), (1,0) and (0,1), indicating:
0 0 0 0 0 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 0 0 0
MATLAB uses gender ((size (SE) +1) / 2) as the source of SE, so in this case it will use the top left pixel of SE. You can verify this using the imdilate MATLAB function.
Jason b
source share