Thresholding is a kind of image segmentation method which is used to simplify the image( or video ) by pixels manipulation.
In thresholding, we convert the image which can either be colored or in greyscale into binary image( only black and white color is used to represent any pixel ). But in computer vision, thresholding is done on grayscale images only. So initially, the image has to be converted in grayscale color space.
Every pixel will either have 0 to represent black or maximum value to represent white color.
Two methods are used in openCV for thresholding:
- Simple thresholding
- Adaptive thresholding
Let’s discuss them one by one:
Simple thresholding:
In this technique, the same threshold value is applied for every pixel.
cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique) function is used to perform simple thresholding with openCV. The function has four arguments:
- source: it signifies the source image which has to be processed.
- thresholdValue: the value of threshold.
- maxVal: it signifies the maximum value which can be assign to a pixel.
- thresholdingTechnique: the type of thresholding to be performed. There are five simple thresholding technique:
- cv2.THRESH_BINARY
- cv2.THRESH_BINARY_INV
- cv2.THRESH_TRUNC
- cv2.THRESH_TOZERO
- cv2.THRESH_TOZERO_INV
Implemented code in Python:
import cv2 img = cv2.imread('flower.jpg',0); #different thresholding techniques on the image retval, thresh1 = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY); retval, thresh2 = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY_INV); retval, thresh3 = cv2.threshold(img, 125, 255, cv2.THRESH_TRUNC); retval, thresh4 = cv2.threshold(img, 125, 255, cv2.THRESH_TOZERO); retval, thresh5 = cv2.threshold(img, 125, 255, cv2.THRESH_TOZERO_INV); cv2.imshow('ORIGINAL', img); cv2.imshow('THRESH_BINARY', thresh1); cv2.imshow('THRESH_BINARY_INV', thresh2); cv2.imshow('THRESH_TRUNC', thresh3); cv2.imshow('THRESH_TOZERO', thresh4); cv2.imshow('THRESH_TOZERO_INV', thresh5); #wait for 10 seconds cv2.waitKey(10000); cv2.destroyAllWindows();
OUTPUT:

Adaptive thresholding:
In this technique, we can have different threshold values for different regions.
cv2.adaptiveThreshold(source, maxVal, adaptiveMethod, thresholdingTechnique, blocksize, constant) function is used in openCV to perform adaptive thresholding. The function has six arguments:
- source: it signifies the source image which has to be processed.
- maxVal: it signifies the maximum value which can be assign to a pixel.
- adaptiveMethod: it signifies the way to calculate threshold value. There are two ways for calculating threshold value:
- cv2.ADAPTIVE_THRESH_MEAN_C
- cv2.ADAPTIVE_THRESH_GAUSSIAN_C
- thresholdingTechnique: the type of thresholding to be performed. There are five simple thresholding technique:
- cv2.THRESH_BINARY
- cv2.THRESH_BINARY_INV
- cv2.THRESH_TRUNC
- cv2.THRESH_TOZERO
- cv2.THRESH_TOZERO_INV
- blocksize: it signifies the size of region used to calculate threshold value.
- constant: it signifies the constant value which subtracted from the mean or weighted mean.
Implemented code in Python:
#importing libraries import cv2 img = cv2.imread('flower.jpg',0); thresh10 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 101, 2); thresh11 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, 101, 2); thresh21 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 101, 2); thresh22 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV, 101, 2); cv2.imshow('ORIGINAL', img); cv2.imshow('BINARY_ADAPTIVE_THRESH_MEAN_C', thresh10); cv2.imshow('BINARY_INV__ADAPTIVE_THRESH_MEAN_C', thresh11); cv2.imshow('BINARY_ADAPTIVE_THRESH_GAUSSIAN_C', thresh21); cv2.imshow('BINARY_INV_ADAPTIVE_THRESH_GAUSSIAN_C', thresh22); #wait for 10 seconds cv2.waitKey(10000); cv2.destroyAllWindows();
OUTPUT:

Thank you Rishika Gupta for this article.
0 Comments