The types of thresholding operation are described according to its formulation. Take into account that src is the source (original) image, and dst corresponds to the destination (result) image after thresholding. In this sense, src(x, y) will correspond to the intensity of the pixel (x, y) of the source image, and dst(x, y) will correspond to the intensity of the pixel (x, y) of the destination image.
Here is the formula for cv2.THRESH_BINARY:
So, if the intensity of the pixel src(x, y) is higher than thresh, then the new pixel intensity is set to a maxval parameter. Otherwise, the pixels are set to 0.
Here is the formula for cv2.THRESH_BINARY_INV:
So, if the intensity of the pixel src(x, y) is higher than thresh, then the new pixel intensity is set to 0. Otherwise, it is set to maxval.
Here is the formula for cv2.THRESH_TRUNC:
So, if the intensity of the pixel src(x, y) is higher than thresh, then the new pixel intensity is set to threshold. Otherwise, it is set to src(x, y).
Here is the formula for cv2.THRESH_TOZERO:
So, if the intensity of the pixel src(x, y) is higher than thresh, the new pixel value will be set to src(x, y). Otherwise, it is set to 0.
Here is the formula for cv2.THRESH_TOZERO_INV:
So, if the intensity of the pixel src(x, y) is greater than thresh, the new pixel value will be set to 0. Otherwise, it is set to src(x, y).
Also, the special cv2.THRESH_OTSU and cv2.THRESH_TRIANGLE values can be combined with one of the values previously introduced (cv2.THRESH_BINARY, cv2.THRESH_BINARY_INV, cv2.THRESH_TRUNC, cv2.THRESH_TOZERO, and cv2.THRESH_TOZERO_INV). In these cases (cv2.THRESH_OTSU and cv2.THRESH_TRIANGLE), the thresholding operation (implemented only for 8-bit images) computes the optimal threshold value instead of the specified thresh value. It should be noted that the thresholding operation returns the computed optimal threshold value.
The thresholding_simple_types.py script helps you understand the aforementioned types. We use the same sample image introduced in the previous section, and we perform a thresholding operation with a fixed threshold value (thresh = 100) with all the previous types.
The key code to perform this is as follows:
ret1, thresh1 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY)
ret2, thresh2 = cv2.threshold(gray_image, 100, 220, cv2.THRESH_BINARY)
ret3, thresh3 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY_INV)
ret4, thresh4 = cv2.threshold(gray_image, 100, 220, cv2.THRESH_BINARY_INV)
ret5, thresh5 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_TRUNC)
ret6, thresh6 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_TOZERO)
ret7, thresh7 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_TOZERO_INV)
As previously mentioned, the maxval parameter sets the maximum value to use only with the cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV thresholding types. In this example, we have set the value of maxval to 255 and 220 for the cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV types in order to see how the thresholded image changes in both cases. The output of this script can be seen in the next screenshot:
In the previous screenshot, you can see both the original grayscale image and the result of each of the seven thresholding operations that were performed. Additionally, you can see the effect of the maxval parameter, used only with the cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV thresholding types. More specifically, see, for example, the difference between the first and second thresholding operation results – white versus gray in the result images – and also the difference between the third and fourth thresholding operation results – white versus gray in the result images.