# 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 OpenCV as much as I do let's connect on Twitter or LinkedIn. I talk about this stuff all the time and build cool projects.

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

``````

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