OpenCV  3.2.0
Open Source Computer Vision
Thresholding Operations using inRange

.2.0+dfsg_doc_tutorials_imgproc_threshold_inRange_threshold_inRange

Goal

In this tutorial you will learn how to:

  • Perform basic thresholding operations using OpenCV function 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 cv::threshold function.
  • In this tutorial, we will learn how to do it using 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

#include <iostream>
#include <stdlib.h>
using namespace std;
using namespace cv;
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 *);
int low_r=30, low_g=30, low_b=30;
int high_r=100, high_g=100, high_b=100;
int main()
{
Mat frame, frame_threshold;
VideoCapture cap(0);
namedWindow("Video Capture", WINDOW_NORMAL);
namedWindow("Object Detection", WINDOW_NORMAL);
//-- 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);
while((char)waitKey(1)!='q'){
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);
//-- Show the frames
imshow("Video Capture",frame);
imshow("Object Detection",frame_threshold);
}
return 0;
}
void on_low_r_thresh_trackbar(int, void *)
{
low_r = min(high_r-1, low_r);
setTrackbarPos("Low R","Object Detection", low_r);
}
void on_high_r_thresh_trackbar(int, void *)
{
high_r = max(high_r, low_r+1);
setTrackbarPos("High R", "Object Detection", high_r);
}
void on_low_g_thresh_trackbar(int, void *)
{
low_g = min(high_g-1, low_g);
setTrackbarPos("Low G","Object Detection", low_g);
}
void on_high_g_thresh_trackbar(int, void *)
{
high_g = max(high_g, low_g+1);
setTrackbarPos("High G", "Object Detection", high_g);
}
void on_low_b_thresh_trackbar(int, void *)
{
low_b= min(high_b-1, low_b);
setTrackbarPos("Low B","Object Detection", low_b);
}
void on_high_b_thresh_trackbar(int, void *)
{
high_b = max(high_b, low_b+1);
setTrackbarPos("High B", "Object Detection", high_b);
}

Explanation

  1. Let's check the general structure of the program:
    • Create two Matrix elements to store the frames
      Mat frame, frame_threshold;
    • Capture the video stream from default capturing device.
      VideoCapture cap(0);
    • Create a window to display the default frame and the threshold frame.
      namedWindow("Video Capture", WINDOW_NORMAL);
      namedWindow("Object Detection", WINDOW_NORMAL);
    • Create trackbars to set the range of RGB values
      //-- 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);
    • Until the user want the program to exit do the following
      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);
    • Show the images
      //-- Show the frames
      imshow("Video Capture",frame);
      imshow("Object Detection",frame_threshold);
    • For a trackbar which controls the lower range, say for example Red value:
      void on_low_r_thresh_trackbar(int, void *)
      {
      low_r = min(high_r-1, low_r);
      setTrackbarPos("Low R","Object Detection", low_r);
      }
    • For a trackbar which controls the upper range, say for example Red value:
      void on_high_r_thresh_trackbar(int, void *)
      {
      high_r = max(high_r, low_r+1);
      setTrackbarPos("High R", "Object Detection", high_r);
      }
    • 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

  1. After compiling this program, run it. The program will open two windows
  2. As you set the RGB range values from the trackbar, the resulting frame will be visible in the other window.

imgproc.hpp
cv::MatExpr::max
MatExpr max(const Mat &a, const Mat &b)
cv::inRange
void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)
Checks if array elements lie between the elements of two other arrays.
cv::MatExpr::min
MatExpr min(const Mat &a, const Mat &b)
cv::WINDOW_NORMAL
@ WINDOW_NORMAL
the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal s...
Definition: highgui.hpp:183
cv::VideoCapture
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:597
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::setTrackbarPos
void setTrackbarPos(const String &trackbarname, const String &winname, int pos)
Sets the trackbar position.
highgui.hpp
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::max
void max(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element maximum of two arrays or an array and a scalar.
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::min
void min(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element minimum of two arrays or an array and a scalar.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:606
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:741
cv::createTrackbar
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
cv
Definition: affine.hpp:52