OPENCV – PYTHON | Image Thresholding | Global + Adaptive | ADAPTIVE MEAN & GAUSSIAN | Variants
Image thresholding is a fundamental technique in computer vision and image processing that plays a pivotal role in segmenting images by converting them into binary form. By setting a predefined threshold, pixel values are categorized as either foreground or background, simplifying subsequent analysis. In this article, we delve into the realm of image thresholding using OpenCV in Python, exploring both global and adaptive thresholding techniques. Specifically, we will investigate the ADAPTIVE MEAN and GAUSSIAN variants, unraveling the intricacies of their implementation.
Flow of Article:
- Understanding Image thresholding with OpenCV
- What is global thresholding
- What is adaptive thresholding
- Coding both thresholding
- Various forms of thresholding functions in OpenCV
- Mean and Gaussian Adaptive thresholding
Video Explanation:
Image Thresholding:
At its core, image thresholding involves converting a grayscale image into a binary image. Pixels with intensity values above a certain threshold are classified as foreground, while those below the threshold are considered background. This process is vital for tasks such as object detection, contour extraction, and image segmentation.
1. Global Thresholding:
In global thresholding, a single, fixed threshold value is applied to the entire image. Pixels with intensities greater than this threshold are set to a high value (e.g., white), while those below are set to a low value (e.g., black). This method is effective when the image exhibits uniform lighting conditions and a clear intensity distinction between objects and the background.
2. Adaptive Thresholding:
Adaptive thresholding takes into account local variations in intensity across the image. Instead of using a single threshold for the entire image, adaptive thresholding computes individual thresholds for different regions. This technique is particularly beneficial when dealing with images affected by varying lighting conditions, shadows, or uneven contrasts.
OpenCV Implementation:
1. Global Thresholding
OpenCV provides a straightforward implementation of global thresholding through the cv2.threshold
function. This function takes the input image, threshold value, and maximum pixel value as parameters, allowing users to easily convert images to binary form.
import cv2
# Load image in grayscale
img = cv2.imread(‘image.jpg’, 0)
# Apply global thresholding
_, global_thresholded = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# Display the results
cv2.imshow(‘Global Thresholding’, global_thresholded)
cv2.waitKey(0)
cv2.destroyAllWindows()
Adaptive Thresholding in OpenCV:
The cv2.adaptiveThreshold
function in OpenCV facilitates adaptive thresholding. This function considers local neighborhoods, calculating individual thresholds for each region based on the specified algorithm (e.g., mean or Gaussian).
import cv2
# Load image in grayscale
img = cv2.imread(‘image.jpg’, 0)
# Apply adaptive mean thresholding
adaptive_mean = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
# Apply adaptive Gaussian thresholding
adaptive_gaussian = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Display the results
cv2.imshow(‘Adaptive Mean Thresholding’, adaptive_mean)
cv2.imshow(‘Adaptive Gaussian Thresholding’, adaptive_gaussian)
cv2.waitKey(0)
cv2.destroyAllWindows()
Thresholding Styles in OpenCV:
OpenCV offers various thresholding styles, each catering to specific image characteristics:
Binary Thresholding (
cv2.THRESH_BINARY
):- Basic thresholding where pixel values above the threshold become white, and those below become black.
Inverse Binary Thresholding (
cv2.THRESH_BINARY_INV
):- Inverted version of binary thresholding, resulting in a black-and-white inversion.
Truncated Thresholding (
cv2.THRESH_TRUNC
):- Pixels above the threshold are set to the threshold value, while those below remain unchanged.
To Zero Thresholding (
cv2.THRESH_TOZERO
):- Pixels below the threshold are set to zero, while those above remain unchanged.
Inverse To Zero Thresholding (
cv2.THRESH_TOZERO_INV
):- Inverse of To Zero Thresholding, setting pixels above the threshold to zero.
Adaptive Mean Thresholding:
Adaptive Mean Thresholding is based on the local mean of pixel intensities within a defined neighborhood. The threshold for each pixel is calculated as the mean of the intensities within a local region, determined by the block size specified. Mathematically, the formula for adaptive mean thresholding can be expressed as:

Adaptive Gaussian Thresholding:
Adaptive Gaussian Thresholding, on the other hand, employs a weighted sum of pixel intensities within the local neighborhood, giving higher importance to the central pixels. The weightings follow a Gaussian distribution, with the central pixel contributing more to the threshold than its neighbors. Mathematically, the formula for adaptive Gaussian thresholding is represented as:

Applications of Adaptive Thresholding:
Document Image Binarization:
- Adaptive thresholding is widely used in document image processing to convert scanned or photographed documents into binary form, facilitating text extraction and recognition.
Object Detection in Varied Lighting Conditions:
- Adaptive thresholding proves effective in scenarios where lighting conditions vary across the image. It allows for localized adjustments, making it robust for object detection in challenging environments.
Medical Image Analysis:
- In medical imaging, adaptive thresholding aids in segmenting regions of interest, such as tumors or abnormalities, by adapting to variations in intensity and contrast.
Robotics and Computer Vision:
- Adaptive thresholding is crucial in robotics and computer vision applications for tasks like obstacle detection and navigation, especially in dynamic environments.
Facial Recognition and Biometrics:
- Facial recognition systems often benefit from adaptive thresholding to handle variations in lighting and facial expressions, improving the accuracy of feature extraction.
infoaryan, computer vision, opencv python projects, image thresholding, adaptive thresholding, adaptive thresholds , opencv projects, opencv python tutorials, aryan verma, infoaryan, aryan verma blog, computer vision, image processing tutorials,