Adaptive Thresholding using OpenCV
Adaptive thresholding addresses one of the main limitations of simple (global) thresholding its inability to handle images with varying lighting conditions. Instead of using a single global threshold value for the whole image, adaptive thresholding calculates the threshold for small regions around each pixel. This approach provides better results for images where illumination changes across different parts.
- Instead of a constant threshold for all pixels, the image is divided into smaller blocks and a threshold is computed for each region.
- Helps segment images effectively even if lighting is not uniform, making it ideal for documents or scenes with shadows and bright spots.
Step-by-Step Implementation
Step 1: Import libraries and Image Preparation
Sample image can be downloaded from here.
Let's import the required libraries and load our image on which we will perform the operations,
- cv2: Handles image reading, processing and applies thresholding techniques.
- numpy: Supports efficient array operations, enabling fast image data handling.
- matplotlib.pyplot: Displays images and results in Colab notebooks.
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('input1.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Step 2: Helper Function
Define the helper function which helps in displaying the images,
def show_image(img, title):
plt.imshow(img, cmap='gray')
plt.title(title)
plt.axis('off')
plt.show()
Step 3: Display Original Image
show_image(gray_image, "Original Grayscale Image")
Output:

Step 4: Adaptive Mean Thresholding
- The threshold for each pixel is computed as the mean value of the surrounding block (199×199 neighborhood, here), minus the constant (5).
- Best for images with fairly consistent noise or lighting within local regions.
thresh_mean = cv2.adaptiveThreshold(
gray_image, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
199, 5
)
show_image(thresh_mean, "Adaptive Mean Thresholding")
Output:

Step 5: Adaptive Gaussian Threosholding
- The threshold for each pixel comes from a weighted sum of surrounding pixels (Gaussian window), minus the constant (5).
- More effective than mean in regions with gradual intensity variations.
thresh_gauss = cv2.adaptiveThreshold(
gray_image, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
199, 5
)
show_image(thresh_gauss, "Adaptive Gaussian Thresholding")
Output:
