This is the sixth day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article will show how to use OpenCV in Python3 to implement image filtering:

Reference: Opencv4 official documentation: docs.opencv.org/4.2.0/

above

In communication engineering, filtering is a process of convolution processing of input signals.

Convolution (input signal, convolution template) The difference of convolution template/mask determines the different filtering methods, and thus produces the basic filtering methods such as high-pass, low-pass, band-pass and band-stop.

Image filtering, which is to suppress the noise of the target image under the condition of preserving the details of the image as far as possible, is an indispensable operation in image preprocessing, and its processing effect will directly affect the effectiveness and reliability of subsequent image processing and analysis.

Image filtering is equivalent to the convolution operation on each pixel of the image. The original function is the image pixel, the convolution kernel is the filter, and the calculation result is the filtered image.

OpenCV provides the function cv2.filter2d () to implement this convolution operation.

Common filters include:

  1. Linear filter
  • Mean filtering: Filtering result = the average of all pixels in the original pixel neighborhood
  • Gaussian filtering: Filtering result = weighted average of all pixels in the original pixel neighborhood
  1. Nonlinear filter
  • Median filtering: Filtering result = median of all pixels in the original pixel neighborhood (which can effectively remove salt-and-pepper noise)

Ordinary image convolution

Here is a 3X3 average filter, also called kernel:

Put the kernel on a pixel of the image, find the sum of pixels in the neighborhood (3×3), and then take the average, and replace the value of the pixel with the average. Repeat until every pixel of the image has been updated.

Code implementation:

import cv2
import numpy as np

img = cv2.imread('E:\\demo\\lena.jpg')

kernel = np.ones((3.3),np.float32)/9
dst = cv2.filter2D(img,-1,kernel)

cv2.imshow('original', img)
cv2.imshow('2D convolution', dst)

cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

The output is:

Average filtering

Median filter is a typical linear filtering algorithm, it is to point to in the target pixels in the image to a template, this template including its surrounding adjacent pixels (target pixel centered around eight pixels, constitute a filtering template, namely remove target pixel itself), then use the template to replace all the pixels in the average pixel values. However, its disadvantage is that it can filter out the edge information of the image while filtering out the noise, so as to lose many details. In addition, mean filtering can not remove noise points well. Especially salt and pepper noise.

In OpenCV, it is implemented by the function cv2.blur() or cv2.boxfilter ().

The 3×3 filter is shown in the figure:

The 5×5 filter is shown in the figure:

The code is as follows:

import cv2

img = cv2.imread('E:\\demo\\lena.jpg')

blur1 = cv2.blur(img,(3.3))
blur2 = cv2.blur(img,(5.5))

cv2.imshow('3*3 Mean filtering', blur1)
cv2.imshow('5*5 Mean filtering', blur2)

cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

The output is:

Gaussian filter

Gaussian filter is a linear smoothing filter, which has a good effect on removing gaussian noise.

The difference between gaussian filtering and mean filtering is that the template coefficient of the filter decreases with the increase of the distance from the template center.

(Gaussian filtering follows a two-dimensional Gaussian distribution).

In OpenCV, the function cv2.gaussianblur () is implemented. If necessary, the function cv2.getgaussiankernel () can be used to create a Gaussian kernel.

The code is as follows:

import cv2

img = cv2.imread('E:\\demo\\lena.jpg')

# 0GaussianBlur1 = cv2.GaussianBlur(img,(3.3),0)
GaussianBlur2 = cv2.GaussianBlur(img,(5.5),0)

cv2.imshow('3*3 Gaussian filtering', GaussianBlur1 )
cv2.imshow('5*5 Gaussian filtering', GaussianBlur2 )
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

The output is:

Median filtering

Replaces the pixel value of a pixel point with the median of the pixels near the pixel point’s neighborhood. Generally speaking, you take five pixels to the left and five to the right of this pixel, sort these pixels, and then you get a median value, and you replace the value of this pixel with the value of the middle size.

Median filter can effectively remove spots and salt and pepper noise. But its efficiency is low, and its operation time is more than five times that of mean filtering.

In OpenCV, median filtering can be implemented using the function cv2.medianblur ().

The code is as follows:

import cv2

img = cv2.imread('E:\\demo\\lena.jpg')

medianBlur1 = cv2.medianBlur(img,3)
medianBlur2 = cv2.medianBlur(img,5)

cv2.imshow('original', img)
cv2.imshow('3*3 median filtering', medianBlur1 )
cv2.imshow('5*5 median filtering', medianBlur2 )

cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

The output is:

A series of articles will follow this month,

Wonderful article, please pay attention.