Otsu Thresholding
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.
Example: Python OpenCV Otsu Thresholding
Python3
# 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( 'geeks.png' ) # 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() |
Output:
Getting Started with Python OpenCV
Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos.
Python OpenCV is the most popular computer vision library. By using it, one can process images and videos to identify objects, faces, or even handwriting of a human. When it is integrated with various libraries, such as NumPy, python is capable of processing the OpenCV array structure for analysis.
In this article, we will discuss Python OpenCV in detail along with some common operations like resizing, cropping, reading, saving images, etc with the help of good examples.