OpenCV: Adaptive and Otsu Threshold in Image Processing with Python
Image pre-processing techniques in artificial intelligence

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

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 Gaussian Threshold.
Example of adaptive threshold:

Otsu threshold
In the previous threshold methods, we used to choose the global threshold value or give the size of the block and constant in case of adaptive.
In the case of Otsu, we need a bimodal image and its histogram shows two peaks so that the otsu method can find the global threshold value automatically.
Python Example:
# openCV library
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# reading the image from the same directory
img = cv.imread('13.png', cv.IMREAD_GRAYSCALE)
# threshold with the simple binary method
ret1,simp_thresh = cv.threshold(img,127,255,cv.THRESH_BINARY)
# Otsu's threshold without any filter
ret2,simp_thresh_otsu = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# Otsu's threshold using Gaussian filtering
blur = cv.GaussianBlur(img,(5,5),0)
ret3,otsu_filter = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# histogram of images
images = [img, 0, simp_thresh,
img, 0, simp_thresh_otsu,
blur, 0, otsu_filter]
titles = ['Original Image','Histogram','Global Thresholding (v=127)',
'Original Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
# using a loop to visualize the different types of methods
for i in range(3):
plt.figure(figsize=(10,6))
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()Output:

I hope you like the article. Reach me on my LinkedIn and twitter.
Recommended Articles
- Most Usable NumPy Methods with Python 2. NumPy: Linear Algebra on Images 3. Exception Handling Concepts in Python 4. Pandas: Dealing with Categorical Data 5. Hyper-parameters: RandomSeachCV and GridSearchCV in Machine Learning 6. Fully Explained Linear Regression with Python 7. Fully Explained Logistic Regression with Python 8. Data Distribution using Numpy with Python 9. 40 Most Insanely Usable Methods in Python 10. 20 Most Usable Pandas Shortcut Methods in Python






