1
1
Sobel Derivatives {#tutorial_sobel_derivatives}
2
2
=================
3
3
4
+ @prev_tutorial{tutorial_copyMakeBorder}
5
+ @next_tutorial{tutorial_laplace_operator}
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:: Sobel to calculate the derivatives from an image.
10
- - Use the OpenCV function @ ref cv:: Scharr to calculate a more accurate derivative for a kernel of
12
+ - Use the OpenCV function ** Sobel() ** to calculate the derivatives from an image.
13
+ - Use the OpenCV function ** Scharr() ** to calculate a more accurate derivative for a kernel of
11
14
size \f$3 \cdot 3\f$
12
15
13
16
Theory
@@ -83,7 +86,7 @@ Assuming that the image to be operated is \f$I\f$:
83
86
@note
84
87
When the size of the kernel is ` 3 ` , the Sobel kernel shown above may produce noticeable
85
88
inaccuracies (after all, Sobel is only an approximation of the derivative). OpenCV addresses
86
- this inaccuracy for kernels of size 3 by using the @ ref cv:: Scharr function. This is as fast
89
+ this inaccuracy for kernels of size 3 by using the ** Scharr() ** function. This is as fast
87
90
but more accurate than the standar Sobel function. It implements the following kernels:
88
91
\f[ G_ {x} = \begin{bmatrix}
89
92
-3 & 0 & +3 \\
@@ -95,9 +98,9 @@ Assuming that the image to be operated is \f$I\f$:
95
98
+3 & +10 & +3
96
99
\end{bmatrix}\f]
97
100
@note
98
- You can check out more information of this function in the OpenCV reference ( @ ref cv:: Scharr ) .
99
- Also, in the sample code below, you will notice that above the code for @ ref cv:: Sobel function
100
- there is also code for the @ ref cv:: Scharr function commented. Uncommenting it (and obviously
101
+ You can check out more information of this function in the OpenCV reference - ** Scharr() ** .
102
+ Also, in the sample code below, you will notice that above the code for ** Sobel() ** function
103
+ there is also code for the ** Scharr() ** function commented. Uncommenting it (and obviously
101
104
commenting the Sobel stuff) should give you an idea of how this function works.
102
105
103
106
Code
@@ -107,28 +110,55 @@ Code
107
110
- Applies the * Sobel Operator* and generates as output an image with the detected * edges*
108
111
bright on a darker background.
109
112
110
- -# The tutorial code's is shown lines below. You can also download it from
111
- [ here] ( https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp )
112
- @include samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp
113
+ -# The tutorial code's is shown lines below.
114
+
115
+ @add_toggle_cpp
116
+ You can also download it from
117
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp )
118
+ @include samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp
119
+ @end_toggle
120
+
121
+ @add_toggle_java
122
+ You can also download it from
123
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgTrans/SobelDemo/SobelDemo.java )
124
+ @include samples/java/tutorial_code/ImgTrans/SobelDemo/SobelDemo.java
125
+ @end_toggle
126
+
127
+ @add_toggle_python
128
+ You can also download it from
129
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/ImgTrans/SobelDemo/sobel_demo.py )
130
+ @include samples/python/tutorial_code/ImgTrans/SobelDemo/sobel_demo.py
131
+ @end_toggle
113
132
114
133
Explanation
115
134
-----------
116
135
117
- -# First we declare the variables we are going to use:
118
- @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp variables
119
- -# As usual we load our source image * src* :
120
- @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp load
121
- -# First, we apply a @ref cv::GaussianBlur to our image to reduce the noise ( kernel size = 3 )
122
- @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp reduce_noise
123
- -# Now we convert our filtered image to grayscale:
124
- @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp convert_to_gray
125
- -# Second, we calculate the "* derivatives* " in * x* and * y* directions. For this, we use the
126
- function @ref cv::Sobel as shown below:
127
- @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp sobel
136
+ #### Declare variables
137
+
138
+ @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp variables
139
+
140
+ #### Load source image
141
+
142
+ @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp load
143
+
144
+ #### Reduce noise
145
+
146
+ @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp reduce_noise
147
+
148
+ #### Grayscale
149
+
150
+ @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp convert_to_gray
151
+
152
+ #### Sobel Operator
153
+
154
+ @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp sobel
155
+
156
+ - We calculate the "derivatives" in * x* and * y* directions. For this, we use the
157
+ function ** Sobel()** as shown below:
128
158
The function takes the following arguments:
129
159
130
160
- * src_gray* : In our example, the input image. Here it is * CV_8U*
131
- - *grad_x*/ *grad_y*: The output image.
161
+ - * grad_x* / * grad_y* : The output image.
132
162
- * ddepth* : The depth of the output image. We set it to * CV_16S* to avoid overflow.
133
163
- * x_order* : The order of the derivative in ** x** direction.
134
164
- * y_order* : The order of the derivative in ** y** direction.
@@ -137,13 +167,20 @@ Explanation
137
167
Notice that to calculate the gradient in * x* direction we use: \f$x_ {order}= 1\f$ and
138
168
\f$y_ {order} = 0\f$. We do analogously for the * y* direction.
139
169
140
- -# We convert our partial results back to * CV_8U* :
141
- @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp convert
142
- -# Finally, we try to approximate the * gradient* by adding both directional gradients (note that
143
- this is not an exact calculation at all! but it is good for our purposes).
144
- @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp blend
145
- -# Finally, we show our result:
146
- @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp display
170
+ #### Convert output to a CV_8U image
171
+
172
+ @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp convert
173
+
174
+ #### Gradient
175
+
176
+ @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp blend
177
+
178
+ We try to approximate the * gradient* by adding both directional gradients (note that
179
+ this is not an exact calculation at all! but it is good for our purposes).
180
+
181
+ #### Show results
182
+
183
+ @snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp display
147
184
148
185
Results
149
186
-------
0 commit comments