OpenCV  4.5.5
Open Source Computer Vision
Basic Thresholding Operations

Prev Tutorial: Image Pyramids

Next Tutorial: Thresholding Operations using inRange

Original author Ana Huamán
Compatibility OpenCV >= 3.0

Goal

In this tutorial you will learn how to:

Cool Theory

Note
The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler. What is

Thresholding?

Types of Thresholding

Threshold Binary

Threshold Binary, Inverted

Truncate

Threshold to Zero

Threshold to Zero, Inverted

Code

Explanation

Let's check the general structure of the program:

As you can see, the function cv::threshold is invoked. We give \(5\) parameters in C++ code:

Results

  1. After compiling this program, run it giving a path to an image as argument. For instance, for an input image as:

    Threshold_Tutorial_Original_Image.jpg
  2. First, we try to threshold our image with a binary threshold inverted. We expect that the pixels brighter than the \(thresh\) will turn dark, which is what actually happens, as we can see in the snapshot below (notice from the original image, that the doggie's tongue and eyes are particularly bright in comparison with the image, this is reflected in the output image).

    Threshold_Tutorial_Result_Binary_Inverted.jpg
  3. Now we try with the threshold to zero. With this, we expect that the darkest pixels (below the threshold) will become completely black, whereas the pixels with value greater than the threshold will keep its original value. This is verified by the following snapshot of the output image:

    Threshold_Tutorial_Result_Zero.jpg