In the previous posts, Simple Thresholding and Adaptive Thresholding were explained. In Simple Thresholding, the global value of threshold was used which remained constant throughout. In Adaptive thresholding, the threshold value is calculated for smaller regions with different threshold values for different regions with respect to the change in lighting.
In Otsu Thresholding, a value of the threshold isn’t chosen but is determined automatically. A bimodal image (two distinct image values) is considered. The histogram generated contains two peaks. So, a generic condition would be to choose a threshold value that lies in the middle of both the histogram peak values.
We use the Traditional cv2.threshold
function and use cv2.THRESH_OTSU
as an extra flag.
Syntax: cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)
Parameters:
-> source: Input Image array (must be in Grayscale).
-> thresholdValue: Value of Threshold below and above which pixel values will change accordingly.
-> maxVal: Maximum value that can be assigned to a pixel.
-> thresholdingTechnique: The type of thresholding to be applied.
Below is the Python code explaining Otsu Thresholding Technique –
import cv2
import numpy as np
image1 = cv2.imread( 'input1.jpg' )
img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(img, 120 , 255 , cv2.THRESH_BINARY +
cv2.THRESH_OTSU)
cv2.imshow( 'Otsu Threshold' , thresh1)
if cv2.waitKey( 0 ) & 0xff = = 27 :
cv2.destroyAllWindows()
|
Input:

Output:

The calculation accepts that the picture contains two classes of pixels following foreground and background pixels, it at that point ascertains the ideal limit isolating the two classes with the goal that their consolidated spread is insignificant.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Jan, 2023
Like Article
Save Article