1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
Thresholding Operations using inRange {#tutorial_threshold_inRange}
=====================================
@tableofcontents
@prev_tutorial{tutorial_threshold}
@next_tutorial{tutorial_filter_2d}
| | |
| -: | :- |
| Original author | Lorena GarcĂa |
| Compatibility | Rishiraj Surti |
Goal
----
In this tutorial you will learn how to:
- 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 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 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/4.x/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/4.x/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/4.x/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:
- 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
- As you set the range values from the trackbar, the resulting frame will be visible in the other window.


|