Mathematical morphology isn't only about erosion and dilation. Those are just the fundamentals which you use to build more complex operations.
These composite operations are made up of two or more basic operations, like dilation, erosion and image arithmetic. And these operations produce characteristic outputs that can help you out during image analysis.
Opening an image is achieved by first eroding an image and then dilating it. Opening removes any narrow "connections" between two regions. Here's an example:
Closing an image is done by first dilating the image and then eroding it. The order is the reverse of opening. Closing fills up any narrow black regions in the image. Here's an example:
The morphological gradient is the difference between the dilation and the erosion of the image. Thus, you have three basic operations being used.
This gradient is used to find boundaries or edges in an image. You should apply some filtering before calculating the gradient because it is very sensitive to noise.
Here's an example of the gradient in action:
The top hat is the difference of the source image and the opening of the source image. It highlights the narrow pathways between different regions.
Here's an example:
The black hat is the difference between the closing of an image and the image itself. This highlights the narrow black regions in the image.
Here's an example:
The structuring element
In each of the above examples, I've used a standard 3x3 square as the structuring element (with an anchor point at the center). You can choose whatever structuring element you want. You'll get different results based on whatever structuring element you choose.
You learned about some interesting composite operations that can be done with just the basic erosion and dilation. With these, you can improve your segmented images (get rid of that pesky noise).
These operations form the basis for even more complex operations. Things like generating a skeleton of the object. We'll look at those in a later post.