“This is the 10th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Image filtering
Image processing technology is the core of computer vision project, usually is the key tool in computer vision project, can be used to complete a variety of computer vision tasks. In this paper, image filtering, a common image processing technique in computer vision projects, will be introduced. First, how to blur and sharpen the image, and then apply the custom core. In addition, some common cores for performing other image processing functions will be described.
Apply filter (convolution kernel or kernel for short)
OpenCV provides the cv2.filter2d () function to apply any kernel to the image, convolving the image with the provided kernel. To use this function, you first need to build the core to be used:
Use 5 x 5 cores
kernel_averaging_5_5 = np.array([[0.04.0.04.0.04.0.04.0.04], [0.04.0.04.0.04.0.04.0.04], [0.04.0.04.0.04.0.04.0.04], [0.04.0.04.0.04.0.04.0.04], [0.04.0.04.0.04.0.04.0.04]])
Copy the code
The above example creates a 5 x 5 mean convolution kernel. You can also create a convolution kernel by:
kernel_averaging_5_5 = np.ones((5.5), np.float32) / 25
Copy the code
The core will then be applied to the source image using the cv2.filter2d () function:
smooth_image_f2D = cv2.filter2D(image, -1, kernel_averaging_5_5)
Copy the code
The above method can apply any kernel to the image. In the example, an average convolution kernel is created to smooth the image. Alternatively, we can use OpenCV’s built-in functions to perform image smoothing (also known as image blur) without creating a core.
The image smooth
Smoothing techniques are commonly used to reduce noise, but they can also be used to reduce pixelation in low-resolution images.
Average filtering
You can use cv2.blur() or cv2.boxfilter () to perform mean filtering by convolving the image with the kernel. You can use cv2.boxfilter () without normalizing and just take the average of all the pixels under the kernel and replace the center element with this average. You can control the size of the core and the location of the anchor point (which by default is in the center of the core). When the normalize argument of cv2.boxfilter () is True, the two functions are completely equivalent. Both functions use the following kernel to smooth the image:
Cv2. BoxFilter () function:
In the case of cv2.blur() : ⋅ksize. Width \alpha =ksize. Width \cdot ksize. Cv2.blur () is used with normalized boxFilter() :
The following two lines of code are equivalent
smooth_image_b = cv2.blur(image, (10.10))
smooth_image_bfi = cv2.boxFilter(image, -1, (10.10), normalize=True)
Copy the code
The mean filtered image is shown as follows:
Gaussian filter
OpenCV provides the cv2.gaussianblur () function for Gaussian filtering, which uses the Gaussian check image for blur processing. The following parameters can be used to control the Gaussian kernel: Ksize (the size of the kernel), sigmaX (the standard deviation of the X direction of the Gaussian kernel), and sigmaY (the standard deviation of the y direction of the Gaussian kernel). To obtain the applied Gaussian kernel, you can use the cv2.getGaussiankernel () function to construct the Gaussian kernel:
# (9, 9) indicates that the length and width of the Gaussian matrix are both 5 and the standard deviation is 0
smooth_image_gb = cv2.GaussianBlur(image, (9.9), 0)
The standard deviation is 0.3
smooth_image_gb_2 = cv2.GaussianBlur(image, (9.9), 0.3)
# Build a Gaussian kernel
print(cv2.getGaussianKernel(9.0))
Copy the code
The image after Gaussian filtering is shown as follows:
Median filtering
OpenCV provides the cv2.medianblur () function for median filtering, which uses median check to blur the image:
smooth_image_mb = cv2.medianBlur(image, 9)
smooth_image_mb_2 = cv2.medianBlur(image, 3)
Copy the code
This filter can be used to reduce the salt-and-pepper noise in the image, and the effect is as follows:
Bilateral filtering
The cv2.bilateralFilter() function applied to the input image can perform bilateral filtering. Unlike all of the above smoothing filters, which tend to be globally smooth, this function can be used to reduce noise while keeping edges sharp:
smooth_image_bf = cv2.bilateralFilter(image, 5, 10, 10)
Copy the code
Bilateral filtering effect is shown in the figure below:
Image sharpening
A simple way to sharpen the edges of an image is to perform unsharp masking, which subtracts an unsharpened or smoothed version of the image from the original. In the following example, a Gaussian smoothing filter is first applied and then the resulting image is subtracted from the original image:
Smoothed = cv2.GaussianBlur(img, (9, 9), 10) unsharped = cv2. AddWeighted (img, 1.5, smoothed, -0.5, 0)Copy the code
Another way is to sharpen the edges with a specific core and apply the cv2.filter2d () function:
kernel_sharpen_1 = np.array([[0, -1.0], [...1.5, -1],
[0, -1.0]])
kernel_sharpen_2 = np.array([[-1, -1, -1], [...1.9, -1], [...1, -1, -1]])
kernel_sharpen_3 = np.array([[1.1.1],
[1, -7.1],
[1.1.1]])
kernel_sharpen_4 = np.array([[-1, -1, -1, -1, -1], [...1.2.2.2, -1], [...1.2.8.2, -1], [...1.2.2.2, -1], [...1, -1, -1, -1, -1/]])8.0
sharp_image_1 = cv2.filter2D(image, -1, kernel_sharpen_1)
sharp_image_2 = cv2.filter2D(image, -1, kernel_sharpen_2)
sharp_image_3 = cv2.filter2D(image, -1, kernel_sharpen_3)
sharp_image_4 = cv2.filter2D(image, -1, kernel_sharpen_4)
Copy the code
The image output after sharpening is as follows:
Commonly used filters in image processing
You can also define some generic cores for different purposes, such as edge detection, smoothing, sharpening, or emburring. After defining the kernel, you can use the cv2.filter2d () function:
image = cv2.imread('sigonghuiye.jpeg')
kernel_identity = np.array([[0.0.0],
[0.1.0],
[0.0.0]])
# Edge detection
kernel_edge_detection_1 = np.array([[1.0, -1],
[0.0.0], [...1.0.1]])
kernel_edge_detection_2 = np.array([[0.1.0],
[1, -4.1],
[0.1.0]])
kernel_edge_detection_3 = np.array([[-1, -1, -1], [...1.8, -1], [...1, -1, -1]])
# sharpening
kernel_sharpen = np.array([[0, -1.0], [...1.5, -1],
[0, -1.0]])
kernel_unsharp_masking = -1 / 256 * np.array([[1.4.6.4.1],
[4.16.24.16.4],
[6.24, -476.24.6],
[4.16.24.16.4],
[1.4.6.4.1]])
# fuzzy
kernel_blur = 1 / 9 * np.array([[1.1.1],
[1.1.1],
[1.1.1]])
gaussian_blur = 1 / 16 * np.array([[1.2.1],
[2.4.2],
[1.2.1]])
# embossed
kernel_emboss = np.array([[-2, -1.0], [...1.1.1],
[0.1.2]])
# Edge detection
sobel_x_kernel = np.array([[1.0, -1],
[2.0, -2],
[1.0, -1]])
sobel_y_kernel = np.array([[1.2.1],
[0.0.0], [...1, -2, -1]])
outline_kernel = np.array([[-1, -1, -1], [...1.8, -1], [...1, -1, -1]])
# Apply the convolution kernel
original_image = cv2.filter2D(image, -1, kernel_identity)
edge_image_1 = cv2.filter2D(image, -1, kernel_edge_detection_1)
edge_image_2 = cv2.filter2D(image, -1, kernel_edge_detection_2)
edge_image_3 = cv2.filter2D(image, -1, kernel_edge_detection_3)
sharpen_image = cv2.filter2D(image, -1, kernel_sharpen)
unsharp_masking_image = cv2.filter2D(image, -1, kernel_unsharp_masking)
blur_image = cv2.filter2D(image, -1, kernel_blur)
gaussian_blur_image = cv2.filter2D(image, -1, gaussian_blur)
emboss_image = cv2.filter2D(image, -1, kernel_emboss)
sobel_x_image = cv2.filter2D(image, -1, sobel_x_kernel)
sobel_y_image = cv2.filter2D(image, -1, sobel_y_kernel)
outline_image = cv2.filter2D(image, -1, outline_kernel)
Copy the code
A link to the
OpenCV image geometric transformation