Thresholding in OpenCV

A step by step tutorial for performing image thresholding in OpenCV

Updated March 19, 2023


Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Welcome to the exciting world of OpenCV and computer vision! Today, we’re going to be exploring one of the most fundamental image processing techniques: image thresholding. Image thresholding is the process of converting an image into a binary image, where each pixel is either black or white, based on a threshold value. This is a simple but powerful technique that can be used for a variety of purposes, such as object recognition, image segmentation, and feature extraction. In this tutorial, we’re going to show you how to perform image thresholding with OpenCV.

Before we dive into the code, let’s take a moment to understand what image thresholding is and why it’s useful.

What is Image Thresholding?

Image thresholding is the process of converting an image into a binary image, where each pixel is either black or white, based on a threshold value. This is done by comparing the intensity of each pixel to the threshold value, and if the intensity of the pixel is greater than the threshold value, it is set to white, otherwise, it is set to black. This process is useful for many computer vision applications because it simplifies the image, making it easier to detect and analyze the objects in the image.

There are many different thresholding algorithms, and one of the most popular is the simple thresholding algorithm. The simple thresholding algorithm is a simple and straightforward algorithm that sets all pixels with an intensity greater than the threshold value to white, and all pixels with an intensity less than or equal to the threshold value to black.

Now that we have a basic understanding of image thresholding, let’s see how we can do it in OpenCV.

The first step is to load an image into OpenCV. You can do this using the imread() function, like this:

import cv2
import numpy as np

# Load an image
img = cv2.imread('image.jpg', 0)

Next, we’re going to apply the simple thresholding algorithm to the image. To do this, we’ll use the threshold() function, like this:

# Apply the simple thresholding algorithm
_, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

The first argument to the threshold() function is the image we want to threshold, and the second argument is the threshold value. The third argument is the maximum intensity value that will be used to represent white in the binary image, and the fourth argument is the type of thresholding algorithm we want to use. In this example, we’re using the simple thresholding algorithm (cv2.THRESH_BINARY). The result of the threshold() function is stored in the variable thresh, and it is a binary image where each pixel is either black or white.

Let’s take a look at another example, this time using the adaptive thresholding algorithm. Here’s the code:

# Apply the adaptive thresholding algorithm
thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

In this example, we’re using the adaptive thresholding algorithm (cv2.ADAPTIVE_THRESH_GAUSSIAN_C) to threshold the image. The adaptive thresholding algorithm is a more sophisticated algorithm that adjusts the threshold value for each pixel based on the surrounding pixels. This makes the adaptive thresholding algorithm more robust to changes in illumination and contrast. The second argument to the adaptiveThreshold() function is the maximum intensity value that will be used to represent white in the binary image. The third argument is the type of adaptive thresholding algorithm we want to use (cv2.ADAPTIVE_THRESH_GAUSSIAN_C), and the fourth argument is the type of thresholding algorithm we want to use (cv2.THRESH_BINARY). The fifth argument is the size of the averaging window used to calculate the threshold value for each pixel, and the sixth argument is a constant value that is subtracted from the threshold value.

In conclusion, image thresholding is a simple but powerful technique that can be used for a variety of purposes, such as object recognition, image segmentation, and feature extraction. Whether you’re working with color images or black and white images, understanding how to perform image thresholding with OpenCV will help you develop more effective computer vision applications. So go ahead and start exploring the world of OpenCV and computer vision today!