OpenCV: Adaptive and Otsu Threshold in Image Processing with Python

Image pre-processing techniques in artificial intelligence

Amit Chauhan
3 min readMar 20


Otsu Image Threshold. An image by the Author

What is OpenCV?

This library is a nutshell and robust collection of image processing methods that drive the vision application in machine learning and deep learning.

To install the OpenCV library in anaconda

pip install opencv-python

What is the image threshold?

In computer vision applications i.e. image segmentation is an important aspect in which the object in the image is separated into foreground and background.

To make an effective threshold need a grayscale image of one channel in which each image pixels value lies between 0 to 255. The value going toward zero considers white and the value going toward 255 considers black.

The Otsu method is an automatic threshold value optimizer from the bimodal images. The bimodal images are the pixels value that differentiates the image in the foreground(object) and background.

The example of a bimodal image that contains two distributions in the histogram defines the separation of two objects in the grayscale image.

Before Otsu threshold methods

Two threshold methods before Otsu do the threshold based on the threshold value as input in the method that makes the image pixels value into two parts i.e. making the binary image.

  1. Simple threshold: In this type of threshold we have to specify the threshold value and it applies to each pixel of the image. If the pixel value is smaller than the threshold then the pixel value becomes 0 i.e. white and if the pixel value is greater than the threshold then the pixel value is set to 255 i.e. black.

Example of a simple threshold:

Simple threshold methods. An image by the author

2. Adaptive Threshold: In this type of threshold, the pixel of the image is replaced with the mean and weighted sum of the neighborhood pixels. The type adaptive thresholds are Adaptive Mean Threshold and Adaptive