diff --git a/doc/tutorials/imgproc/table_of_content_imgproc.markdown b/doc/tutorials/imgproc/table_of_content_imgproc.markdown index 77de126c21..486c6449c5 100644 --- a/doc/tutorials/imgproc/table_of_content_imgproc.markdown +++ b/doc/tutorials/imgproc/table_of_content_imgproc.markdown @@ -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 diff --git a/doc/tutorials/imgproc/threshold_inRange/images/Threshold_inRange_Tutorial_Result_input.jpeg b/doc/tutorials/imgproc/threshold_inRange/images/Threshold_inRange_Tutorial_Result_input.jpeg new file mode 100644 index 0000000000..6d23ca1325 Binary files /dev/null and b/doc/tutorials/imgproc/threshold_inRange/images/Threshold_inRange_Tutorial_Result_input.jpeg differ diff --git a/doc/tutorials/imgproc/threshold_inRange/images/Threshold_inRange_Tutorial_Result_output.jpeg b/doc/tutorials/imgproc/threshold_inRange/images/Threshold_inRange_Tutorial_Result_output.jpeg new file mode 100644 index 0000000000..c5865132a2 Binary files /dev/null and b/doc/tutorials/imgproc/threshold_inRange/images/Threshold_inRange_Tutorial_Result_output.jpeg differ diff --git a/doc/tutorials/imgproc/threshold_inRange/threshold_inRange.markdown b/doc/tutorials/imgproc/threshold_inRange/threshold_inRange.markdown new file mode 100644 index 0000000000..edafc06818 --- /dev/null +++ b/doc/tutorials/imgproc/threshold_inRange/threshold_inRange.markdown @@ -0,0 +1,56 @@ +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) diff --git a/samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp b/samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp new file mode 100644 index 0000000000..3801b30e7b --- /dev/null +++ b/samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp @@ -0,0 +1,102 @@ +#include "opencv2/imgproc.hpp" +#include "opencv2/highgui.hpp" + +#include +#include + +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); +}