Opencv Image Thresholding
In image processing, thresholding is a commonly used technique to convert an image into a binary image (i.e., black and white image). By setting a threshold, the pixels in the image can be divided into two categories: pixels above the threshold and pixels below the threshold.
OpenCV provides various 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 pixels in the image into two categories by setting a fixed threshold.
OpenCV provides the `cv2.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 set threshold value.
* `maxval`: The new value assigned when the pixel value exceeds (or is less than, depending on the type) the threshold.
* `type`: The type of thresholding operation. Common types include:
* `cv2.THRESH_BINARY`: If the pixel value is greater than the threshold, assign `maxval`, otherwise assign `0`.
* `cv2.THRESH_BINARY_INV`: Opposite to `cv2.THRESH_BINARY`. If the pixel value is greater than the threshold, assign `0`, otherwise assign `maxval`.
* `cv2.THRESH_TRUNC`: If the pixel value is greater than the threshold, assign the threshold value, otherwise keep unchanged.
* `cv2.THRESH_TOZERO`: If the pixel value is greater than the threshold, keep unchanged, otherwise assign `0`.
* `cv2.THRESH_TOZERO_INV`: Opposite to `cv2.THRESH_TOZERO`. If the pixel value is greater than the threshold, assign `0`, otherwise keep unchanged.
### Return Value
* `retval`: The actual threshold used (may differ from the set threshold in some cases).
* `dst`: The processed image.
### Example
## Example
import cv2
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 yield ideal results. Adaptive thresholding calculates different thresholds for different regions of the image, thus better handling this situation.
### 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`: The adaptive threshold calculation method. Common types include:
* `cv2.ADAPTIVE_THRESH_MEAN_C`: The threshold is the mean value of the neighborhood minus a constant `C`.
* `cv2.ADAPTIVE_THRESH_GAUSSIAN_C`: The threshold is the weighted mean value of the neighborhood minus a constant `C`, where the weights are determined by a Gaussian function.
* `thresholdType`: The type of thresholding operation, usually `cv2.THRESH_BINARY` or `cv2.THRESH_BINARY_INV`.
* `blockSize`: The neighborhood size used to calculate the threshold, must be an odd number.
* `C`: The constant subtracted from the mean or weighted mean.
### Return Value
* `dst`: The processed image.
### Example
## Example
import cv2
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 for automatically determining the threshold. It finds the optimal global threshold by maximizing the inter-class variance, 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 to `0`.
* `maxval`: The new value assigned when the pixel value exceeds (or is less than, depending on the type) the threshold.
* `type`: The type of thresholding operation, usually `cv2.THRESH_BINARY` or `cv2.THRESH_BINARY_INV`, plus `cv2.THRESH_OTSU`.
### Return Value
* `retval`: The automatically determined threshold.
* `dst`: The processed image.
### Example
## Example
import cv2
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()
YouTip