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 Name | Function name | Description |
1 | Binary Thresholding | cv2.THRESH_BINARY | 1. (pixel intensity) > set threshold : 255 (white) 2. Else set to 0 (black). |
2 | Binary Inverted Thresholding | cv2.THRESH_BINARY_INV | The opposite case of cv2.THRESH_BINARY. |
3 | Tozero Thresholding | cv2.THRESH_TOZERO | 1. (pixel intensity) < set threshold value : 0 (black) 2. Else set it to white |
4 | Tozero Inverted Thresholding | cv2.THRESH_TOZERO_INV | The opposite case of cv2.THRESH_TOZERO |
5 | Truncated Thresholding | cv2.THRESH_TRUNC | 1. ( 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. |
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

2. Binary Threshold Image Output

3. Binary Inverted Threshold Image Output

4. Threshold Tozero Output

5. Threshold Tozero Inverted Output

6. Truncated Threshold Image Output

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!