Learn Image Thresholding with OpenCV cv2.threshold() and cv2.adaptiveThreshold() functions

Introduction

The binary image has only two pixel values 0 and 1, whereas a grayscale image can have pixel values ranging from 0 to 255. Thresholding is used to create a binary image from a grayscaled image. It is one of the simplest techniques of doing image segmentation.

Types of Image Thresholding

Thresholding is of two types:

  1. Simple Thresholding
  2. Adaptive Thresholding
OpenCV has built-in functions cv2.threshold() and cv2.adaptiveThreshold() to carry out image thresholding operations on the image. In this tutorial, we will take a closer look at their syntax along with detailed examples.
But first, let us import the OpenCV library.

Importing opencv library

In [1]:
import cv2

Simple Thresholding : cv2.threshold()

According to Wikipedia-

“The simplest thresholding methods replace each pixel in an image with a black pixel if the image intensity I(i,j) is less than some fixed constant T (that is, I (i,j) < T , or a white pixel if the image intensity is greater than that constant.”

Let us see the syntax of the OpenCV threshold() function along with their examples.

Syntax

retval, ThreshImg = cv2.threshold( src, dst,thresh, maxval, type)

src image that we want to perform thresholding on. This image should be grayscale.

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

thresh thresh is the threshold value which is used to classify the pixel intensities in the grayscale image

maxval maximum value which is assigned to pixel values exceeding the threshold

type Thresholding type. This parameter is used to do different types of simple thresholding.

Opencv provides different types of simple thresholding.

  1. cv2.THRESH_BINARY
  2. cv2.THRESH_BINARY_INV
  3. cv2.THRESH_TRUNC
  4. cv2.THRESH_TOZERO
  5. cv2.THRESH_TOZERO_INV

Example 1: cv2.threshold() with cv2.THRESH_BINARY

Screenshot%20from%202020-03-24%2008-24-51.png

In [2]:
#reading an image
img=cv2.imread("lena.jpg")
In [3]:
#Display image
window_name='imagefirst'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sample Image
Sample Image
In [4]:
#here the first value ret,is the value that was used for the thresholding i.e 127 and thresh1 is the actual threshold image
ret,thresh1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
In [5]:
#Display image
window_name='Threshold1'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh1)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.threshold with cv2.THRESH_BINARY
cv2.threshold with cv2.THRESH_BINARY

Example 2: cv2.threshold() with cv2.THRESH_BINARY_INV

Screenshot%20from%202020-03-24%2008-24-57.png

In [6]:
#here the first value ret2,is the value that was used for the thresholding i.e 100 and thresh2 is the actual threshold image
ret2,thresh2 = cv2.threshold(img,100,255,cv2.THRESH_BINARY_INV)
In [7]:
#Display image
window_name='Threshold2'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh2)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.threshold with cv2.THRESH_BINARY_INV
cv2.threshold with cv2.THRESH_BINARY_INV

Example 3: cv2.threshold() with cv2.THRESH_TRUNC

Screenshot%20from%202020-03-24%2008-25-03.png

In [8]:
#here the first value ret3,is the value that was used for the thresholding i.e 100 and thresh3 is the actual threshold image
ret3,thresh3 = cv2.threshold(img,100,255,cv2.THRESH_TRUNC)
In [9]:
#Display image
window_name='thresh3'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh3)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.threshold with cv2.THRESH_TRUNC
cv2.threshold with cv2.THRESH_TRUNC

Example 4: cv2.threshold() with cv2.THRESH_TOZERO

Screenshot%20from%202020-03-24%2008-25-08.png

In [10]:
#here the first value ret4,is the value that was used for the thresholding i.e 100 and thresh4 is the actual threshold image
ret4,thresh4 = cv2.threshold(img,100,255,cv2.THRESH_TOZERO)
In [11]:
#Display image
window_name='thresh4'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh4)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.threshold with cv2.THRESH_TOZERO
cv2.threshold with cv2.THRESH_TOZERO

Example 5: cv2.threshold() with cv2.THRESH_TOZERO_INV

Screenshot%20from%202020-03-24%2008-25-15.png

In [12]:
#here the first value ret5,is the value that was used for the thresholding i.e 100 and thresh5 is the actual threshold image
ret5,thresh5 = cv2.threshold(img,100,255,cv2.THRESH_TOZERO_INV)
In [13]:
#Display image
window_name='thresh5'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh5)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.threshold with cv2.THRESH_TOZERO_INV
cv2.threshold with cv2.THRESH_TOZERO_INV

 

Adaptive Thresholding:cv2.adaptiveThreshold()

Adaptive thresholding determines the threshold for a pixel, based on a small region around it. So we get different thresholds for different regions of the same image which gives better results for images with varying illumination.

OpenCV provides cv2.adaptiveThreshold() which can perform Adaptive threshold operation on an image.

Syntax

cv2.adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C)

src image that we want to perform thresholding on. This image should be grayscale.

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

maxval maximum value which is assigned to pixel values exceeding the threshold

adaptiveMethod: adaptiveMethod decides how the threshold value is calculated:

  1. cv2.ADAPTIVE_THRESH_MEAN_C: The threshold value is the mean of the neighbourhood area minus the constant C.
  2. cv2.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold value is a gaussian-weighted sum of the neighbourhood values minus the constant C.

thresholdType: Representing the type of threshold to be used.

blockSize: It determines the size of the neighbourhood area.

C: C is a constant that is subtracted from the mean or weighted sum of the neighborhood pixels.

Example 1: cv2.adaptiveThreshold() with cv2.ADAPTIVE_THRESH_MEAN_C

In [14]:
#reading an image

img_second=cv2.imread("lena.jpg",0)
In [15]:
#Display image
window_name='imagesecond'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_second)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sample Image
Sample Image
In [16 ]:
threshold1 = cv2.adaptiveThreshold(img_second,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,5)
In [17]:
#Display image
window_name='threshold1'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,threshold1)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.adaptiveThreshold() with ADAPTIVE_THRESH_MEAN_C
cv2.adaptiveThreshold() with ADAPTIVE_THRESH_MEAN_C

Example 2: cv2.adaptiveThreshold() with cv2.ADAPTIVE_THRESH_GAUSSIAN_C

In [18]:
threshold2 = cv2.adaptiveThreshold(img_second,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,5)
In [19]:
#Display image
window_name='threshold2'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,threshold2)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.adaptiveThreshold() using cv2.ADAPTIVE_THRESH_GAUSSIAN_C
cv2.adaptiveThreshold() using cv2.ADAPTIVE_THRESH_GAUSSIAN_C

Conclusion

In this tutorial, we saw various techniques for thresholding and saw its practical implementation by using the two OpenCV functions cv2.adaptiveThreshold() and cv2.threshold().

 

Reference – https://docs.opencv.org/master/d6/d00/tutorial_py_root.html

  • Sachin Mohan

    My name is Sachin Mohan, an undergraduate student of Computer Science and Engineering. My area of interest is ‘Artificial intelligence’ specifically Deep learning and Machine learning. I have attended various online and offline courses on Machine learning and Deep Learning from different national and international institutes My interest toward Machine Learning and deep Learning made me intern at ISRO and also I become the 1st Runner up in TCS EngiNX 2019 contest. I always love to share my knowledge and experience and my philosophy toward learning is "Learning by doing". So thats why I believe in education which have include both theoretical as well as practical knowledge.

    View all posts

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *