mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 23:33:05 +04:00
Tutorial Sobel Derivatives
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
Sobel Derivatives {#tutorial_sobel_derivatives}
|
||||
=================
|
||||
|
||||
@prev_tutorial{tutorial_copyMakeBorder}
|
||||
@next_tutorial{tutorial_laplace_operator}
|
||||
|
||||
Goal
|
||||
----
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
- Use the OpenCV function @ref cv::Sobel to calculate the derivatives from an image.
|
||||
- Use the OpenCV function @ref cv::Scharr to calculate a more accurate derivative for a kernel of
|
||||
- Use the OpenCV function **Sobel()** to calculate the derivatives from an image.
|
||||
- Use the OpenCV function **Scharr()** to calculate a more accurate derivative for a kernel of
|
||||
size \f$3 \cdot 3\f$
|
||||
|
||||
Theory
|
||||
@@ -83,7 +86,7 @@ Assuming that the image to be operated is \f$I\f$:
|
||||
@note
|
||||
When the size of the kernel is `3`, the Sobel kernel shown above may produce noticeable
|
||||
inaccuracies (after all, Sobel is only an approximation of the derivative). OpenCV addresses
|
||||
this inaccuracy for kernels of size 3 by using the @ref cv::Scharr function. This is as fast
|
||||
this inaccuracy for kernels of size 3 by using the **Scharr()** function. This is as fast
|
||||
but more accurate than the standar Sobel function. It implements the following kernels:
|
||||
\f[G_{x} = \begin{bmatrix}
|
||||
-3 & 0 & +3 \\
|
||||
@@ -95,9 +98,9 @@ Assuming that the image to be operated is \f$I\f$:
|
||||
+3 & +10 & +3
|
||||
\end{bmatrix}\f]
|
||||
@note
|
||||
You can check out more information of this function in the OpenCV reference (@ref cv::Scharr ).
|
||||
Also, in the sample code below, you will notice that above the code for @ref cv::Sobel function
|
||||
there is also code for the @ref cv::Scharr function commented. Uncommenting it (and obviously
|
||||
You can check out more information of this function in the OpenCV reference - **Scharr()** .
|
||||
Also, in the sample code below, you will notice that above the code for **Sobel()** function
|
||||
there is also code for the **Scharr()** function commented. Uncommenting it (and obviously
|
||||
commenting the Sobel stuff) should give you an idea of how this function works.
|
||||
|
||||
Code
|
||||
@@ -107,28 +110,55 @@ Code
|
||||
- Applies the *Sobel Operator* and generates as output an image with the detected *edges*
|
||||
bright on a darker background.
|
||||
|
||||
-# The 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/ImgTrans/Sobel_Demo.cpp)
|
||||
@include samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp
|
||||
-# The tutorial code's is shown lines below.
|
||||
|
||||
@add_toggle_cpp
|
||||
You can also download it from
|
||||
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp)
|
||||
@include samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_java
|
||||
You can also download it from
|
||||
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgTrans/SobelDemo/SobelDemo.java)
|
||||
@include samples/java/tutorial_code/ImgTrans/SobelDemo/SobelDemo.java
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
You can also download it from
|
||||
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/ImgTrans/SobelDemo/sobel_demo.py)
|
||||
@include samples/python/tutorial_code/ImgTrans/SobelDemo/sobel_demo.py
|
||||
@end_toggle
|
||||
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
-# First we declare the variables we are going to use:
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp variables
|
||||
-# As usual we load our source image *src*:
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp load
|
||||
-# First, we apply a @ref cv::GaussianBlur to our image to reduce the noise ( kernel size = 3 )
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp reduce_noise
|
||||
-# Now we convert our filtered image to grayscale:
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp convert_to_gray
|
||||
-# Second, we calculate the "*derivatives*" in *x* and *y* directions. For this, we use the
|
||||
function @ref cv::Sobel as shown below:
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp sobel
|
||||
#### Declare variables
|
||||
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp variables
|
||||
|
||||
#### Load source image
|
||||
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp load
|
||||
|
||||
#### Reduce noise
|
||||
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp reduce_noise
|
||||
|
||||
#### Grayscale
|
||||
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp convert_to_gray
|
||||
|
||||
#### Sobel Operator
|
||||
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp sobel
|
||||
|
||||
- We calculate the "derivatives" in *x* and *y* directions. For this, we use the
|
||||
function **Sobel()** as shown below:
|
||||
The function takes the following arguments:
|
||||
|
||||
- *src_gray*: In our example, the input image. Here it is *CV_8U*
|
||||
- *grad_x*/*grad_y*: The output image.
|
||||
- *grad_x* / *grad_y* : The output image.
|
||||
- *ddepth*: The depth of the output image. We set it to *CV_16S* to avoid overflow.
|
||||
- *x_order*: The order of the derivative in **x** direction.
|
||||
- *y_order*: The order of the derivative in **y** direction.
|
||||
@@ -137,13 +167,20 @@ Explanation
|
||||
Notice that to calculate the gradient in *x* direction we use: \f$x_{order}= 1\f$ and
|
||||
\f$y_{order} = 0\f$. We do analogously for the *y* direction.
|
||||
|
||||
-# We convert our partial results back to *CV_8U*:
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp convert
|
||||
-# Finally, we try to approximate the *gradient* by adding both directional gradients (note that
|
||||
this is not an exact calculation at all! but it is good for our purposes).
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp blend
|
||||
-# Finally, we show our result:
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp display
|
||||
#### Convert output to a CV_8U image
|
||||
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp convert
|
||||
|
||||
#### Gradient
|
||||
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp blend
|
||||
|
||||
We try to approximate the *gradient* by adding both directional gradients (note that
|
||||
this is not an exact calculation at all! but it is good for our purposes).
|
||||
|
||||
#### Show results
|
||||
|
||||
@snippet cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp display
|
||||
|
||||
Results
|
||||
-------
|
||||
|
||||
@@ -91,6 +91,8 @@ In this section you will learn about the image processing (manipulation) functio
|
||||
|
||||
- @subpage tutorial_sobel_derivatives
|
||||
|
||||
*Languages:* C++, Java, Python
|
||||
|
||||
*Compatibility:* \> OpenCV 2.0
|
||||
|
||||
*Author:* Ana Huamán
|
||||
|
||||
Reference in New Issue
Block a user