OpenCV Image Thresholding
In image processing, thresholding is a common technique used to convert an image into a binary image (i.e., black-and-white image). By setting a threshold, the pixels in an image can be divided into two categories: pixels above the threshold and pixels below the threshold.
OpenCV provides multiple thresholding methods. This article will introduce three common thresholding techniques in detail: simple thresholding, adaptive thresholding, and Otsu's binarization.
1. Simple Thresholding (cv2.threshold())
Simple thresholding is the most basic thresholding method. It divides the pixels in an image into two categories by setting a fixed threshold.
OpenCV providescv2.threshold()function to implement this functionality.
Function prototype
retval, dst = cv2.threshold(src, thresh, maxval, type)
Parameter Description
src: Input image, usually a grayscale image.thresh: The threshold value to set.maxval: The new value assigned when the pixel value exceeds (or is less than, depending on the type) the threshold.type: The thresholding type. Common types include:cv2.THRESH_BINARY: If the pixel value is greater than the threshold, assignmaxval, otherwise assign0。cv2.THRESH_BINARY_INV: Withcv2.THRESH_BINARYon the contrary, if the pixel value is greater than the threshold, assign0, otherwise assignmaxval。cv2.THRESH_TRUNC: If the pixel value is greater than the threshold, assign the threshold; otherwise, keep it unchanged.cv2.THRESH_TOZERO: If the pixel value is greater than the threshold, keep it unchanged; otherwise, assign0。cv2.THRESH_TOZERO_INV: Withcv2.THRESH_TOZEROon the contrary, if the pixel value is greater than the threshold, assign0, otherwise keep it unchanged.
Return value
retval: The threshold actually used (may differ from the set threshold in some cases).dst: The processed image.
Example
Example
import numpy as np
# Read image
img = cv2.imread('image.jpg', 0)
# Simple thresholding
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# Display result
cv2.imshow('Binary Threshold', thresh1)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. Adaptive Thresholding (cv2.adaptiveThreshold())
In some cases, the brightness distribution of an image is uneven, and using a fixed threshold may not produce ideal results. Adaptive thresholding handles this situation better by calculating different thresholds for different regions of the image.
Function prototype
dst = cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)
Parameter Description
src: Input image, usually a grayscale image.maxValue: The new value assigned when the pixel value exceeds (or is less than, depending on the type) the threshold.adaptiveMethod: Adaptive threshold calculation method. Common types include:cv2.ADAPTIVE_THRESH_MEAN_C: The threshold is the mean of the neighborhood minus the constantC。cv2.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold is the weighted mean of the neighborhood minus the constantC, the weights are determined by a Gaussian function.
thresholdType: Thresholding type, usuallycv2.THRESH_BINARYorcv2.THRESH_BINARY_INV。blockSize: The neighborhood size used when calculating the threshold; it must be odd.C: The constant subtracted from the mean or weighted mean.
Return value
dst: The processed image.
Example
Example
import numpy as np
# Read image
img = cv2.imread('image.jpg', 0)
# Adaptive thresholding
thresh2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
# Display result
cv2.imshow('Adaptive Threshold', thresh2)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. Otsu's Binarization (cv2.threshold() with cv2.THRESH_OTSU)
Otsu's binarization is a method that automatically determines the threshold. It finds the optimal global threshold by maximizing the between-class variance, and is suitable for bimodal images (i.e., images whose histogram has two distinct peaks).
Function prototype
retval, dst = cv2.threshold(src, thresh, maxval, type)
Parameter Description
src: Input image, usually a grayscale image.thresh: Since Otsu's method automatically determines the threshold, this parameter is usually set to0。maxval: The new value assigned when the pixel value exceeds (or is less than, depending on the type) the threshold.type: Thresholding type, usuallycv2.THRESH_BINARYorcv2.THRESH_BINARY_INV, and addcv2.THRESH_OTSU。
Return value
retval: The automatically determined threshold.dst: The processed image.
Example
Example
import numpy as np
# Read image
img = cv2.imread('image.jpg', 0)
# Otsu's binarization
ret, thresh3 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Display result
cv2.imshow('Otsu\'s Threshold', thresh3)
cv2.waitKey(0)
cv2.destroyAllWindows()