# Erosion and dilation in images

I don't know if I am posting in the correct place. I wanted to ask about dilation and erosion in bitmaps. I have as a homework to implement in a programming language these morphological operations but I fail in understanding how they work. I know that erosion will shrink an object and dilation will enlarge it.

For example I have $$A=\matrix{0 &1 &0 &1 & 1\\ 0 &1 &0 &1 & 0\\ 0 &1 &1 &1 & 0\\ 0 &1 &1 &0 & 0\\ 0 &1 &1 &1 & 0},\; se=\matrix{1 &0 &0\\ 1 &1 &0\\ 0 &0 &1}$$

What would be the eroded and dilated output and how to compute it step by step?

## migrated from math.stackexchange.comFeb 16 '12 at 9:00

This question came from our site for people studying math at any level and professionals in related fields.

• Did you read this? mathworks.com/help/toolbox/images/f18-12508.html – leonbloy Feb 16 '12 at 0:50
• @leonbloy yes I read it but I used matlab to see if I did it correctly and apparently I did not. – omg_img Feb 16 '12 at 1:13
• @J.M. Yes I thought so but then I took a look at the faq and it didn't seem right to post the question there. – omg_img Feb 16 '12 at 1:25
• Basically, the implementation of dilation and erosion follow their definition quite close. Some make sure that you really understood these definitions. (By the way: Can you calculate the outcome of your example by hand?) One point which have to be considered in addition to the definition is the boundary treatment. Most often this is done by extending the image $A$ over the boundary as far as needed (e.g. in a symmetric way) and apply erosion and dilation only the "inner pixels" of the extended image. – Dirk Feb 16 '12 at 7:19
• @Dirk I am trying to do it by hand first. I am failing at this point. This is why I am asking for a step by step example. – omg_img Feb 16 '12 at 8:55

1. Print out A and se on two sheets of paper
2. Place the se paper on every pixel of the A sheet in turn
3. At each position: Take the pixel values of A at the respective positions where se is 1. For the first top-left position, this would be 0,0,1,1 as I have tried to illustrate here:
4. For an erosion, the result for the current pixel is the logical AND of the values you just wrote down. For dilation it is logical OR. (Or min/max for grayvalue images)
Note: You will either get a result image that is smaller than A or you have to add "padding" pixels to A (typically 1 for erosion and 0 for dilation)