OpenCV  4.5.2
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:

  • 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

Explanation

Let's check the general structure of the program:

  • Capture the video stream from default or supplied capturing device.
  • Create a window to display the default frame and the threshold frame.
  • Create the trackbars to set the range of HSV values
  • Until the user want the program to exit do the following
  • Show the images
  • 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);
}
  • For a trackbar which controls the upper range, say for example hue value:
  • 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.

cv::String
std::string String
Definition: cvstd.hpp:150
cv::cvtColor
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
cv::COLOR_BGR2HSV
@ COLOR_BGR2HSV
convert RGB/BGR to HSV (hue saturation value) with H range 0..180 if 8 bit image, color conversions
Definition: imgproc.hpp:594
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::VideoCapture
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:664
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::min
softfloat min(const softfloat &a, const softfloat &b)
Min and Max functions.
Definition: softfloat.hpp:437
cv::max
softfloat max(const softfloat &a, const softfloat &b)
Definition: softfloat.hpp:440
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:669
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:801
cv::imshow
void imshow(const String &winname, const ogl::Texture2D &tex)
Displays OpenGL 2D texture in the specified window.
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
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
imgproc.hpp
videoio.hpp