File: erosion_dilatation.markdown

package info (click to toggle)
opencv 3.2.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 238,480 kB
  • sloc: xml: 901,650; cpp: 703,419; lisp: 20,142; java: 17,843; python: 17,641; ansic: 603; cs: 601; sh: 516; perl: 494; makefile: 117
file content (142 lines) | stat: -rw-r--r-- 6,396 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Eroding and Dilating {#tutorial_erosion_dilatation}
====================

Goal
----

In this tutorial you will learn how to:

-   Apply two very common morphology operators: Dilation and Erosion. For this purpose, you will use
    the following OpenCV functions:
    -   @ref cv::erode
    -   @ref cv::dilate

Cool Theory
-----------

@note The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.

Morphological Operations
------------------------

-   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.
-   The most basic morphological operations are two: Erosion and Dilation. They have a wide array of
    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:

    ![](images/Morphology_1_Tutorial_Theory_Original_Image.png)

### Dilation

-   This operations consists of convoluting an image \f$A\f$ with some kernel (\f$B\f$), which can have any
    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
    name *dilation*). Take as an example the image above. Applying dilation we can get:

    ![](images/Morphology_1_Tutorial_Theory_Dilation.png)

The background (bright) dilates around the black regions of the letter.

To better grasp the idea and avoid possible confusion, in this another example we have inverted the original
image such as the object in white is now the letter. We have performed two dilatations with a rectangular
structuring element of size `3x3`.

![Left image: original image inverted, right image: resulting dilatation](images/Morphology_1_Tutorial_Theory_Dilatation_2.png)

The dilatation makes the object in white bigger.

### Erosion

-   This operation is the sister of dilation. What this does is to compute a local minimum over the
    area of the kernel.
-   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.
-   Analagously to the example for dilation, we can apply the erosion operator to the original image
    (shown above). You can see in the result below that the bright areas of the image (the
    background, apparently), get thinner, whereas the dark zones (the "writing") gets bigger.

    ![](images/Morphology_1_Tutorial_Theory_Erosion.png)

In the same manner, the corresponding image resulting of the erosion operation on the inverted original image (two erosions
with a rectangular structuring element of size `3x3`):

![Left image: original image inverted, right image: resulting erosion](images/Morphology_1_Tutorial_Theory_Erosion_2.png)

The erosion makes the object in white smaller.

Code
----

This tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp)
@include samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp

Explanation
-----------

-#  Most of the stuff shown is known by you (if you have any doubt, please refer to the tutorials in
    previous sections). Let's check the general structure of the program:

    -   Load an image (can be BGR or grayscale)
    -   Create two windows (one for dilation output, the other for erosion)
    -   Create a set of two Trackbars for each operation:
        -   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:

-#  **erosion:**
    @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp erosion

    -   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
            specify, the default is a simple `3x3` matrix. Otherwise, we can specify its
            shape. For this, we need to use the function cv::getStructuringElement :
            @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp kernel

            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
(iterations) at once. We are not using it in this simple tutorial, though. You can check out the
Reference for more details.

-#  **dilation:**

    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.
    @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp dilation

Results
-------

Compile the code above and execute it with an image as argument. For instance, using this image:

![](images/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.

![](images/Morphology_1_Result.jpg)