π‘ Problem Formulation: In image processing, thresholding is a technique that converts an image into a binary image, where the pixels either become solid black or white, effectively segmenting the image into foreground and background. Adaptive thresholding, unlike simple thresholding, changes the threshold dynamically over the image to handle differing lighting conditions. This article demonstrates how to perform adaptive mean and Gaussian thresholding using Python’s OpenCV library. An example of input is a grayscale image with varying illumination, and the desired output is a well-segmented binary image.
Method 1: Adaptive Mean Thresholding
Adaptive mean thresholding considers the mean of a neighbourhood area for each pixel to determine the threshold value. The cv2.adaptiveThreshold() function from OpenCV is used with cv2.ADAPTIVE_THRESH_MEAN_C as the adaptive method parameter. This method works well for images with relatively uniform lighting across the entire image.
β₯οΈ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month
Here’s an example:
import cv2
import numpy as np
# Read the image in grayscale mode
image = cv2.imread('example_grayscale.jpg', 0)
# Apply the adaptive mean thresholding
mean_thresholded = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 11, 2)
# Display the result
cv2.imshow('Adaptive Mean Thresholded', mean_thresholded)
cv2.waitKey(0)
cv2.destroyAllWindows()The output is a binary image where the threshold level is set adaptively for each pixel based on the mean of neighbouring pixel values.
In this code, the image is first read in grayscale. The cv2.adaptiveThreshold() function is then called with appropriate parameters, including the block size (the size of the neighbourhood area, here set to 11) and a constant subtracted from the mean (set to 2) to fine-tune the thresholding. The result is displayed using cv2.imshow().
Method 2: Adaptive Gaussian Thresholding
Adaptive Gaussian thresholding considers the weighted sum of neighbourhood values where the weights are a Gaussian window. This is particularly useful for images with varying lighting conditions. OpenCV’s cv2.adaptiveThreshold() function with cv2.ADAPTIVE_THRESH_GAUSSIAN_C allows this operation, helping to preserve detail in regions with significant luminosity variance.
Here’s an example:
import cv2
import numpy as np
# Read the image in grayscale mode
image = cv2.imread('example_grayscale.jpg', 0)
# Apply the adaptive Gaussian thresholding
gaussian_thresholded = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
# Display the result
cv2.imshow('Adaptive Gaussian Thresholded', gaussian_thresholded)
cv2.waitKey(0)
cv2.destroyAllWindows()The output is a binary image where the threshold for each pixel is calculated with a Gaussian-weighted sum of the neighbourhood pixel values.
Similar to the first method, the image is loaded in grayscale. Here, the cv2.ADAPTIVE_THRESH_GAUSSIAN_C parameter instructs the function to use a Gaussian-weighted sum of the neighbours. The block size and constant in the cv2.adaptiveThreshold() remain the same as in Method 1, but now they relate to a Gaussian window.
Bonus One-Liner Method 3: Quick Adaptive Thresholding with OpenCV
For a quick and concise implementation, you can perform adaptive thresholding in Python using a one-liner that reads, applies thresholding, and displays the result. It is a rapid way to prototype and see the effects of adaptive thresholding on an image.
Here’s an example:
cv2.imshow('Quick Adaptive Threshold', cv2.adaptiveThreshold(cv2.imread('example_grayscale.jpg', 0), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2))
cv2.waitKey(0)
cv2.destroyAllWindows()The output will be the same as Method 1: a binary image with mean adaptive thresholding applied.
This approach combines the image reading, the adaptive thresholding process, and the display of the image in single lines, which reduces the code’s length and complexity for quick results or demonstrations.
Summary/Discussion
- Method 1: Adaptive Mean Thresholding. Simple and effective for consistent lighting conditions. Weakness: may fail in complex lighting scenarios.
- Method 2: Adaptive Gaussian Thresholding. Better for varied lighting conditions, and preserves more detail. Weakness: Computationally more intensive than mean thresholding.
- Method 3: One-Liner Quick Adaptive Thresholding. Offers rapid development and testing, with fast visualization. Weakness: Not suitable for production or fine-tuned control.
