图像二值化( Image Binarization)
It is to set the grayscale value of the pixels on the image to 0 or 255, which is the process of presenting an obvious black and white effect to the entire image.
The principle of binarization
import cv2
img = cv2.imread('img/lena.jpg')
# 转为灰度图
new_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
height, width = new_img.shape[0:2]
# 设置阈值
thresh = 60
# 遍历每一个像素点
for row in range(height):
for col in range(width):
# 获取到灰度值
gray = new_img[row, col]
# 如果灰度值高于阈值 就等于255最大值
if gray > thresh:
new_img[row, col] = 255
# 如果小于阈值,就直接改为0
elif gray < thresh:
new_img[row, col] = 0
cv2.imshow('img', new_img)
cv2.waitKey()
Image binarization API provided by OpenCV
threshold()方法参数:
- image matrix
- threshold
- maximum value in the picture
- binarization method
The way of binarization:
THRESH_BINARY | Above the threshold is changed to 255, below the threshold is changed to 0 |
---|---|
THRESH_BINARY_INV | Above the threshold is changed to 0, below the threshold is changed to 255 |
THRESH_TRUNC | Truncate, change above threshold to threshold, max invalidation |
THRESH_TOZERO | Above the threshold does not change, below the threshold changes to 0 |
THRESH_TOZERO_INV | If it is higher than the threshold, it should be 0, and if it is lower than the threshold, it will not change |
import cv2
img = cv2.imread('img/lena.jpg', cv2.IMREAD_GRAYSCALE)
thresh, new_img = cv2.threshold(img, 60, 255, cv2.THRESH_BINARY)
print(thresh)
cv2.imshow('img', img)
cv2.imshow('NEW_IMG', new_img)
cv2.waitKey()
adaptive threshold
Use a global value as the threshold. But this may not be good in all cases. If the image has different lighting conditions in different areas. In this case, Adaptive Thresholding can help. Here, the algorithm thresholds a pixel based on a small area around it. Therefore, we obtain different thresholds for different regions of the same image, which gives better results for images with different lighting.
adaptlive()方法参数:
- image matrix
- Image grayscale maximum
- Method to Calculate Threshold
- threshold type
- processing block size
- The constant C used by the algorithm
- cv2.ADAPTIVE_THRESH_MEAN_C: The threshold is the constant mean C subtracted from the neighborhood.
- cv2.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold is the Gaussian weighted sum of the neighborhood values minus the constant C.
import cv2
img = cv2.imread('img/lena.jpg', cv2.IMREAD_GRAYSCALE)
thresh_img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 5)
cv2.imshow('thresh_img', thresh_img)
cv2.waitKey()
Otsu algorithm (maximum between-class variance method)
图像分割中阈值选取的最佳算法
threshold(gaussian_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
import cv2
img = cv2.imread('img/lena.jpg', cv2.IMREAD_GRAYSCALE)
# 使用255的阈值进行二值化
ret, thresh_img = cv2.threshold(img, 255, 255, cv2.THRESH_BINARY)
cv2.imshow('normal', thresh_img)
# 使用高斯滤波模糊图像 参数1: 图片矩阵 参数2:卷积核 参数3: 越大越模糊
gaussian_img = cv2.GaussianBlur(img, (5, 5), 0)
cv2.imshow('gaussian_img', gaussian_img)
# 使用大津算法0阈值二值化经过高斯滤波模糊后的图像
ret, thresh_img = cv2.threshold(gaussian_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imshow('otsu', thresh_img)
cv2.imshow('img', img)
cv2.waitKey()
Original image:
Use a threshold of 255 for binarization and the image is all black:
Blur an image using a Gaussian filter:
- image matrix
- convolution kernel
- bigger and blurrier
Use the Otsu algorithm with a threshold of 0 to binarize the image blurred by Gaussian filtering: