Image Thresholding in Python – An Easy and Quick Guide

Featured Img Image Thresholding

Hey, there fellow coder! Today we are going to understand what Image Thresholding is and how to implement the same in the python programming language.

Let’s get right into the concept!

Also read: Edge Detection in Images using Python.

What is Image Thresholding?

Thresholding is defined as a process of dividing an image into two parts namely: “foreground” and “background”. It is mostly used in various Image processing tasks, allows greater image recognition and segmentation, etc.

Different Types of Thresholding Techniques

One can implement various threshold techniques which are named and described below:

S.No.Thresholding Technique NameFunction nameDescription
1Binary Thresholdingcv2.THRESH_BINARY1. (pixel intensity) > set threshold : 255 (white)
2. Else set to 0 (black).
2 Binary Inverted Thresholdingcv2.THRESH_BINARY_INVThe opposite case of cv2.THRESH_BINARY.
3Tozero Thresholdingcv2.THRESH_TOZERO1. (pixel intensity) < set threshold value : 0 (black)
2. Else set it to white
4Tozero Inverted Thresholdingcv2.THRESH_TOZERO_INVThe opposite case of cv2.THRESH_TOZERO
5Truncated Thresholdingcv2.THRESH_TRUNC1. ( pixel intensity ) > threshold: Truncated to the threshold.
2. The pixel values are set to be the same as the threshold.
3. All other values remain the same.
Various Thresholding Techniques

Also read: Python: Converting Images to Pencil Sketch

The Full Code

Since the thresholding methods have direct functions for implementation, we can directly look at the code implementation of the thresholding method. I hope you understand the coding implementation of the same.

import cv2  
import numpy as np

img = cv2.imread('lori.jpg')  
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

ret, thresh_hold = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY) 
ret, thresh_hold1 = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV) 
ret, thresh_hold2 = cv2.threshold(img, 100, 255, cv2.THRESH_TOZERO) 
ret, thresh_hold3 = cv2.threshold(img, 100, 255, cv2.THRESH_TOZERO_INV) 
ret, thresh_hold4 = cv2.threshold(img, 100, 255, cv2.THRESH_TRUNC)   

thresh_hold = cv2.resize(thresh_hold, (960, 540))    
cv2.imshow('Binary Threshold Image', thresh_hold) 

thresh_hold1 = cv2.resize(thresh_hold1, (960, 540))    
cv2.imshow('Binary Threshold Inverted Image', thresh_hold1) 

thresh_hold2 = cv2.resize(thresh_hold2, (960, 540))    
cv2.imshow('Threshold Tozero Image', thresh_hold2) 

thresh_hold3 = cv2.resize(thresh_hold3, (960, 540))    
cv2.imshow('ThresholdTozero Inverted output', thresh_hold3) 

thresh_hold4= cv2.resize(thresh_hold4, (960, 540))    
cv2.imshow('Truncated Threshold output', thresh_hold4) 

if cv2.waitKey(0) & 0xff == 25:  
    cv2.destroyAllWindows()

Sample Output – 1

1. Original Image Output

Lori
Lori

2. Binary Threshold Image Output

Binary Threshold Image
Binary Threshold Image

3. Binary Inverted Threshold Image Output

Binary Inverted Threshold Image
Binary Inverted Threshold Image

4. Threshold Tozero Output

Threshold Tozero Image
Threshold Tozero Image

5. Threshold Tozero Inverted Output

Threshold Tozero Inverted Image
Threshold Tozero Inverted Image

6. Truncated Threshold Image Output

Truncated Threshold Image
Truncated Threshold Image

Sample Output – 2

Threshold Sample Output 2
Threshold Sample Output 2

Conclusion

In the end, I want you to try thresholding different images all by yourself and see the outputs you get with different images. Thank you for reading!

Happy Learning!