Image binarization
- Image binarization: It is to set the gray value of the pixel on the image to 0 or 255, that is, to present the entire image with an obvious visual effect of only black and white. Realized based on the histogram of the image, 0 white 1 black
- An image includes the target object, background and noise. To directly extract the target object from a multi-valued digital image, the common method is to set a threshold T, and use T to divide the image data into two parts: Pixel groups and pixel groups smaller than T. This is the most special method for studying grayscale transformation, called image binarization (Binarization)
threshold
- Threshold, also known as critical value, refers to the lowest or highest value that an effect can produce.
- In image processing, it means the critical point of color conversion. This method is only used in binarized images; as in nature, each color has a value, usually by RGB (that is, the three primary colors of red, green, and blue) ) will be mixed in proportion to get a variety of different colors. Thresholding an image is a way to apply special processing to colors.
- In detail, the threshold is a conversion critical point, no matter what kind of color your picture is, it will eventually treat the picture as a black and white picture; that is to say, after you set a threshold, it will use this value as a standard . Colors above this value are converted to white, and colors below this value are converted to black, so the end result is that you get a black and white picture.
- Use the function of threshold: get a black and white picture with different contrast. A threshold can be a minimum value: a certain performance characteristic cannot fall below this value
1. Global Threshold
cv2.threshold(src, thresh, maxval, type[, dst]) |
-
src: indicates the image source
-
thresh: indicates the threshold (starting value)
-
maxval: indicates the maximum value
-
type: Indicates what type of algorithm is used when dividing here, and the common value is 0 (cv2.THRESH_BINARY)
-
For the last parameter, common threshold types are:
THRESH_BINARY=0,THRESH_BINARY_INV,THRESH_TRUNC,THRESH_TOZERO,THRESH_TOZERO_INV,THRESH_OTSU,THRESH_TRIANGLE,THRESH_MASKParameter description of the function threshold():
cv.THRESH_BINARY | cv.THRESH_OTSU)#大律法,全局自适应阈值 参数0可改为任意数字但不起作用 cv.THRESH_BINARY | cv.THRESH_TRIANGLE)#TRIANGLE法,,全局自适应阈值, 参数0可改为任意数字但不起作用,适用于单个波峰 cv.THRESH_BINARY)# 自定义阈值为150,大于150的是白色 小于的是黑色 cv.THRESH_BINARY_INV)# 自定义阈值为150,大于150的是黑色 小于的是白色 cv.THRESH_TRUNC)# 截断 大于150的是改为150 小于150的保留 cv.THRESH_TOZERO)# 截断 小于150的是改为150 大于150的保
-
-
Function return value: the first is the threshold (T), the second is the processed image
def threshold_image(image):
gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
cv.namedWindow('image', cv.WINDOW_NORMAL)
cv.imshow('image', gray)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) # 大律法,全局自适应阈值,参数0可改成任意数字但不起作用
print("阈值:%s" % ret)
cv.namedWindow('OTSU', cv.WINDOW_NORMAL)
cv.imshow("OTSU", binary)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE) # TRIANGLE法,全局自适应阈值,参数0可改为任意数字,但不起作用,适用于单个峰波
print("阈值:%s" % ret)
cv.namedWindow('TRIANGLE', cv.WINDOW_NORMAL)
cv.imshow("TRIANGLE", binary)
ret, binary = cv.threshold(gray, 150, 255, cv.THRESH_BINARY) # 自定义阈值为150 大于150的是白色,小于150的是黑色
print("阈值:%s" % ret)
cv.namedWindow('zidingyi', cv.WINDOW_NORMAL)
cv.imshow("zidingyi", binary)
ret, binary = cv.threshold(gray, 150, 255, cv.THRESH_BINARY_INV) # 自定义阈值为150 大于150的是黑色,小于150是白色
print("阈值:%s" % ret)
cv.namedWindow('zidingyifanse', cv.WINDOW_NORMAL)
cv.imshow("zidingyifanse", binary)
ret, binary = cv.threshold(gray, 150, 255, cv.THRESH_TRUNC) # 截断大于150的更改为150, 小于150的保留
print("阈值:%s" % ret)
cv.namedWindow('jieduan1', cv.WINDOW_NORMAL)
cv.imshow("jieduan1", binary)
ret, binary = cv.threshold(gray, 150, 255, cv.THRESH_TOZERO) # 截断小于150的更改为150, 大于150的保留
print("阈值:%s" % ret)
cv.namedWindow('jieduan2',cv.WINDOW_NORMAL)
cv.imshow("jieduan2", binary)
src = cv.imread('./static/image/blur.jpg')
threshold_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
operation result:
2. Local Threshold
The adaptive threshold binarization function calculates the threshold value of the corresponding area according to the value of a small area of the picture, so as to obtain a more suitable picture.
dst = cv2.adaptiveThreshold(src, maxval, thresh_type, type, Block Size, C) |
- dst: output map
- src: Input image, only single-channel image can be input, usually a grayscale image
- maxval: When the pixel value exceeds the threshold (or less than the threshold, depending on the type), the value assigned
- thresh_type: Threshold calculation method, including the following 2 types: cv2.ADAPTIVE_THRESH_MEAN_C; cv2.ADAPTIVE_THRESH_GAUSSIAN_C.
- type: The type of binarization operation, which is the same as the fixed threshold function, including the following five types: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO; cv2.THRESH_TOZERO_INV.
- Block Size: the size of the block in the picture
- C : Constant term in the threshold calculation method
code show as below:
def local_image(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.namedWindow("image", cv.WINDOW_NORMAL)
cv.imshow("image", gray)
binary1 = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 25, 10)
cv.namedWindow("image1", cv.WINDOW_NORMAL)
cv.imshow("image1", binary1)
cv.imwrite("./img/H11-1.jpg", binary1)
binary2 = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10)#高斯处理
cv.namedWindow("image2", cv.WINDOW_NORMAL)
cv.imshow("image2", binary2)
cv.imwrite("./img/H11-2.jpg", binary2)
src = cv.imread("./static/image/windows.jpg")
local_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
running result:
3. Find the image average threshold
Find the mean value of the image as a threshold to binarize
def custom_image(image):
gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
cv.namedWindow('image', cv.WINDOW_NORMAL)
cv.imshow("image", gray)
h, w = gray.shape[:2]
m = np.reshape(gray, [1, w*h]) # 化为一维数组
mean = m.sum() / (w*h)
print("mean: ", mean)
ret, binary = cv.threshold(gray, mean, 255, cv.THRESH_BINARY)
cv.namedWindow("twoValue", cv.WINDOW_NORMAL)
cv.imshow('twoValue', binary)
src = cv.imread('./static/image/blur.jpg')
custom_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
running result: