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

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

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.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

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.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN