~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.markdown

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Morphological Transformations {#tutorial_py_morphological_ops}
 
2
=============================
 
3
 
 
4
Goal
 
5
----
 
6
 
 
7
In this chapter,
 
8
    -   We will learn different morphological operations like Erosion, Dilation, Opening, Closing
 
9
        etc.
 
10
    -   We will see different functions like : **cv2.erode()**, **cv2.dilate()**,
 
11
        **cv2.morphologyEx()** etc.
 
12
 
 
13
Theory
 
14
------
 
15
 
 
16
Morphological transformations are some simple operations based on the image shape. It is normally
 
17
performed on binary images. It needs two inputs, one is our original image, second one is called
 
18
**structuring element** or **kernel** which decides the nature of operation. Two basic morphological
 
19
operators are Erosion and Dilation. Then its variant forms like Opening, Closing, Gradient etc also
 
20
comes into play. We will see them one-by-one with help of following image:
 
21
 
 
22
![image](images/j.png)
 
23
 
 
24
### 1. Erosion
 
25
 
 
26
The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of
 
27
foreground object (Always try to keep foreground in white). So what it does? The kernel slides
 
28
through the image (as in 2D convolution). A pixel in the original image (either 1 or 0) will be
 
29
considered 1 only if all the pixels under the kernel is 1, otherwise it is eroded (made to zero).
 
30
 
 
31
So what happends is that, all the pixels near boundary will be discarded depending upon the size of
 
32
kernel. So the thickness or size of the foreground object decreases or simply white region decreases
 
33
in the image. It is useful for removing small white noises (as we have seen in colorspace chapter),
 
34
detach two connected objects etc.
 
35
 
 
36
Here, as an example, I would use a 5x5 kernel with full of ones. Let's see it how it works:
 
37
@code{.py}
 
38
import cv2
 
39
import numpy as np
 
40
 
 
41
img = cv2.imread('j.png',0)
 
42
kernel = np.ones((5,5),np.uint8)
 
43
erosion = cv2.erode(img,kernel,iterations = 1)
 
44
@endcode
 
45
Result:
 
46
 
 
47
![image](images/erosion.png)
 
48
 
 
49
### 2. Dilation
 
50
 
 
51
It is just opposite of erosion. Here, a pixel element is '1' if atleast one pixel under the kernel
 
52
is '1'. So it increases the white region in the image or size of foreground object increases.
 
53
Normally, in cases like noise removal, erosion is followed by dilation. Because, erosion removes
 
54
white noises, but it also shrinks our object. So we dilate it. Since noise is gone, they won't come
 
55
back, but our object area increases. It is also useful in joining broken parts of an object.
 
56
@code{.py}
 
57
dilation = cv2.dilate(img,kernel,iterations = 1)
 
58
@endcode
 
59
Result:
 
60
 
 
61
![image](images/dilation.png)
 
62
 
 
63
### 3. Opening
 
64
 
 
65
Opening is just another name of **erosion followed by dilation**. It is useful in removing noise, as
 
66
we explained above. Here we use the function, **cv2.morphologyEx()**
 
67
@code{.py}
 
68
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
 
69
@endcode
 
70
Result:
 
71
 
 
72
![image](images/opening.png)
 
73
 
 
74
### 4. Closing
 
75
 
 
76
Closing is reverse of Opening, **Dilation followed by Erosion**. It is useful in closing small holes
 
77
inside the foreground objects, or small black points on the object.
 
78
@code{.py}
 
79
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
 
80
@endcode
 
81
Result:
 
82
 
 
83
![image](images/closing.png)
 
84
 
 
85
### 5. Morphological Gradient
 
86
 
 
87
It is the difference between dilation and erosion of an image.
 
88
 
 
89
The result will look like the outline of the object.
 
90
@code{.py}
 
91
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
 
92
@endcode
 
93
Result:
 
94
 
 
95
![image](images/gradient.png)
 
96
 
 
97
### 6. Top Hat
 
98
 
 
99
It is the difference between input image and Opening of the image. Below example is done for a 9x9
 
100
kernel.
 
101
@code{.py}
 
102
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
 
103
@endcode
 
104
Result:
 
105
 
 
106
![image](images/tophat.png)
 
107
 
 
108
### 7. Black Hat
 
109
 
 
110
It is the difference between the closing of the input image and input image.
 
111
@code{.py}
 
112
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
 
113
@endcode
 
114
Result:
 
115
 
 
116
![image](images/blackhat.png)
 
117
 
 
118
Structuring Element
 
119
-------------------
 
120
 
 
121
We manually created a structuring elements in the previous examples with help of Numpy. It is
 
122
rectangular shape. But in some cases, you may need elliptical/circular shaped kernels. So for this
 
123
purpose, OpenCV has a function, **cv2.getStructuringElement()**. You just pass the shape and size of
 
124
the kernel, you get the desired kernel.
 
125
@code{.py}
 
126
# Rectangular Kernel
 
127
>>> cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
 
128
array([[1, 1, 1, 1, 1],
 
129
       [1, 1, 1, 1, 1],
 
130
       [1, 1, 1, 1, 1],
 
131
       [1, 1, 1, 1, 1],
 
132
       [1, 1, 1, 1, 1]], dtype=uint8)
 
133
 
 
134
# Elliptical Kernel
 
135
>>> cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
 
136
array([[0, 0, 1, 0, 0],
 
137
       [1, 1, 1, 1, 1],
 
138
       [1, 1, 1, 1, 1],
 
139
       [1, 1, 1, 1, 1],
 
140
       [0, 0, 1, 0, 0]], dtype=uint8)
 
141
 
 
142
# Cross-shaped Kernel
 
143
>>> cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
 
144
array([[0, 0, 1, 0, 0],
 
145
       [0, 0, 1, 0, 0],
 
146
       [1, 1, 1, 1, 1],
 
147
       [0, 0, 1, 0, 0],
 
148
       [0, 0, 1, 0, 0]], dtype=uint8)
 
149
@endcode
 
150
Additional Resources
 
151
--------------------
 
152
 
 
153
-#  [Morphological Operations](http://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm) at HIPR2
 
154
 
 
155
Exercises
 
156
---------