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


In this tutorial you will learn how to:

Cool Theory

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


Types of Thresholding

Threshold Binary

Threshold Binary, Inverted


Threshold to Zero

Threshold to Zero, Inverted



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:


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

  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).

  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: