[python + opencv image processing three] image threshold processing

(1) Simple threshold
Simple threshold is of course the simplest, select a global threshold, and then divide the entire image into binary images that are either black or white. The function is cv2.threshold()
. This function has four parameters:
the first original image, the second threshold for classification, the third is the new value assigned when it is higher (lower) than the threshold, and the fourth is a Method selection parameters, commonly used are:
cv2.THRESH_BINARY (black and white binary)
cv2.THRESH_BINARY_INV (black and white binary inversion)
cv2.THRESH_TRUNC (obtained image is multi-pixel value)
cv2.THRESH_TOZERO
cv2.THRESH_TOZERO_INV
This function has two returns value, the first retVal (the obtained threshold value (will be used in the next method)), and the second is the thresholded image.

(2) Adaptive threshold:
As seen earlier, the simple threshold is a global threshold, and only one threshold value needs to be specified, and the entire image is compared with this threshold. The adaptive threshold can be regarded as a local threshold, by specifying an area size, comparing the size relationship between this point and the average value (or other features) of the pixels in the area size to determine whether the pixel belongs to black or white (if is a binary case). The function used is: cv2.adaptiveThreshold()

This function needs to fill in 6 parameters:
the first original image
, the second pixel value upper limit,
and the third adaptive method Adaptive Method:
cv2.ADAPTIVE_THRESH_MEAN_C: the mean value in the field
cv2.ADAPTIVE_THRESH_GAUSSIAN_C: the weighted sum of pixels in the field, and the weight is a Gaussian
The assignment method of the fourth value of the window : only cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV
The fifth Block size: specifies the size of the field (a square field)
The sixth constant C, the threshold is equal to the mean value or the weighted value minus this constant (for 0 is equivalent to the threshold is to find the mean or weighted value in the field)

This method has a better effect in theory, which is equivalent to dynamically and adaptively adjusting the threshold value of its own pixels, instead of using a threshold value for the entire image.

Guess you like

Origin blog.csdn.net/poppyty/article/details/118495716