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:

1. Simple 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:

1. source: it signifies the source image which has to be processed.
2. thresholdValue: the value of threshold.
3. maxVal: it signifies the maximum value which can be assign to a pixel.
4. thresholdingTechnique: the type of thresholding to be performed. There are five simple thresholding technique:
1. cv2.THRESH_BINARY
2. cv2.THRESH_BINARY_INV
3. cv2.THRESH_TRUNC
4. cv2.THRESH_TOZERO
5. cv2.THRESH_TOZERO_INV

Implemented code in Python:

import cv2

#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:

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:

1. source: it signifies the source image which has to be processed.
2. maxVal: it signifies the maximum value which can be assign to a pixel.
3. adaptiveMethod: it signifies the way to calculate threshold value. There are two ways for calculating threshold value:
4. thresholdingTechnique: the type of thresholding to be performed. There are five simple thresholding technique:
1. cv2.THRESH_BINARY
2. cv2.THRESH_BINARY_INV
3. cv2.THRESH_TRUNC
4. cv2.THRESH_TOZERO
5. cv2.THRESH_TOZERO_INV
5. blocksize: it signifies the size of region used to calculate threshold value.
6. constant: it signifies the constant value which subtracted from the mean or weighted mean.

Implemented code in Python:

#importing libraries
import cv2

cv2.imshow('ORIGINAL', img);

#wait for 10 seconds
cv2.waitKey(10000);
cv2.destroyAllWindows();

OUTPUT:

$${}$$