@@ -84,57 +84,198 @@ This tutorial's code is shown below. You can also download it
84
84
Explanation
85
85
-----------
86
86
87
- -# Most of the material shown here is trivial (if you have any doubt, please refer to the tutorials in
88
- previous sections). Let's check the general structure of the C++ program:
87
+ @add_toggle_cpp
88
+ Most of the material shown here is trivial (if you have any doubt, please refer to the tutorials in
89
+ previous sections). Let's check the general structure of the C++ program:
90
+
91
+ @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp main
92
+
93
+ -# Load an image (can be BGR or grayscale)
94
+ -# Create two windows (one for dilation output, the other for erosion)
95
+ -# Create a set of two Trackbars for each operation:
96
+ - The first trackbar "Element" returns either ** erosion_elem** or ** dilation_elem**
97
+ - The second trackbar "Kernel size" return ** erosion_size** or ** dilation_size** for the
98
+ corresponding operation.
99
+ -# Call once erosion and dilation to show the initial image.
100
+
101
+
102
+ Every time we move any slider, the user's function ** Erosion** or ** Dilation** will be
103
+ called and it will update the output image based on the current trackbar values.
104
+
105
+ Let's analyze these two functions:
106
+
107
+ #### The erosion function
108
+
109
+ @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp erosion
110
+
111
+ The function that performs the * erosion* operation is @ref cv::erode . As we can see, it
112
+ receives three arguments:
113
+ - * src* : The source image
114
+ - * erosion_dst* : The output image
115
+ - * element* : This is the kernel we will use to perform the operation. If we do not
116
+ specify, the default is a simple ` 3x3 ` matrix. Otherwise, we can specify its
117
+ shape. For this, we need to use the function cv::getStructuringElement :
118
+ @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp kernel
119
+
120
+ We can choose any of three shapes for our kernel:
121
+
122
+ - Rectangular box: MORPH_RECT
123
+ - Cross: MORPH_CROSS
124
+ - Ellipse: MORPH_ELLIPSE
125
+
126
+ Then, we just have to specify the size of our kernel and the * anchor point* . If not
127
+ specified, it is assumed to be in the center.
128
+
129
+ That is all. We are ready to perform the erosion of our image.
130
+
131
+ #### The dilation function
132
+
133
+ The code is below. As you can see, it is completely similar to the snippet of code for ** erosion** .
134
+ Here we also have the option of defining our kernel, its anchor point and the size of the operator
135
+ to be used.
136
+ @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp dilation
137
+ @end_toggle
138
+
139
+ @add_toggle_java
140
+ Most of the material shown here is trivial (if you have any doubt, please refer to the tutorials in
141
+ previous sections). Let's check however the general structure of the java class. There are 4 main
142
+ parts in the java class:
143
+
144
+ - the class constructor which setups the window that will be filled with window components
145
+ - the ` addComponentsToPane ` method, which fills out the window
146
+ - the ` update ` method, which determines what happens when the user changes any value
147
+ - the ` main ` method, which is the entry point of the program
148
+
149
+ In this tutorial we will focus on the ` addComponentsToPane ` and ` update ` methods. However, for completion the
150
+ steps followed in the constructor are:
151
+
152
+ -# Load an image (can be BGR or grayscale)
153
+ -# Create a window
154
+ -# Add various control components with ` addComponentsToPane `
155
+ -# show the window
156
+
157
+ The components were added by the following method:
158
+
159
+ @snippet java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java components
160
+
161
+ In short we
162
+
163
+ -# create a panel for the sliders
164
+ -# create a combo box for the element types
165
+ -# create a slider for the kernel size
166
+ -# create a combo box for the morphology function to use (erosion or dilation)
167
+
168
+ The action and state changed listeners added call at the end the ` update ` method which updates
169
+ the image based on the current slider values. So every time we move any slider, the ` update ` method is triggered.
89
170
90
- - Load an image (can be BGR or grayscale)
91
- - Create two windows (one for dilation output, the other for erosion)
92
- - Create a set of two Trackbars for each operation:
93
- - The first trackbar "Element" returns either **erosion_elem** or **dilation_elem**
94
- - The second trackbar "Kernel size" return **erosion_size** or **dilation_size** for the
95
- corresponding operation.
96
- - Every time we move any slider, the user's function **Erosion** or **Dilation** will be
97
- called and it will update the output image based on the current trackbar values.
171
+ #### Updating the image
98
172
99
- Let's analyze these two functions :
173
+ To update the image we used the following implementation :
100
174
101
- -# ** erosion:**
102
- @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp erosion
175
+ @snippet java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java update
103
176
104
- - The function that performs the *erosion* operation is @ref cv::erode . As we can see, it
105
- receives three arguments:
106
- - *src*: The source image
107
- - *erosion_dst*: The output image
108
- - *element*: This is the kernel we will use to perform the operation. If we do not
109
- specify, the default is a simple `3x3` matrix. Otherwise, we can specify its
110
- shape. For this, we need to use the function cv::getStructuringElement :
111
- @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp kernel
177
+ In other words we
112
178
113
- We can choose any of three shapes for our kernel:
179
+ -# get the structuring element the user chose
180
+ -# execute the ** erosion** or ** dilation** function based on ` doErosion `
181
+ -# reload the image with the morphology applied
182
+ -# repaint the frame
114
183
115
- - Rectangular box: MORPH_RECT
116
- - Cross: MORPH_CROSS
117
- - Ellipse: MORPH_ELLIPSE
184
+ Let's analyze the ` erode ` and ` dilate ` methods:
118
185
119
- Then, we just have to specify the size of our kernel and the *anchor point*. If not
120
- specified, it is assumed to be in the center.
186
+ #### The erosion method
121
187
122
- - That is all. We are ready to perform the erosion of our image.
123
- @note Additionally, there is another parameter that allows you to perform multiple erosions
124
- (iterations) at once. However, We haven't used it in this simple tutorial. You can check out the
125
- reference for more details.
188
+ @snippet java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java erosion
126
189
127
- -# ** dilation:**
190
+ The function that performs the * erosion* operation is @ref cv::erode . As we can see, it
191
+ receives three arguments:
192
+ - * src* : The source image
193
+ - * erosion_dst* : The output image
194
+ - * element* : This is the kernel we will use to perform the operation. For specifying the shape, we need to use
195
+ the function cv::getStructuringElement :
196
+ @snippet java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java kernel
128
197
129
- The code is below. As you can see, it is completely similar to the snippet of code for **erosion**.
130
- Here we also have the option of defining our kernel, its anchor point and the size of the operator
131
- to be used.
132
- @snippet cpp/tutorial_code/ImgProc/Morphology_1.cpp dilation
198
+ We can choose any of three shapes for our kernel:
199
+
200
+ - Rectangular box: CV_SHAPE_RECT
201
+ - Cross: CV_SHAPE_CROSS
202
+ - Ellipse: CV_SHAPE_ELLIPSE
203
+
204
+ Together with the shape we specify the size of our kernel and the * anchor point* . If the anchor point is not
205
+ specified, it is assumed to be in the center.
206
+
207
+ That is all. We are ready to perform the erosion of our image.
208
+
209
+ #### The dilation function
210
+
211
+ The code is below. As you can see, it is completely similar to the snippet of code for ** erosion** .
212
+ Here we also have the option of defining our kernel, its anchor point and the size of the operator
213
+ to be used.
214
+ @snippet java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java dilation
215
+ @end_toggle
216
+
217
+ @add_toggle_python
218
+ Most of the material shown here is trivial (if you have any doubt, please refer to the tutorials in
219
+ previous sections). Let's check the general structure of the python script:
220
+
221
+ @snippet python/tutorial_code/imgProc/erosion_dilatation/morphology_1.py main
222
+
223
+ -# Load an image (can be BGR or grayscale)
224
+ -# Create two windows (one for erosion output, the other for dilation) with a set of trackbars each
225
+ - The first trackbar "Element" returns the value for the morphological type that will be mapped
226
+ (1 = rectangle, 2 = cross, 3 = ellipse)
227
+ - The second trackbar "Kernel size" returns the size of the element for the
228
+ corresponding operation
229
+ -# Call once erosion and dilation to show the initial image
230
+
231
+ Every time we move any slider, the user's function ** erosion** or ** dilation** will be
232
+ called and it will update the output image based on the current trackbar values.
233
+
234
+ Let's analyze these two functions:
235
+
236
+ #### The erosion function
237
+
238
+ @snippet python/tutorial_code/imgProc/erosion_dilatation/morphology_1.py erosion
239
+
240
+ The function that performs the * erosion* operation is @ref cv::erode . As we can see, it
241
+ receives two arguments and returns the processed image:
242
+ - * src* : The source image
243
+ - * element* : The kernel we will use to perform the operation. We can specify its
244
+ shape by using the function cv::getStructuringElement :
245
+ @snippet python/tutorial_code/imgProc/erosion_dilatation/morphology_1.py kernel
246
+
247
+ We can choose any of three shapes for our kernel:
248
+
249
+ - Rectangular box: MORPH_RECT
250
+ - Cross: MORPH_CROSS
251
+ - Ellipse: MORPH_ELLIPSE
252
+
253
+ Then, we just have to specify the size of our kernel and the * anchor point* . If the anchor point not
254
+ specified, it is assumed to be in the center.
255
+
256
+ That is all. We are ready to perform the erosion of our image.
257
+
258
+ #### The dilation function
259
+
260
+ The code is below. As you can see, it is completely similar to the snippet of code for ** erosion** .
261
+ Here we also have the option of defining our kernel, its anchor point and the size of the operator
262
+ to be used.
263
+
264
+ @snippet python/tutorial_code/imgProc/erosion_dilatation/morphology_1.py dilation
265
+ @end_toggle
266
+
267
+ @note Additionally, there are further parameters that allow you to perform multiple erosions/dilations
268
+ (iterations) at once and also set the border type and value. However, We haven't used those
269
+ in this simple tutorial. You can check out the reference for more details.
133
270
134
271
Results
135
272
-------
136
273
137
- Compile the code above and execute it with an image as argument. For instance, using this image:
274
+ Compile the code above and execute it (or run the script if using python) with an image as argument.
275
+ If you do not provide an image as argument the default sample image
276
+ ([ LinuxLogo.jpg] ( https://github.com/opencv/opencv/tree/master/samples/data/LinuxLogo.jpg ) ) will be used.
277
+
278
+ For instance, using this image:
138
279
139
280
![ ] ( images/Morphology_1_Tutorial_Original_Image.jpg )
140
281
@@ -143,3 +284,4 @@ naturally. Try them out! You can even try to add a third Trackbar to control the
143
284
iterations.
144
285
145
286
![ ] ( images/Morphology_1_Result.jpg )
287
+ (depending on the programming language the output might vary a little or be only 1 window)
0 commit comments