OpenCV Image Thresholding
In image processing, thresholding is a commonly used technique for converting an image into a binary image (i.e., a black-and-white image). By setting a threshold, pixels in the image can be divided into two categories: pixels above the threshold and pixels below the threshold.
OpenCV provides a variety of thresholding methods. This article will introduce in detail three common thresholding techniques: simple thresholding, adaptive thresholding, and Otsu's binarization.
1. Simple Thresholding (cv2.threshold())
Simple thresholding is the most basic thresholding method. It divides pixels in an image into two categories by setting a fixed threshold.
OpenCV provides the cv2.threshold() function to achieve this.
Function prototype
retval, dst = cv2.threshold(src, thresh, maxval, type)
Parameter Description
src: input image, usually a grayscale image.thresh: the set threshold.maxval: the new value assigned when the pixel value exceeds (or is less than, depending on the type) the threshold.type: thresholding type; common types include:cv2.THRESH_BINARY: if the pixel value is greater than the threshold, assignmaxval, otherwise assign0。cv2.THRESH_BINARY_INV: Similar tocv2.THRESH_BINARYConversely, 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: Similar tocv2.THRESH_TOZEROConversely, if the pixel value is greater than the threshold, assign0, otherwise keep it unchanged.
Return Value
retval: the threshold actually used (in some cases it may differ from the set threshold).dst: the processed image.
Example
Example
import
cv2
import
numpy
as
np
Read an image
img
=
cv2.
imread
(
'image.jpg'
,
0
)
Simple thresholding
ret
,
thresh1
=
cv2.
threshold
(
img
,
127
,
255
,
cv2.
THRESH_BINARY
)
Display Results
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 computing 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 thresholding method; common types include:cv2.ADAPTIVE_THRESH_MEAN_C: the threshold is the average of the neighborhood minus a constantC。cv2.ADAPTIVE_THRESH_GAUSSIAN_C: the threshold is the weighted average of the neighborhood minus a constantC, where the weights are determined by a Gaussian function.
thresholdType: thresholding type, usuallycv2.THRESH_BINARYorcv2.THRESH_BINARY_INV。blockSize: the neighborhood size used when computing the threshold, which must be an odd number.C: the constant subtracted from the average or weighted average.
Return Value
dst: the processed image.
Example
Example
import
cv2
import
numpy
as
np
Read an 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 Results
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 for automatically determining 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
cv2
import
numpy
as
np
Read an image
img
=
cv2.
imread
(
'image.jpg'
,
0
)
Otsu's binarization
ret
,
thresh3
=
cv2.
threshold
(
img
,
0
,
255
,
cv2.
THRESH_BINARY
- cv2.
THRESH_OTSU
)
Display Results
cv2.
imshow
(
'Otsu's Threshold'
,
thresh3
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)