What is Otsu’s Binarization in OpenCV

This recipe explains what is Otsu’s Binarization in OpenCV

Recipe Objective: What is Otsu's Binarization in OpenCV?

Let us take this recipe to understand what is Otsu's Binary Thresholding and how it is useful.

Learn to Implement Deep Learning Techniques for Medical Image Segmentation

Step 1: Import the necessary libraries and read the image

Let us first import the required libraries and read the images. The image that we are using here is the one shown below. It is crucial to read the image in grayscale format to perform thresholding.

input image

import cv2
from matplotlib import pyplot as plt
image = cv2.imread('coins.jpg',0)

Step 2: Otsu's Binarization

In the Simple Thresholding technique, we define a global threshold value for the image arbitrarily. But how do we know if that is the best value or obtain the best threshold value? We have to try various threshold values and find which one works best for the image. The only answer is the trial and error method.

But suppose we have a Bimodal image (A bimodal image with two dominant peaks in its histogram or, to put it in simple terms, it is a type of image whose pixel values are distributed over two dominating regions). In that case, we can use Otsu's Binarization technique which assigns a threshold value that is approximately in the middle of the two peaks automatically.

We can use the usual cv2.threshold() function with an extra flag of cv2.THRESH_OTSU and set the threshold value to 0 to perform Otsu's Binarization. The optimal threshold value is returned as the first output retVal.

retVal,th = cv2.threshold(image,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

Step 3: Display the output

Let us use the matplotlib library to display the output.

plt.figure(figsize=(13,9))

plt.subplot(1,2,1)
plt.imshow(image,'gray')
plt.title('Original Image')
plt.xticks([])
plt.yticks([])

plt.subplot(1,2,2)
plt.imshow(th,'gray')
plt.title("Otsu's Binary Thresholding")
plt.xticks([])
plt.yticks([])

plt.tight_layout()
plt.show()

Output:

output image

 

Let us also see what the optimal threshold value that was set was.

print(f'The optimum threshold value is {retVal}')

Output:

    The optimum threshold value is 102.0

Download Materials

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.