1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #11543 from catree:add_tutorial_imgproc_java_python

This commit is contained in:
Alexander Alekhin
2018-05-22 12:26:34 +00:00
19 changed files with 1429 additions and 340 deletions
@@ -11,7 +11,7 @@ In this tutorial you will learn how to:
Theory
------
The *Canny Edge detector* was developed by John F. Canny in 1986. Also known to many as the
The *Canny Edge detector* @cite Canny86 was developed by John F. Canny in 1986. Also known to many as the
*optimal detector*, the Canny algorithm aims to satisfy three main criteria:
- **Low error rate:** Meaning a good detection of only existent edges.
- **Good localization:** The distance between edge pixels detected and real edge pixels have
@@ -66,19 +66,33 @@ The *Canny Edge detector* was developed by John F. Canny in 1986. Also known to
Code
----
-# **What does this program do?**
@add_toggle_cpp
- 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/CannyDetector_Demo.cpp)
@include samples/cpp/tutorial_code/ImgTrans/CannyDetector_Demo.cpp
@end_toggle
@add_toggle_java
- The tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ImgTrans/canny_detector/CannyDetectorDemo.java)
@include samples/java/tutorial_code/ImgTrans/canny_detector/CannyDetectorDemo.java
@end_toggle
@add_toggle_python
- The tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/ImgTrans/canny_detector/CannyDetector_Demo.py)
@include samples/python/tutorial_code/ImgTrans/canny_detector/CannyDetector_Demo.py
@end_toggle
- **What does this program do?**
- Asks the user to enter a numerical value to set the lower threshold for our *Canny Edge
Detector* (by means of a Trackbar).
- Applies the *Canny Detector* and generates a **mask** (bright lines representing the edges
on a black background).
- Applies the mask obtained on the original image and display it in a window.
-# 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/CannyDetector_Demo.cpp)
@include samples/cpp/tutorial_code/ImgTrans/CannyDetector_Demo.cpp
Explanation
-----------
Explanation (C++ code)
----------------------
-# Create some needed variables:
@snippet cpp/tutorial_code/ImgTrans/CannyDetector_Demo.cpp variables
@@ -45,61 +45,91 @@ Theory
Code
----
-# **What does this program do?**
- **What does this program do?**
- Loads an image
- Each second, apply 1 of 4 different remapping processes to the image and display them
indefinitely in a window.
- Wait for the user to exit the program
-# The tutorial code's is shown lines below. You can also download it from
@add_toggle_cpp
- 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/Remap_Demo.cpp)
@include samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp
@end_toggle
@add_toggle_java
- The tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java)
@include samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java
@end_toggle
@add_toggle_python
- The tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py)
@include samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py
@end_toggle
Explanation
-----------
-# Create some variables we will use:
@code{.cpp}
Mat src, dst;
Mat map_x, map_y;
char* remap_window = "Remap demo";
int ind = 0;
@endcode
-# Load an image:
@code{.cpp}
src = imread( argv[1], 1 );
@endcode
-# Create the destination image and the two mapping matrices (for x and y )
@code{.cpp}
dst.create( src.size(), src.type() );
map_x.create( src.size(), CV_32FC1 );
map_y.create( src.size(), CV_32FC1 );
@endcode
-# Create a window to display results
@code{.cpp}
namedWindow( remap_window, WINDOW_AUTOSIZE );
@endcode
-# Establish a loop. Each 1000 ms we update our mapping matrices (*mat_x* and *mat_y*) and apply
- Load an image:
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Load
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Load
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Load
@end_toggle
- Create the destination image and the two mapping matrices (for x and y )
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Create
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Create
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Create
@end_toggle
- Create a window to display results
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Window
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Window
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Window
@end_toggle
- Establish a loop. Each 1000 ms we update our mapping matrices (*mat_x* and *mat_y*) and apply
them to our source image:
@code{.cpp}
while( true )
{
/// Each 1 sec. Press ESC to exit the program
char c = (char)waitKey( 1000 );
if( c == 27 )
{ break; }
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Loop
@end_toggle
/// Update map_x & map_y. Then apply remap
update_map();
remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0,0, 0) );
@add_toggle_java
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Loop
@end_toggle
/// Display results
imshow( remap_window, dst );
}
@endcode
The function that applies the remapping is @ref cv::remap . We give the following arguments:
@add_toggle_python
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Loop
@end_toggle
- The function that applies the remapping is @ref cv::remap . We give the following arguments:
- **src**: Source image
- **dst**: Destination image of same size as *src*
- **map_x**: The mapping function in the x direction. It is equivalent to the first component
@@ -112,9 +142,9 @@ Explanation
How do we update our mapping matrices *mat_x* and *mat_y*? Go on reading:
-# **Updating the mapping matrices:** We are going to perform 4 different mappings:
- **Updating the mapping matrices:** We are going to perform 4 different mappings:
-# Reduce the picture to half its size and will display it in the middle:
\f[h(i,j) = ( 2*i - src.cols/2 + 0.5, 2*j - src.rows/2 + 0.5)\f]
\f[h(i,j) = ( 2 \times i - src.cols/2 + 0.5, 2 \times j - src.rows/2 + 0.5)\f]
for all pairs \f$(i,j)\f$ such that: \f$\dfrac{src.cols}{4}<i<\dfrac{3 \cdot src.cols}{4}\f$ and
\f$\dfrac{src.rows}{4}<j<\dfrac{3 \cdot src.rows}{4}\f$
-# Turn the image upside down: \f$h( i, j ) = (i, src.rows - j)\f$
@@ -123,41 +153,18 @@ Explanation
This is expressed in the following snippet. Here, *map_x* represents the first coordinate of
*h(i,j)* and *map_y* the second coordinate.
@code{.cpp}
for( int j = 0; j < src.rows; j++ )
{ for( int i = 0; i < src.cols; i++ )
{
switch( ind )
{
case 0:
if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
{
map_x.at<float>(j,i) = 2*( i - src.cols*0.25 ) + 0.5 ;
map_y.at<float>(j,i) = 2*( j - src.rows*0.25 ) + 0.5 ;
}
else
{ map_x.at<float>(j,i) = 0 ;
map_y.at<float>(j,i) = 0 ;
}
break;
case 1:
map_x.at<float>(j,i) = i ;
map_y.at<float>(j,i) = src.rows - j ;
break;
case 2:
map_x.at<float>(j,i) = src.cols - i ;
map_y.at<float>(j,i) = j ;
break;
case 3:
map_x.at<float>(j,i) = src.cols - i ;
map_y.at<float>(j,i) = src.rows - j ;
break;
} // end of switch
}
}
ind++;
}
@endcode
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Update
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Update
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Update
@end_toggle
Result
------
@@ -15,6 +15,8 @@ In this section you will learn about the image processing (manipulation) functio
- @subpage tutorial_erosion_dilatation
*Languages:* C++, Java, Python
*Compatibility:* \> OpenCV 2.0
Author: Ana Huamán
@@ -23,6 +25,8 @@ In this section you will learn about the image processing (manipulation) functio
- @subpage tutorial_opening_closing_hats
*Languages:* C++, Java, Python
*Compatibility:* \> OpenCV 2.0
*Author:* Ana Huamán
@@ -61,6 +65,8 @@ In this section you will learn about the image processing (manipulation) functio
- @subpage tutorial_threshold
*Languages:* C++, Java, Python
*Compatibility:* \> OpenCV 2.0
*Author:* Ana Huamán
@@ -69,6 +75,8 @@ In this section you will learn about the image processing (manipulation) functio
- @subpage tutorial_threshold_inRange
*Languages:* C++, Java, Python
*Compatibility:* \> OpenCV 2.0
*Author:* Rishiraj Surti
@@ -117,6 +125,8 @@ In this section you will learn about the image processing (manipulation) functio
- @subpage tutorial_canny_detector
*Languages:* C++, Java, Python
*Compatibility:* \> OpenCV 2.0
*Author:* Ana Huamán
@@ -145,6 +155,8 @@ In this section you will learn about the image processing (manipulation) functio
- @subpage tutorial_remap
*Languages:* C++, Java, Python
*Compatibility:* \> OpenCV 2.0
*Author:* Ana Huamán
@@ -96,43 +96,101 @@ Thresholding?
Code
----
@add_toggle_cpp
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/ImgProc/Threshold.cpp)
@include samples/cpp/tutorial_code/ImgProc/Threshold.cpp
@end_toggle
@add_toggle_java
The tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ImgProc/threshold/Threshold.java)
@include samples/java/tutorial_code/ImgProc/threshold/Threshold.java
@end_toggle
@add_toggle_python
The tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/imgProc/threshold/threshold.py)
@include samples/python/tutorial_code/imgProc/threshold/threshold.py
@end_toggle
Explanation
-----------
-# Let's check the general structure of the program:
- Load an image. If it is BGR we convert it to Grayscale. For this, remember that we can use
Let's check the general structure of the program:
- Load an image. If it is BGR we convert it to Grayscale. For this, remember that we can use
the function @ref cv::cvtColor :
@snippet cpp/tutorial_code/ImgProc/Threshold.cpp load
- Create a window to display the result
@snippet cpp/tutorial_code/ImgProc/Threshold.cpp window
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold.cpp load
@end_toggle
- Create \f$2\f$ trackbars for the user to enter user input:
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold/Threshold.java load
@end_toggle
- **Type of thresholding**: Binary, To Zero, etc...
- **Threshold value**
@snippet cpp/tutorial_code/ImgProc/Threshold.cpp trackbar
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold/threshold.py load
@end_toggle
- Wait until the user enters the threshold value, the type of thresholding (or until the
program exits)
- Whenever the user changes the value of any of the Trackbars, the function *Threshold_Demo*
is called:
@snippet cpp/tutorial_code/ImgProc/Threshold.cpp Threshold_Demo
- Create a window to display the result
As you can see, the function @ref cv::threshold is invoked. We give \f$5\f$ parameters:
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold.cpp window
@end_toggle
- *src_gray*: Our input image
- *dst*: Destination (output) image
- *threshold_value*: The \f$thresh\f$ value with respect to which the thresholding operation
is made
- *max_BINARY_value*: The value used with the Binary thresholding operations (to set the
chosen pixels)
- *threshold_type*: One of the \f$5\f$ thresholding operations. They are listed in the
comment section of the function above.
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold/Threshold.java window
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold/threshold.py window
@end_toggle
- Create \f$2\f$ trackbars for the user to enter user input:
- **Type of thresholding**: Binary, To Zero, etc...
- **Threshold value**
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold.cpp trackbar
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold/Threshold.java trackbar
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold/threshold.py trackbar
@end_toggle
- Wait until the user enters the threshold value, the type of thresholding (or until the
program exits)
- Whenever the user changes the value of any of the Trackbars, the function *Threshold_Demo*
(*update* in Java) is called:
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold.cpp Threshold_Demo
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold/Threshold.java Threshold_Demo
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold/threshold.py Threshold_Demo
@end_toggle
As you can see, the function @ref cv::threshold is invoked. We give \f$5\f$ parameters in C++ code:
- *src_gray*: Our input image
- *dst*: Destination (output) image
- *threshold_value*: The \f$thresh\f$ value with respect to which the thresholding operation
is made
- *max_BINARY_value*: The value used with the Binary thresholding operations (to set the
chosen pixels)
- *threshold_type*: One of the \f$5\f$ thresholding operations. They are listed in the
comment section of the function above.
Results
-------
Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

@@ -1,56 +1,173 @@
Thresholding Operations using inRange {#tutorial_threshold_inRange}
=============================
=====================================
Goal
----
In this tutorial you will learn how to:
- Perform basic thresholding operations using OpenCV function @ref cv::inRange
- Detect an object based on the range of pixel values it has
- Perform basic thresholding operations using OpenCV @ref cv::inRange function.
- Detect an object based on the range of pixel values in the HSV colorspace.
Theory
-----------
- In the previous tutorial, we learnt how perform thresholding using @ref cv::threshold function.
------
- In the previous tutorial, we learnt how to perform thresholding using @ref cv::threshold function.
- In this tutorial, we will learn how to do it using @ref cv::inRange function.
- The concept remains same, but now we add a range of pixel values we need.
- The concept remains the same, but now we add a range of pixel values we need.
HSV colorspace
--------------
<a href="https://en.wikipedia.org/wiki/HSL_and_HSV">HSV</a> (hue, saturation, value) colorspace
is a model to represent the colorspace similar to the RGB color model. Since the hue channel
models the color type, it is very useful in image processing tasks that need to segment objects
based on its color. Variation of the saturation goes from unsaturated to represent shades of gray and
fully saturated (no white component). Value channel describes the brightness or the intensity of the
color. Next image shows the HSV cylinder.
![By SharkDderivative work: SharkD [CC BY-SA 3.0 or GFDL], via Wikimedia Commons](images/Threshold_inRange_HSV_colorspace.jpg)
Since colors in the RGB colorspace are coded using the three channels, it is more difficult to segment
an object in the image based on its color.
![By SharkD [GFDL or CC BY-SA 4.0], from Wikimedia Commons](images/Threshold_inRange_RGB_colorspace.jpg)
Formulas used to convert from one colorspace to another colorspace using @ref cv::cvtColor function
are described in @ref imgproc_color_conversions
Code
----
@add_toggle_cpp
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/ImgProc/Threshold_inRange.cpp)
@include samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp
@end_toggle
@add_toggle_java
The tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java)
@include samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java
@end_toggle
@add_toggle_python
The tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py)
@include samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py
@end_toggle
Explanation
-----------
-# Let's check the general structure of the program:
- Create two Matrix elements to store the frames
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp mat
- Capture the video stream from default capturing device.
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp cap
- Create a window to display the default frame and the threshold frame.
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp window
- Create trackbars to set the range of RGB values
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp trackbar
- Until the user want the program to exit do the following
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp while
- Show the images
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp show
- For a trackbar which controls the lower range, say for example Red value:
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp low
- For a trackbar which controls the upper range, say for example Red value:
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp high
- It is necessary to find the maximum and minimum value to avoid discrepancies such as
the high value of threshold becoming less the low value.
Let's check the general structure of the program:
- Capture the video stream from default or supplied capturing device.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp cap
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java cap
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py cap
@end_toggle
- Create a window to display the default frame and the threshold frame.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp window
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java window
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py window
@end_toggle
- Create the trackbars to set the range of HSV values
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp trackbar
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java trackbar
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py trackbar
@end_toggle
- Until the user want the program to exit do the following
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp while
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java while
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py while
@end_toggle
- Show the images
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp show
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java show
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py show
@end_toggle
- For a trackbar which controls the lower range, say for example hue value:
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp low
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java low
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py low
@end_toggle
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp low
- For a trackbar which controls the upper range, say for example hue value:
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp high
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java high
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py high
@end_toggle
- It is necessary to find the maximum and minimum value to avoid discrepancies such as
the high value of threshold becoming less than the low value.
Results
-------
-# After compiling this program, run it. The program will open two windows
- After compiling this program, run it. The program will open two windows
-# As you set the RGB range values from the trackbar, the resulting frame will be visible in the other window.
- As you set the range values from the trackbar, the resulting frame will be visible in the other window.
![](images/Threshold_inRange_Tutorial_Result_input.jpeg)
![](images/Threshold_inRange_Tutorial_Result_output.jpeg)