1
1
Making your own linear filters! {#tutorial_filter_2d}
2
2
===============================
3
3
4
+ @prev_tutorial{tutorial_threshold_inRange}
5
+ @next_tutorial{tutorial_copyMakeBorder}
6
+
4
7
Goal
5
8
----
6
9
7
10
In this tutorial you will learn how to:
8
11
9
- - Use the OpenCV function @ ref cv:: filter2D to create your own linear filters.
12
+ - Use the OpenCV function ** filter2D() ** to create your own linear filters.
10
13
11
14
Theory
12
15
------
@@ -40,61 +43,127 @@ Expressing the procedure above in the form of an equation we would have:
40
43
41
44
\f[ H(x,y) = \sum_ {i=0}^{M_ {i} - 1} \sum_ {j=0}^{M_ {j}-1} I(x+i - a_ {i}, y + j - a_ {j})K(i,j)\f]
42
45
43
- Fortunately, OpenCV provides you with the function @ ref cv:: filter2D so you do not have to code all
46
+ Fortunately, OpenCV provides you with the function ** filter2D() ** so you do not have to code all
44
47
these operations.
45
48
46
- Code
47
- ----
48
-
49
- -# ** What does this program do?**
50
- - Loads an image
51
- - Performs a * normalized box filter* . For instance, for a kernel of size \f$size = 3\f$, the
52
- kernel would be:
49
+ ### What does this program do?
50
+ - Loads an image
51
+ - Performs a * normalized box filter* . For instance, for a kernel of size \f$size = 3\f$, the
52
+ kernel would be:
53
53
54
- \f[K = \dfrac{1}{3 \cdot 3} \begin{bmatrix}
55
- 1 & 1 & 1 \\
54
+ \f[ K = \dfrac{1}{3 \cdot 3} \begin{bmatrix}
55
+ 1 & 1 & 1 \\
56
56
1 & 1 & 1 \\
57
57
1 & 1 & 1
58
- \end{bmatrix}\f]
58
+ \end{bmatrix}\f]
59
+
60
+ The program will perform the filter operation with kernels of sizes 3, 5, 7, 9 and 11.
59
61
60
- The program will perform the filter operation with kernels of sizes 3, 5, 7, 9 and 11.
62
+ - The filter output ( with each kernel) will be shown during 500 milliseconds
61
63
62
- - The filter output (with each kernel) will be shown during 500 milliseconds
64
+ Code
65
+ ----
63
66
64
- -# The tutorial code's is shown lines below. You can also download it from
65
- [ here] ( https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/filter2D_demo.cpp )
66
- @include cpp/tutorial_code/ImgTrans/filter2D_demo.cpp
67
+ The tutorial code's is shown in the lines below.
68
+
69
+ @add_toggle_cpp
70
+ You can also download it from
71
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/filter2D_demo.cpp )
72
+ @include cpp/tutorial_code/ImgTrans/filter2D_demo.cpp
73
+ @end_toggle
74
+
75
+ @add_toggle_java
76
+ You can also download it from
77
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgTrans/Filter2D/Filter2D_Demo.java )
78
+ @include java/tutorial_code/ImgTrans/Filter2D/Filter2D_Demo.java
79
+ @end_toggle
80
+
81
+ @add_toggle_python
82
+ You can also download it from
83
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/ImgTrans/Filter2D/filter2D.py )
84
+ @include python/tutorial_code/ImgTrans/Filter2D/filter2D.py
85
+ @end_toggle
67
86
68
87
Explanation
69
88
-----------
70
89
71
- -# Load an image
72
- @snippet cpp/tutorial_code/ImgTrans/filter2D_demo.cpp load
73
- -# Initialize the arguments for the linear filter
74
- @snippet cpp/tutorial_code/ImgTrans/filter2D_demo.cpp init_arguments
75
- -# Perform an infinite loop updating the kernel size and applying our linear filter to the input
76
- image. Let's analyze that more in detail:
77
- -# First we define the kernel our filter is going to use. Here it is:
78
- @snippet cpp/tutorial_code/ImgTrans/filter2D_demo.cpp update_kernel
79
- The first line is to update the * kernel_size* to odd values in the range: \f$[ 3,11] \f$. The second
80
- line actually builds the kernel by setting its value to a matrix filled with \f$1's\f$ and
81
- normalizing it by dividing it between the number of elements.
82
-
83
- -# After setting the kernel, we can generate the filter by using the function @ref cv::filter2D :
84
- @snippet cpp/tutorial_code/ImgTrans/filter2D_demo.cpp apply_filter
85
- The arguments denote:
86
-
87
- -# *src*: Source image
88
- -# *dst*: Destination image
89
- -# *ddepth*: The depth of *dst*. A negative value (such as \f$-1\f$) indicates that the depth is
90
+ #### Load an image
91
+
92
+ @add_toggle_cpp
93
+ @snippet cpp/tutorial_code/ImgTrans/filter2D_demo.cpp load
94
+ @end_toggle
95
+
96
+ @add_toggle_java
97
+ @snippet java/tutorial_code/ImgTrans/Filter2D/Filter2D_Demo.java load
98
+ @end_toggle
99
+
100
+ @add_toggle_python
101
+ @snippet python/tutorial_code/ImgTrans/Filter2D/filter2D.py load
102
+ @end_toggle
103
+
104
+ #### Initialize the arguments
105
+
106
+ @add_toggle_cpp
107
+ @snippet cpp/tutorial_code/ImgTrans/filter2D_demo.cpp init_arguments
108
+ @end_toggle
109
+
110
+ @add_toggle_java
111
+ @snippet java/tutorial_code/ImgTrans/Filter2D/Filter2D_Demo.java init_arguments
112
+ @end_toggle
113
+
114
+ @add_toggle_python
115
+ @snippet python/tutorial_code/ImgTrans/Filter2D/filter2D.py init_arguments
116
+ @end_toggle
117
+
118
+ ##### Loop
119
+
120
+ Perform an infinite loop updating the kernel size and applying our linear filter to the input
121
+ image. Let's analyze that more in detail:
122
+
123
+ - First we define the kernel our filter is going to use. Here it is:
124
+
125
+ @add_toggle_cpp
126
+ @snippet cpp/tutorial_code/ImgTrans/filter2D_demo.cpp update_kernel
127
+ @end_toggle
128
+
129
+ @add_toggle_java
130
+ @snippet java/tutorial_code/ImgTrans/Filter2D/Filter2D_Demo.java update_kernel
131
+ @end_toggle
132
+
133
+ @add_toggle_python
134
+ @snippet python/tutorial_code/ImgTrans/Filter2D/filter2D.py update_kernel
135
+ @end_toggle
136
+
137
+ The first line is to update the * kernel_size* to odd values in the range: \f$[ 3,11] \f$.
138
+ The second line actually builds the kernel by setting its value to a matrix filled with
139
+ \f$1's\f$ and normalizing it by dividing it between the number of elements.
140
+
141
+ - After setting the kernel, we can generate the filter by using the function ** filter2D()** :
142
+
143
+ @add_toggle_cpp
144
+ @snippet cpp/tutorial_code/ImgTrans/filter2D_demo.cpp apply_filter
145
+ @end_toggle
146
+
147
+ @add_toggle_java
148
+ @snippet java/tutorial_code/ImgTrans/Filter2D/Filter2D_Demo.java apply_filter
149
+ @end_toggle
150
+
151
+ @add_toggle_python
152
+ @snippet python/tutorial_code/ImgTrans/Filter2D/filter2D.py apply_filter
153
+ @end_toggle
154
+
155
+ - The arguments denote:
156
+ - * src* : Source image
157
+ - * dst* : Destination image
158
+ - * ddepth* : The depth of * dst* . A negative value (such as \f$-1\f$) indicates that the depth is
90
159
the same as the source.
91
- -# *kernel*: The kernel to be scanned through the image
92
- -# *anchor*: The position of the anchor relative to its kernel. The location *Point(-1, -1)*
93
- indicates the center by default.
94
- -# *delta*: A value to be added to each pixel during the correlation. By default it is \f$0\f$
95
- -# *BORDER_DEFAULT*: We let this value by default (more details in the following tutorial)
160
+ - * kernel* : The kernel to be scanned through the image
161
+ - * anchor* : The position of the anchor relative to its kernel. The location * Point(-1, -1)*
162
+ indicates the center by default.
163
+ - * delta* : A value to be added to each pixel during the correlation. By default it is \f$0\f$
164
+ - * BORDER_DEFAULT* : We let this value by default (more details in the following tutorial)
96
165
97
- -# Our program will effectuate a * while* loop, each 500 ms the kernel size of our filter will be
166
+ - Our program will effectuate a * while* loop, each 500 ms the kernel size of our filter will be
98
167
updated in the range indicated.
99
168
100
169
Results
@@ -104,4 +173,4 @@ Results
104
173
result should be a window that shows an image blurred by a normalized filter. Each 0.5 seconds
105
174
the kernel size should change, as can be seen in the series of snapshots below:
106
175
107
- 
176
+ ![ ] ( images/filter_2d_tutorial_result.jpg )
0 commit comments