Skip to content

Commit fc5e32c

Browse files
committed
Merge pull request opencv#6305 from rishirajsurti:master
2 parents ed508dc + 4a9170b commit fc5e32c

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

doc/tutorials/imgproc/table_of_content_imgproc.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ In this section you will learn about the image processing (manipulation) functio
5151

5252
After so much processing, it is time to decide which pixels stay!
5353

54+
- @subpage tutorial_threshold_inRange
55+
56+
*Compatibility:* \> OpenCV 2.0
57+
58+
*Author:* Rishiraj Surti
59+
60+
Thresholding operations using inRange function.
61+
5462
- @subpage tutorial_filter_2d
5563

5664
*Compatibility:* \> OpenCV 2.0
Loading
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Thresholding Operations using inRange {#tutorial_threshold_inRange}
2+
=============================
3+
4+
Goal
5+
----
6+
7+
In this tutorial you will learn how to:
8+
9+
- Perform basic thresholding operations using OpenCV function @ref cv::inRange
10+
- Detect an object based on the range of pixel values it has
11+
12+
Theory
13+
-----------
14+
- In the previous tutorial, we learnt how perform thresholding using @ref cv::threshold function.
15+
- In this tutorial, we will learn how to do it using @ref cv::inRange function.
16+
- The concept remains same, but now we add a range of pixel values we need.
17+
18+
Code
19+
----
20+
21+
The tutorial code's is shown lines below. You can also download it from
22+
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp)
23+
@include samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp
24+
25+
Explanation
26+
-----------
27+
28+
-# Let's check the general structure of the program:
29+
- Create two Matrix elements to store the frames
30+
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp mat
31+
- Capture the video stream from default capturing device.
32+
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp cap
33+
- Create a window to display the default frame and the threshold frame.
34+
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp window
35+
- Create trackbars to set the range of RGB values
36+
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp trackbar
37+
- Until the user want the program to exit do the following
38+
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp while
39+
- Show the images
40+
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp show
41+
- For a trackbar which controls the lower range, say for example Red value:
42+
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp low
43+
- For a trackbar which controls the upper range, say for example Red value:
44+
@snippet samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp high
45+
- It is necessary to find the maximum and minimum value to avoid discrepancies such as
46+
the high value of threshold becoming less the low value.
47+
48+
Results
49+
-------
50+
51+
-# After compiling this program, run it. The program will open two windows
52+
53+
-# As you set the RGB range values from the trackbar, the resulting frame will be visible in the other window.
54+
55+
![](images/Threshold_inRange_Tutorial_Result_input.jpeg)
56+
![](images/Threshold_inRange_Tutorial_Result_output.jpeg)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include "opencv2/imgproc.hpp"
2+
#include "opencv2/highgui.hpp"
3+
4+
#include <iostream>
5+
#include <stdlib.h>
6+
7+
using namespace std;
8+
using namespace cv;
9+
10+
/** Function Headers */
11+
void on_low_r_thresh_trackbar(int, void *);
12+
void on_high_r_thresh_trackbar(int, void *);
13+
void on_low_g_thresh_trackbar(int, void *);
14+
void on_high_g_thresh_trackbar(int, void *);
15+
void on_low_b_thresh_trackbar(int, void *);
16+
void on_high_b_thresh_trackbar(int, void *);
17+
18+
/** Global Variables */
19+
int low_r=30, low_g=30, low_b=30;
20+
int high_r=100, high_g=100, high_b=100;
21+
22+
/** @function main */
23+
int main()
24+
{
25+
//! [mat]
26+
Mat frame, frame_threshold;
27+
//! [mat]
28+
//! [cap]
29+
VideoCapture cap(0);
30+
//! [cap]
31+
//! [window]
32+
namedWindow("Video Capture", WINDOW_NORMAL);
33+
namedWindow("Object Detection", WINDOW_NORMAL);
34+
//! [window]
35+
//! [trackbar]
36+
//-- Trackbars to set thresholds for RGB values
37+
createTrackbar("Low R","Object Detection", &low_r, 255, on_low_r_thresh_trackbar);
38+
createTrackbar("High R","Object Detection", &high_r, 255, on_high_r_thresh_trackbar);
39+
createTrackbar("Low G","Object Detection", &low_g, 255, on_low_g_thresh_trackbar);
40+
createTrackbar("High G","Object Detection", &high_g, 255, on_high_g_thresh_trackbar);
41+
createTrackbar("Low B","Object Detection", &low_b, 255, on_low_b_thresh_trackbar);
42+
createTrackbar("High B","Object Detection", &high_b, 255, on_high_b_thresh_trackbar);
43+
//! [trackbar]
44+
while(char(waitKey(1))!='q'){
45+
//! [while]
46+
cap>>frame;
47+
if(frame.empty())
48+
break;
49+
//-- Detect the object based on RGB Range Values
50+
inRange(frame,Scalar(low_b,low_g,low_r), Scalar(high_b,high_g,high_r),frame_threshold);
51+
//! [while]
52+
//! [show]
53+
//-- Show the frames
54+
imshow("Video Capture",frame);
55+
imshow("Object Detection",frame_threshold);
56+
//! [show]
57+
}
58+
return 0;
59+
}
60+
//! [low]
61+
/** @function on_low_r_thresh_trackbar */
62+
void on_low_r_thresh_trackbar(int, void *)
63+
{
64+
low_r = min(high_r-1, low_r);
65+
setTrackbarPos("Low R","Object Detection", low_r);
66+
}
67+
//! [low]
68+
//! [high]
69+
/** @function on_high_r_thresh_trackbar */
70+
void on_high_r_thresh_trackbar(int, void *)
71+
{
72+
high_r = max(high_r, low_r+1);
73+
setTrackbarPos("High R", "Object Detection", high_r);
74+
}
75+
//![high]
76+
/** @function on_low_g_thresh_trackbar */
77+
void on_low_g_thresh_trackbar(int, void *)
78+
{
79+
low_g = min(high_g-1, low_g);
80+
setTrackbarPos("Low G","Object Detection", low_g);
81+
}
82+
83+
/** @function on_high_g_thresh_trackbar */
84+
void on_high_g_thresh_trackbar(int, void *)
85+
{
86+
high_g = max(high_g, low_g+1);
87+
setTrackbarPos("High G", "Object Detection", high_g);
88+
}
89+
90+
/** @function on_low_b_thresh_trackbar */
91+
void on_low_b_thresh_trackbar(int, void *)
92+
{
93+
low_b= min(high_b-1, low_b);
94+
setTrackbarPos("Low B","Object Detection", low_b);
95+
}
96+
97+
/** @function on_high_b_thresh_trackbar */
98+
void on_high_b_thresh_trackbar(int, void *)
99+
{
100+
high_b = max(high_b, low_b+1);
101+
setTrackbarPos("High B", "Object Detection", high_b);
102+
}

0 commit comments

Comments
 (0)