Thresholding — it is a technique in OpenCV that is the assignment of pixel values in relation to a provided threshold value. In a threshold value, each pixel value is compared to a threshold value. If the pixel value is less than the threshold value, it is set to 0, otherwise it is set to the maximum value (usually 255). Thresholding — a very popular segmentation technique used to separate the foreground object from the background. Threshold — it is the value that has two areas on either side, that is, below the threshold or above the threshold.
Computer Vision performs this thresholding on grayscale images. Therefore, initially the image must be converted to a grayscale color space.
If f (x, y)" T then f (x, y) = 0 else f (x, y) = 255 where f (x, y) = Coordinate Pixel Value T = Threshold Value.
OpenCV with Python uses the cv2.threshold function for the threshold.
Syntax: cv2.threshold ( source, thresholdValue, maxVal, thresholdingTechnique)
Parameters:
-" source : Input Image array (must be in Grayscale).
-" thresholdValue : Value of Threshold below and above which pixel values will change accordingly.
-" maxVal : Maximum value that can be assigned to a pixel.
-" thresholdingTechnique : The type of thresholding to be applied.
Simple Threshold
Basic Thresholding — binary threshold. The same threshold is applied to each pixel. If the pixel value is less than the threshold value, it is set to 0, otherwise it is set to the maximum value.
Various simple thresholding techniques:
-
cv2. THRESH_BINARY
: If the pixel intensity exceeds the set threshold, the value is set to 255, otherwise it is set to 0 (black). -
cv2.THRESH_BINARY_INV
: Inverted or opposite casecv2.THRESH_BINARY
. -
cv.THRESH_TRUNC
: If the pixel intensity value exceeds the threshold, it is truncated to the threshold. The pixel values are set equal to the threshold. All other values remain the same. -
cv.THRESH_TOZERO
: Pixel intensity is setcv.THRESH_TOZERO
0, for the intensity of all pixels is less than the threshold. -
cv.THRESH_TOZERO_INV
: inverted or opposite casecv2.THRESH_TOZERO
.
Below is Python code explaining various simple threshold level techniques —
import cv2 |
Input data :

Exit:
