OpenCV common functions - grayscale processing and image binarization processing

1. Grayscale processing

1.1 cvtColor function

函数原型:

cv2.cvtColor(src, code[, dst[, dstCn]]) -> dst

功能: Convert image color space.
参数:

  • src: input image.
  • code: Color space conversion code. You can take the constant cv2.COLOR_BGR2GRAY or cv2.COLOR_RGB2GRAY.
  • dst: output image.
  • dstCn: The number of channels of the output image. If set to 0, it will be automatically set following the conversion code.

Built-in function sample code:

import cv2
img = cv2.imread("color.jpg")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Gray", img_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

灰度处理的原理是将彩色图像转换成灰度图像, the commonly used method is to weight the average of the values ​​of the three RGB channels of the color image to obtain a grayscale value, and then use this grayscale value to replace the values ​​of the three RGB channels to obtain a grayscale image. The commonly used weighted average formula is:
gray = 0.299 × R + 0.587 × G + 0.114 × B gray = 0.299 \times R + 0.587 \times G + 0.114 \times Bgray=0.299×R+0.587×G+0.114×B
inside,R, G, BR, G, BR , G , and B are the values ​​of the red, green, and blue channels of the color image, respectively,0.299, 0.587, 0.114 0.299, 0.587, 0.1140.299 , 0.587 , and 0.114 are weighting coefficients. These coefficients are derived from experimental results in psychophysics based on the human eye's sensitivity to different colors. This formula is also called the brightness weighting method, which can better preserve the light and dark contrast of color images.

Implement grayscale processing code through principles:

import cv2
img = cv2.imread('lean.jpg')
img = cv2.resize(img, (240, 240), interpolation=cv2.INTER_CUBIC)
height, width = img.shape[:2]
gray = np.zeros((height, width, 1), dtype="uint8")
for i in range(height):
    for j in range(width):
        gray[i][j] = img[i][j][0] * 0.114 + img[i][j][1] * 0.587 + img[i][j][2] * 0.299  # 加权值法
        # gray[i][j] = (img[i][j][0] +img[i][j][1]+img[i][j][2])/3#平均值法
        # gray[i][j] = max(img[i][j][0],img[i][j][1],img[i][j][2]) #最大值法
        
cv2.imshow("Gray", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

The above code calculates the grayscale value pixel by pixel, and then replaces the values ​​of the three RGB channels with the grayscale value to obtain a grayscale image and display it. It is worth noting that although this method is simple and easy to understand, it is less efficient in calculating the gray value of each pixel and is not suitable for processing larger images. In practice, we can use the functions provided by OpenCV to implement grayscale processing to improve the execution efficiency of the program.

注意:灰度图像在Python中数据类型是numpy的uint8类型,即8位无符号整型。

2. Image binarization processing

图像二值化处理是将灰度图像上的像素点转化为黑白两种颜色的处理方法。The principle is to segment the pixel values ​​of grayscale images according to a certain threshold. The pixel values ​​greater than or equal to the threshold are set to one value (usually 255), and the pixel values ​​smaller than the threshold are set to another value (usually 0). The image obtained in this way has only two colors: black and white, which is convenient for some morphological processing and feature extraction.

There are two methods for image binarization: 全局阈值和自适应阈值. 全局阈值It means that the pixel values ​​of the entire image are segmented according to the same threshold, but 自适应阈值the image is segmented into 若干个小块and then 单独the threshold is calculated for each small block to obtain a more accurate segmentation result. The implementation of these two methods is introduced below.

2.1 Global threshold

In OpenCV, cv2.threshold()global threshold binarization can be achieved using functions. The function prototype is as follows:

retval, dst = cv2.threshold(src, thresh, maxval, type)

参数说明:

  • src is the input image, which must be a grayscale image.
  • dst is the output image, the size and type are the same as the original image.
  • thresh is the set threshold.
  • maxval is the maximum value. When the pixel value is greater than the threshold, the value is assigned to maxval.
  • type is the type of threshold processing, including the following types:
    • cv2.THRESH_BINARY: Binarization, when the pixel value is greater than the threshold, the value is assigned to maxval, otherwise the value is assigned to 0.
    • cv2.THRESH_BINARY_INV: Anti-binarization. When the pixel value is greater than the threshold, the value is assigned to 0, otherwise the value is maxval.
    • cv2.THRESH_TRUNC: truncation, when the pixel value is greater than the threshold, the value is assigned to the threshold, otherwise it remains unchanged.
    • cv2.THRESH_TOZERO: Return to zero below the threshold. When the pixel value is less than the threshold, it is set to 0, otherwise it remains unchanged.
    • cv2.THRESH_TOZERO_INV: Return to zero above the threshold. When the pixel value is greater than the threshold, it is set to 0, otherwise it remains unchanged.

The sample code is as follows:

import cv2

# 加载灰度图
img_gray = cv2.imread('lena_gray.jpg', 0)

# 全局阈值二值化
retval, img_binary = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)

# 显示图像
cv2.imshow('original', img_gray)
cv2.imshow('binary', img_binary)
cv2.waitKey()
cv2.destroyAllWindows()

Among them, when using cv2.imread() to load a grayscale image, the second parameter must be set to 0, indicating that the grayscale image is loaded.

2.2 Adaptive threshold

In OpenCV, this cv2.adaptiveThreshold()can be achieved using functions 自适应阈值二值化. The function prototype is similar to the previous cv2.threshold() function, but with two more parameters:

dst = cv2.adaptiveThreshold(src, maxval, adaptiveMethod, thresholdType, blockSize, C)

参数说明:

  • src is the input image, which must be a grayscale image.
  • maxValue is the maximum value. When the pixel value is greater than the threshold, the value is assigned to maxValue.
  • adaptiveMethod is an adaptive threshold algorithm type, including the following two types:
    • cv2.ADAPTIVE_THRESH_MEAN_C: Calculate the average gray value of each small area as the threshold.
    • cv2.ADAPTIVE_THRESH_GAUSSIAN_C: Calculate the Gaussian weighted average gray value of each small area as the threshold.
  • thresholdType is the type of threshold processing, which is the same as the global threshold binarization function.
  • blockSize is the block size, which is required to be an odd number and represents the small area size used in adaptive threshold calculation.
  • C is a constant, a constant that reduces the threshold.
    The example code is as follows:
import cv2

# 加载灰度图
img_gray = cv2.imread('lena_gray.jpg', 0)

# 自适应阈值二值化
img_binary = cv2.adaptiveThreshold(
    img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 127, 1)

# 显示图像
cv2.imshow('original', img_gray)
cv2.imshow('binary', img_binary)
cv2.waitKey()
cv2.destroyAllWindows()

Among them, cv2.ADAPTIVE_THRESH_MEAN_C is used as the adaptive threshold algorithm to calculate the average value of each small block; cv2.THRESH_BINARY is used as the type of threshold processing. The threshold size is jointly determined by blockSizetwo Cparameters, and these two parameters can be adjusted to obtain a more suitable segmentation effect.

Guess you like

Origin blog.csdn.net/m0_63007797/article/details/134023682