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 –
# Python program to illustrate # Otsu thresholding type on an image # organizing imports import cv2 import numpy as np # path to input image is specified and # image is loaded with imread command image1 = cv2.imread('input1.jpg') # cv2.cvtColor is applied over the # image input with applied parameters # to convert the image in grayscale img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) # applying Otsu thresholding # as an extra flag in binary # thresholding ret, thresh1 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # the window showing output image # with the corresponding thresholding # techniques applied to the input image cv2.imshow('Otsu Threshold', thresh1) # De-allocate any associated memory usage if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows() |
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.
Recommended Posts:
- Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)
- Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
- MATLAB | Change the color of background pixels by OTSU Thresholding
- OpenCV: Segmentation using Thresholding
- Mahotas - Otsu's method
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- Mahotas - Bernsen local thresholding
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Transition from OpenCV 2 to OpenCV 3.x
- Python | Create video using multiple images using OpenCV
- Image resizing using Seam carving using OpenCV in Python
- Project Idea | Motion detection using Background Subtraction Techniques
- Short Circuiting Techniques in Python
- Looping Techniques in Python
- Cartooning an Image using OpenCV - Python
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- OpenCV Python Program to analyze an image using Histogram
- Detection of a specific color(blue here) using OpenCV with Python
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Erosion and Dilation of images using OpenCV in python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



