For thresholding a grayscale image, below a threshold value becomes 0, the threshold value is greater than the constant

https://blog.csdn.net/qq_40962368/article/details/80917250

Thresholding image

      Including an image of the target object, as well as background noise, in order to extract from the digital image directly multivalued target object, a common method is to set a threshold value T, T with the data of an image divided into two parts: larger than T T is smaller than the pixel group and the pixel group. This study is the most specific method of gradation conversion is referred to as an image binarization (Binarization).

       Thresholding Method features are: for the target and the background gray with a strong contrast to the situation, it is important that a gray background or objects relatively simple, and can be closed and communication total boundary area.

(A) Simple threshold

Selecting a global threshold, then put the entire image into black and white binary image.

Function cv2.threshold ()

When the new value assigned to this function has four parameters, the first one is the original image matrix, the second threshold value are classified, and the third is above (below) the threshold, a fourth method is the selection parameter, commonly used are:

cv2.THRESH_BINARY (black and white binary)
cv2.THRESH_BINARY_INV (binary black and white inverted)
cv2.THRESH_TRUNC (amount obtained image into a multi-pixel value)
cv2.THRESH_TOZERO (when the pixel value of the pixel above the threshold pixel is set to provide their own, less than when the threshold value is not treated)
cv2.THRESH_TOZERO_INV (to provide their own set of pixel values when the pixel is below the threshold, no treatment is above the threshold)
this function returns two values, a first threshold value, the second threshold value is treated after the image matrix.
 

img = cv2.imread('4.jpg', 0)
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)  # binary (黑白二值)
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)  # (黑白二值反转)
ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)  # 得到的图像为多像素值
ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)  # 高于阈值时像素设置为255,低于阈值时不作处理
ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)  # 低于阈值时设置为255,高于阈值时不作处理
 
print(ret)
 
cv2.imshow('thresh1', thresh1)
cv2.imshow('thresh2', thresh2)
cv2.imshow('thresh3', thresh3)
cv2.imshow('thresh4', thresh4)
cv2.imshow('thresh5', thresh5)
cv2.imshow('grey-map', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Published 458 original articles · won praise 138 · views 240 000 +

Guess you like

Origin blog.csdn.net/zjc910997316/article/details/103629081