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

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Build a Multimodal RAG System using AWS Bedrock and FAISS
In this LLM RAG Project, you will learn to build a Multimodal RAG system for a restaurant aggregator app, integrating text and visuals to deliver personalized food recommendations using advanced technologies like Amazon S3, Amazon Bedrock, and FAISS.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

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.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

OSZAR »