2014-11-27 20:39:05 +08:00
|
|
|
Eroding and Dilating {#tutorial_erosion_dilatation}
|
|
|
|
====================
|
|
|
|
|
2018-08-03 02:22:58 +08:00
|
|
|
@prev_tutorial{tutorial_gausian_median_blur_bilateral_filter}
|
|
|
|
@next_tutorial{tutorial_opening_closing_hats}
|
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
Goal
|
|
|
|
----
|
|
|
|
|
|
|
|
In this tutorial you will learn how to:
|
|
|
|
|
2017-03-02 02:44:34 +08:00
|
|
|
- Apply two very common morphological operators: Erosion and Dilation. For this purpose, you will use
|
2014-11-27 20:39:05 +08:00
|
|
|
the following OpenCV functions:
|
|
|
|
- @ref cv::erode
|
|
|
|
- @ref cv::dilate
|
|
|
|
|
|
|
|
@note The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
|
2014-11-28 00:54:13 +08:00
|
|
|
|
|
|
|
Morphological Operations
|
|
|
|
------------------------
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
- In short: A set of operations that process images based on shapes. Morphological operations
|
|
|
|
apply a *structuring element* to an input image and generate an output image.
|
2017-03-02 02:44:34 +08:00
|
|
|
- The most basic morphological operations are: Erosion and Dilation. They have a wide array of
|
2014-11-27 20:39:05 +08:00
|
|
|
uses, i.e. :
|
|
|
|
- Removing noise
|
|
|
|
- Isolation of individual elements and joining disparate elements in an image.
|
|
|
|
- Finding of intensity bumps or holes in an image
|
|
|
|
- We will explain dilation and erosion briefly, using the following image as an example:
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Morphology_1_Tutorial_Theory_Original_Image.png)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
### Dilation
|
|
|
|
|
2017-03-02 02:44:34 +08:00
|
|
|
- This operations consists of convolving an image \f$A\f$ with some kernel (\f$B\f$), which can have any
|
2014-11-27 20:39:05 +08:00
|
|
|
shape or size, usually a square or circle.
|
|
|
|
- The kernel \f$B\f$ has a defined *anchor point*, usually being the center of the kernel.
|
|
|
|
- As the kernel \f$B\f$ is scanned over the image, we compute the maximal pixel value overlapped by
|
|
|
|
\f$B\f$ and replace the image pixel in the anchor point position with that maximal value. As you can
|
|
|
|
deduce, this maximizing operation causes bright regions within an image to "grow" (therefore the
|
2018-05-16 20:05:39 +08:00
|
|
|
name *dilation*).
|
|
|
|
- The dilatation operation is: \f$\texttt{dst} (x,y) = \max _{(x',y'): \, \texttt{element} (x',y') \ne0 } \texttt{src} (x+x',y+y')\f$
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2018-05-16 20:05:39 +08:00
|
|
|
- Take the above image as an example. Applying dilation we can get:
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2018-05-16 20:05:39 +08:00
|
|
|
![](images/Morphology_1_Tutorial_Theory_Dilation.png)
|
2016-10-10 05:45:04 +08:00
|
|
|
|
2018-05-16 20:05:39 +08:00
|
|
|
- The bright area of the letter dilates around the black regions of the background.
|
2016-10-10 05:45:04 +08:00
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
### Erosion
|
|
|
|
|
2017-03-02 02:44:34 +08:00
|
|
|
- This operation is the sister of dilation. It computes a local minimum over the
|
|
|
|
area of given kernel.
|
2014-11-27 20:39:05 +08:00
|
|
|
- As the kernel \f$B\f$ is scanned over the image, we compute the minimal pixel value overlapped by
|
|
|
|
\f$B\f$ and replace the image pixel under the anchor point with that minimal value.
|
2018-05-16 20:05:39 +08:00
|
|
|
- The erosion operation is: \f$\texttt{dst} (x,y) = \min _{(x',y'): \, \texttt{element} (x',y') \ne0 } \texttt{src} (x+x',y+y')\f$
|
2014-11-27 20:39:05 +08:00
|
|
|
- Analagously to the example for dilation, we can apply the erosion operator to the original image
|
2018-05-16 20:05:39 +08:00
|
|
|
(shown above). You can see in the result below that the bright areas of the image get thinner,
|
|
|
|
whereas the dark zones gets bigger.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Morphology_1_Tutorial_Theory_Erosion.png)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
Code
|
|
|
|
----
|
|
|
|
|
2018-05-16 20:05:39 +08:00
|
|
|
@add_toggle_cpp
|
2017-03-02 02:44:34 +08:00
|
|
|
This tutorial's code is shown below. You can also download it
|
2018-05-31 21:45:18 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/3.4/samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp)
|
2015-04-29 15:31:53 +08:00
|
|
|
@include samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp
|
2018-05-16 20:05:39 +08:00
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_java
|
|
|
|
This tutorial's code is shown below. You can also download it
|
2018-05-31 21:45:18 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/3.4/samples/java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java)
|
2018-05-16 20:05:39 +08:00
|
|
|
@include samples/java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
|
|
|
This tutorial's code is shown below. You can also download it
|
2018-05-31 21:45:18 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/3.4/samples/python/tutorial_code/imgProc/erosion_dilatation/morphology_1.py)
|
2018-05-16 20:05:39 +08:00
|
|
|
@include samples/python/tutorial_code/imgProc/erosion_dilatation/morphology_1.py
|
|
|
|
@end_toggle
|
2014-11-28 00:54:13 +08:00
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
Explanation
|
|
|
|
-----------
|
|
|
|
|
2017-03-02 02:44:34 +08:00
|
|
|
-# Most of the material shown here is trivial (if you have any doubt, please refer to the tutorials in
|
2018-05-16 20:05:39 +08:00
|
|
|
previous sections). Let's check the general structure of the C++ program:
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2015-04-30 17:27:58 +08:00
|
|
|
- Load an image (can be BGR or grayscale)
|
2014-11-27 20:39:05 +08:00
|
|
|
- Create two windows (one for dilation output, the other for erosion)
|
2016-07-18 21:32:05 +08:00
|
|
|
- Create a set of two Trackbars for each operation:
|
2014-11-27 20:39:05 +08:00
|
|
|
- The first trackbar "Element" returns either **erosion_elem** or **dilation_elem**
|
|
|
|
- The second trackbar "Kernel size" return **erosion_size** or **dilation_size** for the
|
|
|
|
corresponding operation.
|
|
|
|
- Every time we move any slider, the user's function **Erosion** or **Dilation** will be
|
|
|
|
called and it will update the output image based on the current trackbar values.
|
|
|
|
|
|
|
|
Let's analyze these two functions:
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
-# **erosion:**
|
2016-07-18 21:32:05 +08:00
|
|
|
@snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp erosion
|
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
- The function that performs the *erosion* operation is @ref cv::erode . As we can see, it
|
|
|
|
receives three arguments:
|
|
|
|
- *src*: The source image
|
|
|
|
- *erosion_dst*: The output image
|
|
|
|
- *element*: This is the kernel we will use to perform the operation. If we do not
|
2014-11-28 00:54:13 +08:00
|
|
|
specify, the default is a simple `3x3` matrix. Otherwise, we can specify its
|
|
|
|
shape. For this, we need to use the function cv::getStructuringElement :
|
2016-07-18 21:32:05 +08:00
|
|
|
@snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp kernel
|
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
We can choose any of three shapes for our kernel:
|
|
|
|
|
|
|
|
- Rectangular box: MORPH_RECT
|
|
|
|
- Cross: MORPH_CROSS
|
|
|
|
- Ellipse: MORPH_ELLIPSE
|
|
|
|
|
|
|
|
Then, we just have to specify the size of our kernel and the *anchor point*. If not
|
|
|
|
specified, it is assumed to be in the center.
|
|
|
|
|
|
|
|
- That is all. We are ready to perform the erosion of our image.
|
|
|
|
@note Additionally, there is another parameter that allows you to perform multiple erosions
|
2017-03-02 02:44:34 +08:00
|
|
|
(iterations) at once. However, We haven't used it in this simple tutorial. You can check out the
|
|
|
|
reference for more details.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
-# **dilation:**
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 00:54:13 +08:00
|
|
|
The code is below. As you can see, it is completely similar to the snippet of code for **erosion**.
|
|
|
|
Here we also have the option of defining our kernel, its anchor point and the size of the operator
|
|
|
|
to be used.
|
2016-07-18 21:32:05 +08:00
|
|
|
@snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp dilation
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 00:54:13 +08:00
|
|
|
Results
|
|
|
|
-------
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 00:54:13 +08:00
|
|
|
Compile the code above and execute it with an image as argument. For instance, using this image:
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Morphology_1_Tutorial_Original_Image.jpg)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 00:54:13 +08:00
|
|
|
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.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Morphology_1_Result.jpg)
|