OpenCV python (four) image preprocessing: binarization && filtering operation

1. Binarization

Binarization refers to setting the pixel values ​​of the original image to 0 or 255 through a threshold, and finally obtain a one-dimensional image with pixel values ​​of 0 or 255. Under normal circumstances, the images we obtain or after color space conversion are all 3-channel images. Most of the useless information can be removed through binarization, and the required information can be kept while being divided into 0 or 255 blacks . Points and white points, and reducing the dimensionality of an image with 3 channels to an image with 1 channel can eliminate most of the useless information and reduce the amount of calculation . Binarization is an image preprocessing method often used in image processing. The following are two common binarization methods.

1. Grayscale image binarization

(1), threshold function

The following is the grayscale image binarization function provided by opencv-python:

t,img_bin = cv2.threshold(img,thresh,maxval,type)	#二值化,阈值设置
'''
其中t为阈值,img_bin为输出图; img为输入图像,thresh为阈值,maxval为最大值,
type为模式,如下所示:
cv2.THRESH_BINARY	#大于阈值的部分取最大值,小于等于阈值的部分取0
cv2.THRESH_BINARY_INV	#大于阈值的部分取0,小于等于阈值的部分取最大值
cv2.THRESH_TOZERO	#大于阈值的部分不变,小于等于阈值的部分取0
cv2.THRESH_TOZERO_INV	#大于阈值的部分取0,小于等于阈值的部分不变
cv2.THRESH_TRUNC	#大于阈值的部分取阈值,小于等于阈值的部分不变
'''

(2), case

The program looks like this:

import cv2  # 导入opencv库

if __name__ == '__main__':
    while True:
        img_g = cv2.imread('img/6.jpg', 0)    # 获取路径img/0.jpg的图像,图像类型为RGB图像

        t, img_bin = cv2.threshold(img_g, 180, 255, cv2.THRESH_BINARY)  # 二值化

        cv2.imshow("img_g", img_g)  # 显示RGB图像
        cv2.imshow("img_bin", img_bin)  # 显示二值化图像

        cv2.waitKey(1)  # 等待时间

The effect is as follows:
insert image description here

2. HSV image binarization

The following is the HSV image binarization function provided by opencv-python:

img_bin = cv2.inRange(img_hsv, lower, upper)	#低于lower、高于upper为0,中间为255

HSV image binarization is introduced in the third article, and it also has a case, in the color recognition part: OpenCV python (3) [image preprocessing: color space conversion] && color recognition
The effect is shown in the figure below:
insert image description here

2. Filtering operation

Filtering is an indispensable part of image preprocessing and noise reduction. It can remove most of the noise without affecting the main features of the original image, which determines the subsequent image quality and greatly reduces the complexity of subsequent image processing .

1. Mean filtering

Mean filtering is linear filtering. The principle of mean filtering is to take the average value of the surrounding pixels as the value of the middle point. If it is an edge point, it cannot satisfy the situation that there are pixels around it, then all the remaining pixels in the kernel will be used. Points are averaged as the value of this edge pixel . However, after traversing the entire image, the image will become very blurred , so the mean filtering method may lose too many original features of the image.
cv2.blur is the average filter function provided by opencv-python, as follows:

img_blur = cv2.blur(img, (x, x))		# 均值滤波

2. Median filtering

The principle of median filtering is to specify that the coordinates do not contain edge points, and take the median of all numbers in the kernel as the value of the intermediate point (the edge point processing here is just a method, which does not represent the method used by the function provided by OpenCV, there are also It is possible to use the method of mirroring, or use all the remaining pixels in the kernel to perform median filtering like mean filtering) .
The median filter function provided by cv2.medianBlur for opencv-python is as follows:

cv2.medianBlur(img, x)			# 中值滤波

3. Gaussian filter

Use Gaussian filtering to first confirm an odd-numbered kernel ( the size of this kernel needs to be odd, because only odd-numbered×odd-numbered kernels have a center point, which is a condition that must be satisfied by the principle of Gaussian filtering ), if it is not an odd-numbered kernel, an error will be reported directly. The error is as follows.

cv2.error: (-215:Assertion failed) ksize.width > 0 && ksize.width % 2 == 1 && ksize.height > 0 && ksize.height % 2 == 1 in function 'cv::createGaussianKernels'

The principle of Gaussian filtering is to multiply the array of the same size as the Gaussian kernel with the Gaussian kernel, and then add them together, and the obtained number is the value of the center point .
The Gaussian filter function provided by cv2.GaussianBlur for opencv-python is as follows:

cv2.GaussianBlur(img, (x, x), 0)	# 高斯滤波

4. Case

The specific code is as follows:

import cv2  # 导入opencv库

if __name__ == '__main__':
    while True:
        img_g = cv2.imread('img/6.jpg', 0)    # 获取路径img/0.jpg的图像,图像类型为RGB图像
        img_g = cv2.resize(img_g, (0, 0), fx=0.5, fy=0.5)  # 改变图像shape

        img_blur = cv2.blur(img_g, (5, 5))  # 均值滤波
        img_medianBlur = cv2.medianBlur(img_g, 5)			# 中值滤波
        img_GaussianBlur = cv2.GaussianBlur(img_g, (5, 5), 0)  # 高斯滤波

        t1, img_g_bin = cv2.threshold(img_g, 170, 255, cv2.THRESH_BINARY)  # 二值化
        t2, img_blur_bin = cv2.threshold(img_blur, 170, 255, cv2.THRESH_BINARY)  # 二值化
        t3, img_medianBlur_bin = cv2.threshold(img_medianBlur, 170, 255, cv2.THRESH_BINARY)  # 二值化
        t4, img_GaussianBlur_bin = cv2.threshold(img_GaussianBlur, 170, 255, cv2.THRESH_BINARY)  # 二值化

        cv2.imshow("img_g", img_g)  # 显示RGB图像
        cv2.imshow("img_g_bin", img_g_bin)  # 显示二值化图像
        cv2.imshow("img_blur", img_blur)    # 显示均值滤波后的图像
        cv2.imshow("img_blur_bin", img_blur_bin)    # 显示均值滤波后的二值化图像
        cv2.imshow("img_medianBlur", img_medianBlur)    # 显示中值滤波后的图像
        cv2.imshow("img_medianBlur_bin", img_medianBlur_bin)    # 显示中值滤波后的二值化图像
        cv2.imshow("img_GaussianBlur", img_GaussianBlur)    # 显示高斯滤波后的图像
        cv2.imshow("img_GaussianBlur_bin", img_GaussianBlur_bin)    # 显示高斯滤波后的二值化图像

        cv2.waitKey(1)  # 等待时间

The effect is as follows:
insert image description here
Regarding the choice of filtering method, my personal understanding is that the applicability and performance of Gaussian filtering are good, and Gaussian filtering can be used for general noise reduction requirements. As shown in the figure above, if you want to remove some large noise points, the effect of median filtering will be relatively poor. On the contrary, in this case, taking the average value will be more appropriate than taking the median value. On the contrary, when the noise points are very small, the median filter can have a good effect and will not cause any blurred consequences to the original image. On the contrary, the mean filter will eliminate many features of the image. If the features are not obvious or When the demand cannot have too blurry images, mean filtering cannot be used.

I am a student and I am currently studying. This article can be regarded as my study notes. Please correct me if I am wrong.

Guess you like

Origin blog.csdn.net/xztli/article/details/126175975