OpenCV  4.5.5
Open Source Computer Vision
Eroding and Dilating

Prev Tutorial: Smoothing Images

Next Tutorial: More Morphology Transformations

Original author Ana Huamán
Compatibility OpenCV >= 3.0

Goal

In this tutorial you will learn how to:

Note
The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.

Morphological Operations

Dilation

Erosion

Code

Explanation

Note
Additionally, there are further parameters that allow you to perform multiple erosions/dilations (iterations) at once and also set the border type and value. However, We haven't used those in this simple tutorial. You can check out the reference for more details.

Results

Compile the code above and execute it (or run the script if using python) with an image as argument. If you do not provide an image as argument the default sample image (LinuxLogo.jpg) will be used.

For instance, using this image:

Morphology_1_Tutorial_Original_Image.jpg

We get the results below. Varying the indices in the Trackbars give different output images, naturally. Try them out! You can even try to add a third Trackbar to control the number of iterations.

Morphology_1_Result.jpg

(depending on the programming language the output might vary a little or be only 1 window)