OpenCV
Open Source Computer Vision
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Thresholding Operations using inRange

Prev Tutorial: Basic Thresholding Operations
Next Tutorial: Making your own linear filters!

Original author Lorena García
Compatibility Rishiraj Surti

Goal

In this tutorial you will learn how to:

  • Perform basic thresholding operations using OpenCV 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 cv::threshold function.
  • In this tutorial, we will learn how to do it using cv::inRange function.
  • The concept remains the same, but now we add a range of pixel values we need.

HSV colorspace

HSV (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

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

Formulas used to convert from one colorspace to another colorspace using cv::cvtColor function are described in Color conversions

Code

The tutorial code's is shown lines below. You can also download it from here

#include <iostream>
using namespace cv;
const int max_value_H = 360/2;
const int max_value = 255;
const String window_capture_name = "Video Capture";
const String window_detection_name = "Object Detection";
int low_H = 0, low_S = 0, low_V = 0;
int high_H = max_value_H, high_S = max_value, high_V = max_value;
static void on_low_H_thresh_trackbar(int, void *)
{
low_H = min(high_H-1, low_H);
setTrackbarPos("Low H", window_detection_name, low_H);
}
static void on_high_H_thresh_trackbar(int, void *)
{
high_H = max(high_H, low_H+1);
setTrackbarPos("High H", window_detection_name, high_H);
}
static void on_low_S_thresh_trackbar(int, void *)
{
low_S = min(high_S-1, low_S);
setTrackbarPos("Low S", window_detection_name, low_S);
}
static void on_high_S_thresh_trackbar(int, void *)
{
high_S = max(high_S, low_S+1);
setTrackbarPos("High S", window_detection_name, high_S);
}
static void on_low_V_thresh_trackbar(int, void *)
{
low_V = min(high_V-1, low_V);
setTrackbarPos("Low V", window_detection_name, low_V);
}
static void on_high_V_thresh_trackbar(int, void *)
{
high_V = max(high_V, low_V+1);
setTrackbarPos("High V", window_detection_name, high_V);
}
int main(int argc, char* argv[])
{
VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
namedWindow(window_capture_name);
namedWindow(window_detection_name);
// Trackbars to set thresholds for HSV values
createTrackbar("Low H", window_detection_name, &low_H, max_value_H, on_low_H_thresh_trackbar);
createTrackbar("High H", window_detection_name, &high_H, max_value_H, on_high_H_thresh_trackbar);
createTrackbar("Low S", window_detection_name, &low_S, max_value, on_low_S_thresh_trackbar);
createTrackbar("High S", window_detection_name, &high_S, max_value, on_high_S_thresh_trackbar);
createTrackbar("Low V", window_detection_name, &low_V, max_value, on_low_V_thresh_trackbar);
createTrackbar("High V", window_detection_name, &high_V, max_value, on_high_V_thresh_trackbar);
Mat frame, frame_HSV, frame_threshold;
while (true) {
cap >> frame;
if(frame.empty())
{
break;
}
// Convert from BGR to HSV colorspace
cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
// Detect the object based on HSV Range Values
inRange(frame_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), frame_threshold);
// Show the frames
imshow(window_capture_name, frame);
imshow(window_detection_name, frame_threshold);
char key = (char) waitKey(30);
if (key == 'q' || key == 27)
{
break;
}
}
return 0;
}
n-dimensional dense array class
Definition mat.hpp:829
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:766
void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)
Checks if array elements lie between the elements of two other arrays.
std::string String
Definition cvstd.hpp:151
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void setTrackbarPos(const String &trackbarname, const String &winname, int pos)
Sets the trackbar position.
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.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition core.hpp:107

Explanation

Let's check the general structure of the program:

  • Capture the video stream from default or supplied capturing device.

    VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
  • Create a window to display the default frame and the threshold frame.

    namedWindow(window_capture_name);
    namedWindow(window_detection_name);
  • Create the trackbars to set the range of HSV values

    // Trackbars to set thresholds for HSV values
    createTrackbar("Low H", window_detection_name, &low_H, max_value_H, on_low_H_thresh_trackbar);
    createTrackbar("High H", window_detection_name, &high_H, max_value_H, on_high_H_thresh_trackbar);
    createTrackbar("Low S", window_detection_name, &low_S, max_value, on_low_S_thresh_trackbar);
    createTrackbar("High S", window_detection_name, &high_S, max_value, on_high_S_thresh_trackbar);
    createTrackbar("Low V", window_detection_name, &low_V, max_value, on_low_V_thresh_trackbar);
    createTrackbar("High V", window_detection_name, &high_V, max_value, on_high_V_thresh_trackbar);
  • Until the user want the program to exit do the following

    cap >> frame;
    if(frame.empty())
    {
    break;
    }
    // Convert from BGR to HSV colorspace
    cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
    // Detect the object based on HSV Range Values
    inRange(frame_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), frame_threshold);
  • Show the images

    // Show the frames
    imshow(window_capture_name, frame);
    imshow(window_detection_name, frame_threshold);
  • For a trackbar which controls the lower range, say for example hue value:

    static void on_low_H_thresh_trackbar(int, void *)
    {
    low_H = min(high_H-1, low_H);
    setTrackbarPos("Low H", window_detection_name, low_H);
    }
    static void on_low_H_thresh_trackbar(int, void *)
    {
    low_H = min(high_H-1, low_H);
    setTrackbarPos("Low H", window_detection_name, low_H);
    }
  • For a trackbar which controls the upper range, say for example hue value:

    static void on_high_H_thresh_trackbar(int, void *)
    {
    high_H = max(high_H, low_H+1);
    setTrackbarPos("High H", window_detection_name, high_H);
    }
  • 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.