Thresholding threshold(), adaptive threshold adaptiveThreshold()

1 thresholding

A type of image segmentation: Image thresholding (also called binarization in some cases) is a type of image segmentation. It is generally used to distinguish the area of ​​interest from the background. The process is to compare each pixel with the threshold. , the required pixels to be separated are set to a specific white value of 255 or a black value of 0, depending on the actual usage requirements.

Threshold function interface form: cv2.threshold(src, thresh, maxval, type[, dst]) ->retval, dst
This method returns 2 values, the first value retval is the threshold, and the second value dst is the thresholded image.
Parameter meaning:

src: source image, 8bit or 32bit floating point type, it can be a multi-channel image when the type does not use the cv2.THRESH_OTSU or cv2.THRESH_TRIANGLE flag;
thresh: the comparison threshold;
maxval: single pixel conversion when the thresholding method is THRESH_BINARY and THRESH_BINARY_INV The maximum value after;
type: thresholding type;

img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
thr_val, img_ret11 = cv2.threshold(img_gray, 50, 255, cv2.THRESH_BINARY )
thr_val, img_ret12 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY )
thr_val, img_ret13 = cv2.threshold(img_gray, 200, 255, cv2.THRESH_BINARY_INV )
thr_val, img_ret21 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC )
thr_val, img_ret22 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO )
thr_val, img_ret23 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV )
thr_val, img_ret31 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
thr_val, img_ret32 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY|cv2.THRESH_TRIANGLE)

When the type is set with the cv2.THRESH_OTSU or cv2.THRESH_TRIANGLE flag, the input parameter thresh has no practical meaning, and the image will not change following the thresh. This is because this method will automatically calculate the threshold based on the image. At this time, the threshold() function returns The first value is the automatically calculated threshold.

thresh,img_bin = cv2.threshold(img_gray,127,255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)

threshold() uses a specific threshold for binarization in an image, either by specifying the threshold in the input parameter thresh, or by automatically calculating the threshold based on the image pixel value when the cv2.THRESH_OTSU or cv2.THRESH_TRIANGLE flag is set. , no matter which method uses the same threshold for the same picture.
In some cases, due to differences in light, the brightness of a certain area of ​​a picture may be significantly different from other areas. In this case, the image effect obtained by thresholding the entire image with a single value may be very poor.

2.Adaptive threshold

Interface form of adaptiveThreshold():

cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) ->dst

src: source image;
maxValue: the maximum value after conversion of a single pixel;
adaptiveMethod: adaptive method: cv2.ADAPTIVE_THRESH_MEAN_C (average method) or cv2.ADAPTIVE_THRESH_GAUSSIAN_C (Gaussian method)
thresholdType: thresholding type, choose one of THRESH_BINARY or THRESH_BINARY_INV;
blockSize : The window size used when calculating the threshold used by a certain pixel, odd value;
C: The constant value subtracted from the average or weighted average; can be a positive value, 0 or negative value;

adaptiveMethodDetailed explanation of parameters:
cv2.ADAPTIVE_THRESH_MEAN_C (Average method): At this time, the threshold is equal to the average value of adjacent pixels with a window size of blockSize minus C;
cv2.ADAPTIVE_THRESH_GAUSSIAN_C (Gaussian method): The threshold is equal to the Gaussian window of adjacent pixels with a window size of blockSize. Cross-correlation weighted sum minus C.

import cv

img = cv2.imread('..\\leaf.jpeg') 
img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('img',img)
thresh,img_bin = cv2.threshold(img_gray,10,255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
cv2.imshow('threshold',img_bin)
img_ret11 = cv2.adaptiveThreshold(img_gray,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,55,0 ) 
cv2.imshow('adaptiveThreshold',img_ret11)
img_ret12 = cv2.adaptiveThreshold(img_gray,255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,55,0 ) 
cv2.imshow('adaptive2',img_ret12)
cv2.waitKey()

The threshold() method does not need to specify a threshold when using the Otsu method and the trigonometric method. The threshold will be automatically calculated based on the pixel value. Other methods require specifying the threshold size. The threshold() method uses a global single threshold, while the adaptiveThreshold() method uses a local threshold, so it can achieve better results when processing unevenly lit images.

Guess you like

Origin blog.csdn.net/aqiangdeba/article/details/129766392