Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
4a9170ba
Commit
4a9170ba
authored
Mar 21, 2016
by
rishirajsurti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Thresholding Tutorial using inRange function on a video
parent
4e479d58
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
166 additions
and
0 deletions
+166
-0
table_of_content_imgproc.markdown
doc/tutorials/imgproc/table_of_content_imgproc.markdown
+8
-0
Threshold_inRange_Tutorial_Result_input.jpeg
...Range/images/Threshold_inRange_Tutorial_Result_input.jpeg
+0
-0
Threshold_inRange_Tutorial_Result_output.jpeg
...ange/images/Threshold_inRange_Tutorial_Result_output.jpeg
+0
-0
threshold_inRange.markdown
...ials/imgproc/threshold_inRange/threshold_inRange.markdown
+56
-0
Threshold_inRange.cpp
samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp
+102
-0
No files found.
doc/tutorials/imgproc/table_of_content_imgproc.markdown
View file @
4a9170ba
...
...
@@ -51,6 +51,14 @@ In this section you will learn about the image processing (manipulation) functio
After so much processing, it is time to decide which pixels stay!
-
@subpage tutorial_threshold_inRange
*Compatibility:* \> OpenCV 2.0
*Author:* Rishiraj Surti
Thresholding operations using inRange function.
-
@subpage tutorial_filter_2d
*Compatibility:* \> OpenCV 2.0
...
...
doc/tutorials/imgproc/threshold_inRange/images/Threshold_inRange_Tutorial_Result_input.jpeg
0 → 100644
View file @
4a9170ba
35 KB
doc/tutorials/imgproc/threshold_inRange/images/Threshold_inRange_Tutorial_Result_output.jpeg
0 → 100644
View file @
4a9170ba
10.7 KB
doc/tutorials/imgproc/threshold_inRange/threshold_inRange.markdown
0 → 100644
View file @
4a9170ba
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
Theory
-----------
-
In the previous tutorial, we learnt how 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.
Code
----
The tutorial code's is shown lines below. You can also download it from
[
here
](
https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp
)
@include samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp
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.
Results
-------
-# 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.
![](images/Threshold_inRange_Tutorial_Result_input.jpeg)
![](images/Threshold_inRange_Tutorial_Result_output.jpeg)
samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp
0 → 100644
View file @
4a9170ba
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <stdlib.h>
using
namespace
std
;
using
namespace
cv
;
/** Function Headers */
void
on_low_r_thresh_trackbar
(
int
,
void
*
);
void
on_high_r_thresh_trackbar
(
int
,
void
*
);
void
on_low_g_thresh_trackbar
(
int
,
void
*
);
void
on_high_g_thresh_trackbar
(
int
,
void
*
);
void
on_low_b_thresh_trackbar
(
int
,
void
*
);
void
on_high_b_thresh_trackbar
(
int
,
void
*
);
/** Global Variables */
int
low_r
=
30
,
low_g
=
30
,
low_b
=
30
;
int
high_r
=
100
,
high_g
=
100
,
high_b
=
100
;
/** @function main */
int
main
()
{
//! [mat]
Mat
frame
,
frame_threshold
;
//! [mat]
//! [cap]
VideoCapture
cap
(
0
);
//! [cap]
//! [window]
namedWindow
(
"Video Capture"
,
WINDOW_NORMAL
);
namedWindow
(
"Object Detection"
,
WINDOW_NORMAL
);
//! [window]
//! [trackbar]
//-- Trackbars to set thresholds for RGB values
createTrackbar
(
"Low R"
,
"Object Detection"
,
&
low_r
,
255
,
on_low_r_thresh_trackbar
);
createTrackbar
(
"High R"
,
"Object Detection"
,
&
high_r
,
255
,
on_high_r_thresh_trackbar
);
createTrackbar
(
"Low G"
,
"Object Detection"
,
&
low_g
,
255
,
on_low_g_thresh_trackbar
);
createTrackbar
(
"High G"
,
"Object Detection"
,
&
high_g
,
255
,
on_high_g_thresh_trackbar
);
createTrackbar
(
"Low B"
,
"Object Detection"
,
&
low_b
,
255
,
on_low_b_thresh_trackbar
);
createTrackbar
(
"High B"
,
"Object Detection"
,
&
high_b
,
255
,
on_high_b_thresh_trackbar
);
//! [trackbar]
while
(
char
(
waitKey
(
1
))
!=
'q'
){
//! [while]
cap
>>
frame
;
if
(
frame
.
empty
())
break
;
//-- Detect the object based on RGB Range Values
inRange
(
frame
,
Scalar
(
low_b
,
low_g
,
low_r
),
Scalar
(
high_b
,
high_g
,
high_r
),
frame_threshold
);
//! [while]
//! [show]
//-- Show the frames
imshow
(
"Video Capture"
,
frame
);
imshow
(
"Object Detection"
,
frame_threshold
);
//! [show]
}
return
0
;
}
//! [low]
/** @function on_low_r_thresh_trackbar */
void
on_low_r_thresh_trackbar
(
int
,
void
*
)
{
low_r
=
min
(
high_r
-
1
,
low_r
);
setTrackbarPos
(
"Low R"
,
"Object Detection"
,
low_r
);
}
//! [low]
//! [high]
/** @function on_high_r_thresh_trackbar */
void
on_high_r_thresh_trackbar
(
int
,
void
*
)
{
high_r
=
max
(
high_r
,
low_r
+
1
);
setTrackbarPos
(
"High R"
,
"Object Detection"
,
high_r
);
}
//![high]
/** @function on_low_g_thresh_trackbar */
void
on_low_g_thresh_trackbar
(
int
,
void
*
)
{
low_g
=
min
(
high_g
-
1
,
low_g
);
setTrackbarPos
(
"Low G"
,
"Object Detection"
,
low_g
);
}
/** @function on_high_g_thresh_trackbar */
void
on_high_g_thresh_trackbar
(
int
,
void
*
)
{
high_g
=
max
(
high_g
,
low_g
+
1
);
setTrackbarPos
(
"High G"
,
"Object Detection"
,
high_g
);
}
/** @function on_low_b_thresh_trackbar */
void
on_low_b_thresh_trackbar
(
int
,
void
*
)
{
low_b
=
min
(
high_b
-
1
,
low_b
);
setTrackbarPos
(
"Low B"
,
"Object Detection"
,
low_b
);
}
/** @function on_high_b_thresh_trackbar */
void
on_high_b_thresh_trackbar
(
int
,
void
*
)
{
high_b
=
max
(
high_b
,
low_b
+
1
);
setTrackbarPos
(
"High B"
,
"Object Detection"
,
high_b
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment