Image Thresholding using OpenCV

Thresholding is a simple technique that can be used to separate the objects from its background. We generally refer to the object in which we are interested as the foreground while rest as background.
Thresholding is an important step of segmentation which will be used as a preprocessing step before performing any high level image processing step such as object detection or recognition.
Technically speaking, in thresholding, a threshold value (t) is selected and then each pixel in the image is compared with this threshold value. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value (e.g. in case of uint8 we set to 255). There are two main ways of performing thresholding in OpenCV.

  1. Global Thresholding
  2. Adaptive Thresholding

In this section we will focus on Global Thresholding.

Global Thresholding

In global thresholding one threshold value is selected for the whole image and each pixel is compared with this threshold value for segmentation. If the pixel value is greater than threshold then the output pixel is set to 1 or 255. However, if the pixel value is less than threshold then the output value is set to 0. In OpenCV, global thresholding is performed using threshold() function.
The signature of the threshold() function is as given below:

thresh, dst = cv2.threshold( src, thresh, maxval, type[, dst] )

The parameters of the above function are described as below:

Parameter Description

src

input array (multiple-channel, 8-bit or 32-bit floating point).

thresh

The threshold value used for thresholding.

maxval

The value which is used to replace the pxiels that satisfy the threshold condition. Usually the maximum value is used.

type

thresholding type. Described below (see ThresholdTypes).

The function returns a tuple as output consisting of the following two values.

thresh

The output array of the same size and type and the same number of channels as src.

dst

The threshold value used for thresholding.

Let us look at an example of performing global thresholding. The Code shown in Code-1 performs thresholding using OpenCV.

Code-1: An example of global thresholding
 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)  
    thresh, BW = cv2.threshold(img,85, 255, cv2.THRESH_BINARY)
    cv2.imshow('Original Image',img)
    cv2.imshow('Thresholded Image',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 lines defines a try block. The exception block (Line 11 and 12) is associated with this block. If any exception occurs, it throws it and code in exception block is executed.
Line 4: Define path of the image to read.
Line 5: Here we read a color image (see second parameter).
Line 6: This line performs the thresholding operation using threshold(). We provided threshold value of 85. If pixel value is greater than this threshold, then it is replaced with 255.This behavior is defined using cv2.THRESH_BINARY.
It returned two values. The first value is the threshold value used in the method. With cv2.THRESH_BINARY it returns the same value as we used which is 85. The second value (BW) is the thresholded image returned.
Line 8-11: These lines help display the input and output image.

The output obtained when code-1 is executed is shown in Figure 1.

No image
Figure 1: Original Image (left), thresholded image (right)

Let us look at another example in which we just updated the fourth parameter in Line 6 to cv2.THRESH_BINARY_INV as follows:

thresh, BW = cv2.threshold(img,85, 255, cv2.THRESH_BINARY_INV)

The output obtained after executing the above code is shown in Figure 2.

No image
Figure 2: Original image (left), thresholded image (right)

Now we can see that the output is just opposite to the one shown in figure 1 (white is black and black is white). It is because the cv2.THRESH_BINARY_INV if the input pixel value is greater than threshold value then output value is set to zero otherwise to the maximum.

OpenCV Tutorials