Types of Thresholding in OpenCV

OpenCV provides five different types of thresholding which is given by the fourth parameter of the function. The possible thresholding values are as given below:

  • cv.THRESH_BINARY
  • cv.THRESH_BINARY_INV
  • cv.THRESH_TRUNC
  • cv.THRESH_TOZERO
  • cv.THRESH_TOZERO_INV

The mathematical representation of the methods are summarized in Table 1. Here the src is the source image whose pixel values are compared with the threshold and produces the result for the destination image.

Table 1: The summary of thresholding types in OpenCV:
Type Description

THRESH_BINARY

$$ dst(x,y) = \begin{cases} maxval, & \text{if src(x,y)>thresh} \\ 0, & \text{otherwise} \end{cases} $$

If the input pixel value is greater than threshold value then output value is set to maximum value otherwise it is set to zero.

THRESH_BINARY_INV

$$ dst(x,y) = \begin{cases} maxval, & \text{if src(x,y)>thresh} \\ 0, & \text{otherwise} \end{cases} $$

If the input pixel value is greater than threshold value then output value is set to zero otherwise to maximum.

THRESH_TRUNC

$$ dst(x,y) = \begin{cases} threshold, & \text{if src(x,y)>thresh} \\ src(x,y), & \text{otherwise} \end{cases} $$

If the input pixel value is greater than threshold value then output is set to threshold otherwise set it to input pixel value.

THRESH_TOZERO

$$ dst(x,y) = \begin{cases} src(x,y), & \text{if src(x,y)>thresh} \\ 0, & \text{otherwise} \end{cases} $$

If the input pixel value is greater than threshold value then output is set to input pixel value otherwise set it to zero.

THRESH_TOZERO_INV

$$ dst(x,y) = \begin{cases} 0, & \text{if src(x,y)>thresh} \\ src(x,y), & \text{otherwise} \end{cases} $$

If the input pixel value is greater than threshold value then output is set to zero otherwise set it to input pixel value.

Let us look an example on how to use perform simple thresholding using these techniques. Code-1 shows an example of using cv2.THRESH_BINARY to obtain a binary image.

Code-1: Use of different thresholding types in OpenCV
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2
import numpy as np
import matplotlib.pyplot as plt
try:
    path = r'F:\img\coins.jpg'
    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)  
    thresh, BW1 = cv2.threshold(img,85,255,cv2.THRESH_BINARY)
    thresh, BW2 = cv2.threshold(img,85,255,cv2.THRESH_BINARY_INV)
    thresh, BW3 = cv2.threshold(img,85,255,cv2.THRESH_TOZERO)
    thresh, BW4 = cv2.threshold(img,85,255,cv2.THRESH_TOZERO_INV)
    thresh, BW5 = cv2.threshold(img,85,255,cv2.THRESH_TRUNC)

    fig, axs = plt.subplots(2,3,figsize=(16,8))
    axs[0,0].imshow(img,cmap='gray')
    axs[0,0].axis('off')
    axs[0,0].set_title('Original Image',fontsize='medium')

    axs[0,1].imshow(BW1,cmap='gray')
    axs[0,1].axis('off')
    axs[0,1].set_title('THRESH_BINARY',fontsize='medium')

    axs[0,2].imshow(BW2,cmap='gray')
    axs[0,2].axis('off')
    axs[0,2].set_title('THRESH_BINARY_INV',fontsize='medium')
    
    axs[1,0].imshow(BW3,cmap='gray')
    axs[1,0].axis('off')
    axs[1,0].set_title('THRESH_TOZERO',fontsize='medium')
    
    axs[1,1].imshow(BW4,cmap='gray')
    axs[1,1].axis('off')
    axs[1,1].set_title('THRESH_TOZERO_INV',fontsize='medium')
    
    axs[1,2].imshow(BW5,cmap='gray')
    axs[1,2].axis('off')
    axs[1,2].set_title('THRESH_TRUNC',fontsize='medium')
except Exception as e: 
    print('Unable to read image.' + e)

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: Results obtained for different types of thresholds supported by OpenCV.

OpenCV Tutorials