Chapter 3 - OpenCV Basics - 9 - Image Threshold

front content

It is found that 40 examples of this book need to be combined with the author's ability to learn OpenCV better, and this article begins to learn image thresholds.

Threshold is an important concept in image processing, similar to a "standard line of pixel values". All pixels are compared with this standard line, and finally they are either larger or smaller or equal. The program groups all the pixels according to these results, and then performs "darkening" or "lightening" operations on a certain group of pixel values, making the outline of the entire image more distinct and easier to be recognized by the computer or the naked eye. Thresholding can effectively handle functions such as foreground and background separation. Here we mainly introduce simple threshold processing, adaptive threshold processing, and Otsu threshold processing.

Simple thresholding (fixed threshold)

OpenCV provides the threshold() function for thresholding.

Function syntax description: retval, dst = cv2.threshold( src, thresh, maxval, type )

  • retval : the threshold to return
  • dst : return the processed image, the size type is equal to the original image
  • src : the original image to be thresholded (can be single-channel/multi-channel), generally choose grayscale image
  • thresh : the threshold to set
  • maxval : the maximum value set
  • type : the type of threshold segmentation, the specific rules are as follows:

type

Chinese name

definition

cv2.THRESH_BINARY

Binarization Thresholding

dst( x,y ) = src( x,y ) > thresh ? maxval : 0

cv2.THRESH_BINARY_INV

Debinarization Thresholding

dst( x,y ) = src( x,y ) > thresh ? 0 : maxval

cv2.THRESH_TRUNC

Truncated Thresholding

dst( x,y ) = src( x,y ) > thresh ? thresh : 0

cv2.THRESH_TOZERO_INV

Superthreshold zero processing

dst( x,y ) = src( x,y ) > thresh ? 0 : src( x,y )

cv2.THRESH_TOZERO

Low Threshold Zero Processing

dst( x,y ) = src( x,y ) > thresh ? src( x,y ) : 0

cv2.THRESH_MASK

mask processing

mask

cv2.THRESH_OTSU

Optimal Thresholding

flag, an optional threshold parameter when using Otsu's algorithm

cv2.THRESH_TRIANGLE

Triangle Thresholding

flag, an optional threshold parameter when using the Triangle algorithm

The threshold processing method provided by threshold() is a unified processing method for the pixels of the entire image, and the threshold is set by itself, and the threshold processing is different according to the type.

  • THRESH_BINARY ---- Binarization threshold processing

dst( x,y ) = src( x,y ) > thresh ? maxval : 0

If the pixel is greater than the threshold, the pixel becomes the set maximum value, otherwise it is set to 0, and finally a binary image with only 0 and maxval is obtained.

  • THRESH_BINARY_INV ---- Inverse binarization threshold processing

dst( x,y ) = src( x,y ) >thresh ? 0 : maxval

If the pixel is greater than the threshold, the pixel becomes 0, otherwise it is set to the set maximum value, which is just the opposite of the above, and finally a binary image with only 0 and maxval is obtained.

  • THRESH_TRUNC ---- Truncated thresholding

dst( x,y ) = src( x,y ) >thresh ? thresh : 0

If the pixel is greater than the threshold, the pixel value is set to the threshold, otherwise it is set to 0, and finally a binary image with only 0 and threshold thresh is obtained

  • THRESH_TOZERO_INV ---- over-threshold zero processing

dst( x,y ) = src( x,y )> thresh ? 0 : src( x,y )

If the pixel is greater than the threshold, the pixel is set to 0, otherwise it remains unchanged, and there are no pixels greater than the threshold in the final image (these pixels become 0)

  • THRESH_TOZERO ---- low threshold zero processing

dst( x,y ) = src( x,y ) > thresh ? src( x,y ) : 0

If the pixel is greater than the threshold, the pixel remains unchanged, otherwise it is set to 0, and the pixels smaller than the threshold in the final image become 0

The procedure is as follows:

color_0_255 = cv.imread("0_255.png", 0)  # 灰度图像素带
r, b1 = cv2.threshold(color_0_255, 127, 255, cv2.THRESH_BINARY)  # 二进制阈值化:比阈值大设为最大值,否则为0
r, b2 = cv2.threshold(color_0_255, 127, 255, cv2.THRESH_BINARY_INV)  # 反二进制阈值化:比阈值大设为0,否则为最大值
r, b3 = cv2.threshold(color_0_255, 127, 255, cv2.THRESH_TRUNC)  # 截断阈值化:比阈值大的都设置成阈值
r, b4 = cv2.threshold(color_0_255, 127, 255, cv2.THRESH_TOZERO)  # 反阈值化为0:大于阈值的为0
r, b5 = cv2.threshold(color_0_255, 127, 255, cv2.THRESH_TOZERO_INV)  # 阈值化为0:小于阈值则设为0
cv.imshow("orginal 0~255", color_0_255)
cv.imshow("BINARY", b1)
cv.imshow("BINARY_INV", b2)
cv.imshow("TRUNC", b3)
cv.imshow("TOZERO", b4)
cv.imshow("TOZERO_INV", b5)

cv.waitKey()
cv.destroyAllWindows()

The program runs as follows:

Adaptive Thresholding

For a color-balanced image, one threshold can complete image thresholding, but for an image with unbalanced color, a clear and effective threshold segmentation image cannot be obtained with only one threshold, so there is an adaptive threshold processing method.

Adaptive threshold processing is suitable for images with large differences in light and shade. The threshold is obtained by calculating the weighted average value of the adjacent area around each pixel, and the current pixel is processed using the threshold, so that the whole process is to use the changed threshold to process the image. threshold processing.

Function syntax description: dst = cv2.adaptiveThreshold( src, maxValue, adaptiveMethod, thresholdType, blockSize, C )

  • dst : the processing result of the adaptive threshold
  • src : the original image, must be an 8-bit single-channel image
  • maxValue : maximum value
  • adaptiveMethon : adaptive method, cv2.ADAPTVIVE_THRESH_MEAN_C or cv2.ADAPTIVE_THRESH_GAUSSIAN_C
  • thresholdType : threshold processing method, must be cv2.THRESH_BINARY or cv2.THRESH_BINARY_INV
  • blockSize : Block size, which identifies the neighborhood size used by a pixel when calculating the threshold, usually 3/5/7
  • C : constant

The adaptive threshold is equal to the weighted average value of the neighborhood specified by the parameter blockSize for each pixel minus the constant C, and the weight of each point in the neighborhood is determined by adaptiveMethon.

adaptiveMethon includes the following parameters:

  • cv2.ADAPTVIVE_THRESH_MEAN_C : The weight values ​​of all pixels in the neighborhood are the same
  • cv2.ADAPTIVE_THRESH_GAUSSIAN_C : The weight value of all pixels in the field is related to the distance from the center point, and the weight value of each point is obtained through the Gaussian equation

The procedure is as follows:

import cv2

image_Gray = cv2.imread("boy.png", 0)
t1, thresh_img = cv2.threshold(image_Gray, 127, 255, cv2.THRESH_BINARY)
# 自适应阈值的计算方法为cv2.ADAPTIVE_THRESH_MEAN_C 邻域权重都一样
athdMEAM = cv2.adaptiveThreshold(image_Gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 3)
# 自适应阈值的计算方法为cv2.ADAPTIVE_THRESH_GAUSSIAN_C 邻域权重为高斯加权
athdGAUS = cv2.adaptiveThreshold(image_Gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 3)
cv2.imshow("image_gray", image_Gray)
cv2.imshow("shreshold_img", thresh_img)
cv2.imshow("MEAN_C", athdMEAM)
cv2.imshow("GAUSSIAN_C", athdGAUS)
cv2.waitKey()
cv2.destroyAllWindows()

The result of the operation is as follows:

Otsu Thresholding

For the one-size-fits-all processing method of setting the threshold for simple threshold processing, the threshold set with a high probability is not the optimal threshold, and the obtained processed image is often not the optimal image to be processed.

The Otsu threshold processing method is to select cv2.THRESH_OTSU in the type of cv2.threshold(). At the same time, the set threshold also needs to be changed to 0, so that this method will traverse all possible thresholds to find the best threshold and perform thresholding. Split processing.

The procedure is as follows:

import cv2

image_Gray = cv2.imread("p1.png", 0)
t1, thresh_img_127 = cv2.threshold(image_Gray, 127, 255, cv2.THRESH_BINARY)
print(t1)  # 127
t2, thresh_otsu = cv2.threshold(image_Gray, 0, 255, cv2.THRESH_OTSU)
print(t2)  # 149
cv2.imshow("image_gray", image_Gray)
cv2.imshow("thresh_img_127", thresh_img_127)
cv2.imshow("thresh_otsu", thresh_otsu)
cv2.waitKey()
cv2.destroyAllWindows()

The program runs as follows:

Otsu's method is indeed better than the default setting of 127, but the best feeling is adaptive threshold processing.

Guess you like

Origin blog.csdn.net/sunguanyong/article/details/129352721