Image Binarization with Otsu's Global Thresholding Algorithm

The result of image thresholding is affected by the selected threshold value. In simple thresholding, the user has to select this threshold value. Selecting a high value may result in omitting important information, while selecting a small value may result in unintended information included in the output.

Now the question is, can we calculate the threshold value automatically by analyzing the pixel values in an image? The answer is a big YES. Here comes the story of Ostu’s global thresholding algorithm, which is discussed in the following section that automatically calculates an optimal value of threshold.

Otsu’s method automatically selects an optimal threshold value using the image histogram. In OpenCV, Otsu method can be used by adding an extra flag in the threshold() method. Although we have to also pass an arbitrary threshold value as a second parameter to the function during the call, it will not be used when the Otsu method is used.

Notice that threshold() function returns two values: retVal and Destination Image. The retVal (return value) contains the optimal value of the threshold obtained when THRESH_OTSU flag is used in the threshold() function; otherwise, retVal is the same as the threshold value you used. The destination image is the result of applying thresholding on the input image.

Let us look at an example of image binarization using Otsu’s method. The example in Code-1 shows how to use Otsu’s global thresholding algorithm to automatically detect the threshold value and use it for image binarization.

Code 1: Image binarization using Otsu’s method
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import cv2
import numpy as np
try:
    path = r'F:\img\coins.jpg'
    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)  
    thr, BW = cv2.threshold(img,85,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    cv2.imshow('Original Image',(img))
    cv2.imshow('THRESH_BINARY',BW)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
except:
    print('Unable to read image.')

Line 1 and 2: We start by importing OpenCV and numpy libraries.
Line 3: This line defines a try block. The exception block (Line 11 and 12) is associated with this block. If any exception occurs, it throws it, and the code in the exception block is executed.
Line 4: Define the path of the image to read.
Line 5: Here we read a grayscale image (see the second parameter).
Line 6: This line performs the thresholding operation using threshold() function in OpenCV. Notice the last parameter in the function: cv2.THRESH_BINARY + cv2.THRESH_OTSU. This is how we mention the use of the Otsu method for finding the threshold value. Notice that we also provided a threshold value (85), however, it is not used in the thresholding.
It returned two values. The first value is the threshold value calculated by Otsu which is then used in the thresholding method.
Line 7-10: These lines help display the input and output image.
The output obtained when code-1 is executed is shown below in Figure 1.

The result obtained when above code is executed is shown in Figure 1. Note that we used Matplotlib’s pyplot to display the image within the JupyterLab.

No image
Figure 1: Original image (left), thresholded image using Otsu method (right).

OpenCV Tutorials