OpenCV  4.5.4
Open Source Computer Vision
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:

Theory

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.

Threshold_inRange_HSV_colorspace.jpg
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.

Threshold_inRange_RGB_colorspace.jpg
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

Explanation

Let's check the general structure of the program:

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);
}

Results