OpenCV
4.5.3
Open Source Computer Vision
|
Prev Tutorial: Eroding and Dilating
Next Tutorial: Hit-or-Miss
Original author | Ana Huamán |
Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn how to:
In the previous tutorial we covered two basic Morphology operations:
Based on these two we can effectuate more sophisticated transformations to our images. Here we discuss briefly 5 operations offered by OpenCV:
It is obtained by the erosion of an image followed by a dilation.
\[dst = open( src, element) = dilate( erode( src, element ) )\]
For instance, check out the example below. The image at the left is the original and the image at the right is the result after applying the opening transformation. We can observe that the small dots have disappeared.
It is obtained by the dilation of an image followed by an erosion.
\[dst = close( src, element ) = erode( dilate( src, element ) )\]
Useful to remove small holes (dark regions).
It is the difference between the dilation and the erosion of an image.
\[dst = morph_{grad}( src, element ) = dilate( src, element ) - erode( src, element )\]
It is useful for finding the outline of an object as can be seen below:
It is the difference between an input image and its opening.
\[dst = tophat( src, element ) = src - open( src, element )\]
It is the difference between the closing and its input image
\[dst = blackhat( src, element ) = close( src, element ) - src\]
operation: The kind of morphology transformation to be performed. Note that we have 5 alternatives:
As you can see the values range from <2-6>, that is why we add (+2) to the values entered by the Trackbar:
After compiling the code above we can execute it giving an image path as an argument. Results using the image: baboon.png:
And here are two snapshots of the display window. The first picture shows the output after using the operator Opening with a cross kernel. The second picture (right side, shows the result of using a Blackhat operator with an ellipse kernel.