Simple Thresholding And Its Types In OpenCV
Computer vision mainly deals with how computers can gain high-level understanding from digital images or videos so that one can use it for their autonomous systems to automate it for a better solution. OpenCV is a computer vision library mainly aimed at solving real-time image processing-based problems. With the help of it, we can process images and videos to identify objects, faces, or even handwritten text as OpenCV is a huge open-source library, it is widely used and has a very huge community.OpenCV provides different techniques for pre-processing, processing, and post-processing the image to achieve accurate results. Today in this blog I will tell you about one of the pre-processing techniques i.e. Thresholding and we will also be going to see what are the different types of simple thresholding.
What is thresholding?
Thresholding is the method used for image segmentation. It is used for creating binary images from a gray-scale image.
The simplest thresholding methods replace each pixel in an image with a black pixel if the image intensity Iij is less than some fixed constant T (that is, Iij < T ) or a white pixel if the image intensity is greater than that constant. In the example image on the right, this results in the dark tree becoming completely black, and the white snow becoming completely white.
In short, If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value that is 255.
Different Types Of Thresholding
- Simple Thresholding
- Adaptive Thresholding
- Otsu Thresholding
Today we will discuss Simple Thresholding and its types.
In Simple Thresholding, If a pixel value is greater than a threshold value, it is assigned one value (maybe white), else it is assigned another value (maybe black). The function used is cv2.threshold.
Structure of cv2.threshold
cv2.threshold(argument1, argument2, argument3, argument4) # argument1 = source image, which should be a grayscale image # argument2 = threshold value which is used to classify the pixel values # argument3 = maxVal which represents the value to be given if pixel value is more than (sometimes less than) the threshold value. # argument4 = different styles of thresholding
Different Types Of Simple Thresholding are as follows
Here are the results of different thresholding techniques after applying on the image.
Now let's discuss complete flow of Thresholding in python3
Step 1 — Import all necessary packages
import numpy as np
from matplotlib import pyplot as plt
Step 2 — Load image
img1 = cv2.imread('inputimg.png')
Step 4 — Applying different Thresholding techniques. Here I want all pixels value above 150 will be set to 255
ret,thresh1 = cv2.threshold(img,150,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,150,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,150,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,150,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,150,255,cv2.THRESH_TOZERO_INV)
Step 5 — here you can use cv2.imshow() function to display one by one window.But we are using matplotlib and plot all the output images on the same window
titles = ['OriginalImage','BINARY','BINARY_INV','TRUNC','TOZERO',
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
Step 6 — Now display out matplolib graph having different threshold applied images
for i in xrange(6):