OpenCV (image processing) - based on python-filter (how to use low-pass and high-pass filters)

insert image description here

1. Concept introduction

Low-pass filtering : Low-pass filtering can remove noise from an image or smooth an image.
High-pass filtering : can help find the edges of the image.
Noise : That is, the negative effect on an image, the part that is too dark or too bright, and the value of an image that is lower or higher than a certain pixel point can be considered as noise.


Convolution kernel : the matrix used for filtering. The convolution kernel is generally an odd number, such as 3×3, 5×5, 7×7, etc.;
anchor point : the coordinate point in the middle of the convolution kernel.
The larger the convolution kernel, the better the convolution effect, but the amount of calculation will also increase accordingly.
Boundary expansion : When the convolution kernel is greater than 1, and no boundary expansion is performed, the output size is reduced accordingly. When the convolution kernel performs boundary expansion in a standard way, the output space size is equal to the input.
insert image description here
The bottom is the original image, the top is the output image, the gray is the convolution kernel, and the dotted line is the image expansion

2. Image convolution

filter2D()

dst = cv2.filter2D(src, ddepth, kernel, anchor, delta, borderType)
src: original image
ddepth: output image size, default is -1
kernel: convolution kernel (a matrix)
anchor: anchor point, default random Convolution kernel change
delta: Add a value after convolution, the default is 0
borderType: There is a mapping type, add a black border, default is not set

kernel: is a matrix
insert image description here

import cv2
import numpy as np

img = cv2.imread('./image/lena.jpg')

# 创建一个5*5的卷积核
kernel = np.ones((5, 5), np.float32) / 25
img2 = cv2.filter2D(img, -1, kernel)

# 展示图像
cv2.imshow('img', img)
cv2.imshow('img2', img2)

cv2.waitKey(0)

After image processing, it looks blurred and smoother
insert image description here

3. Low-pass filter

The filter2D interface requires us to define the convolution kernel by ourselves. How to set a suitable convolution kernel has also become a difficult problem. For this reason, OpenCV provides a series of filters, and each filter has its own dedicated convolution kernel. d greatly reduces the burden on the user.

3.1 Box filtering and mean filtering

boxFilter()

dst = cv2.boxFilter(src, ddepth, ksize , anchor, normalize, borderType)
src: input image
ddepth: output image size, default is -1
kernel: convolution kernel size (x, y)
anchor: anchor point, default Normalize as the convolution kernel changes
: Boolean type defaults to True; True: a is 1/W*H (mean filter), false: a=1
borderType: has a mapping type, plus a black border, not set by defaultinsert image description here

import cv2
import numpy as np

img = cv2.imread('./image/lena.jpg')

# 方盒滤波(当为True时)变成均值滤波,当为False时,就只加和不变化,超过255的结果设置为255
img2 = cv2.boxFilter(img, -1, (5, 5), normalize=True)
img3 = cv2.boxFilter(img, -1, (5, 5), normalize=False)
# 展示图像
cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('img3', img3)

cv2.waitKey(0)

insert image description here

blur()

When the parameter of the box filter is True, it is the mean filter, so this API is not used much.
dst = cv2.blur(scr, ksize, anchor, borderType)
scr: source image
kernel: convolution kernel size (x, y)
anchor: anchor point
borderType: mapping type, add a black border, not set by default

3.2 Gaussian filtering (Gaussian noise)

Applicable to images with Gaussian noise
dst = cv2.GaussianBlur(img, ksize, sigmaX, sigmaY, …)
img: input image
ksize: convolution kernel size
sigmaX: Indicates the standard deviation of the Gaussian kernel function in the X direction.
sigmaY: Indicates the standard deviation of the Gaussian kernel function in the Y direction.
Generally only need to look at the first three parameters

insert image description here

import cv2
import numpy as np

img = cv2.imread('./image/Gaussian.png')

# 高斯去噪
img2 = cv2.GaussianBlur(img, (3, 3), 0)

# 展示图像
cv2.imshow('img', img)
cv2.imshow('img2', img2)

cv2.waitKey(0)

insert image description here

3.3 Median filtering (pepper noise)

Denoising the pepper noise is obvious, take the middle value as the convolution result
dst = cv2.medianBlur(img, ksize)
img: input image
ksize: convolution kernel sizea number

import cv2
import numpy as np

img = cv2.imread('./image/median.png')

# 胡椒噪声
img2 = cv2.medianBlur(img, 5)

# 展示图像
cv2.imshow('img', img)
cv2.imshow('img2', img2)

cv2.waitKey(0)

3.4 Bilateral filtering

The main application scenario of bilateral filtering is video beauty
cv2.bilateralFilter(img, d, sigmaColor, sigmaSpace, …)
img: input image
d: diameter, distance from the center point of the convolution kernel, generally 5
sigmaColor: color space filter The sigma value. A larger value for this parameter indicates that a wider range of colors in the pixel neighborhood will be mixed together, resulting in a larger area of ​​semi-equal colors.
sigmaSpace: The sigma value of the filter in the sigmaSpace coordinate space, and the label variance of the coordinate space. The larger the value, the more distant pixels will affect each other, so that the larger area is similar enough to get the same color. When d>0, d specifies the neighborhood size and has nothing to do with sigmaSpace. Otherwise, d is proportional to sigmaSpace.

The role of bilateral filtering: Image denoising and edge protection have a greater impact on the results of correlation analysis. For images with strong cracks and less noise, the degree of denoising can be enlarged, which is beneficial to future correlation analysis. The result is less noise. For images with less concentrated noise and images with more details, the effect of edge preservation is increased, allowing correlation analysis and subsequent further structural processing and denoising.

import cv2
import numpy as np

img = cv2.imread('./image/lena.jpg')


# 双边滤波
img2 = cv2.bilateralFilter(img, 5, 20, 50)

# 展示图像
cv2.imshow('img', img)
cv2.imshow('img2', img2)

cv2.waitKey(0)

insert image description here

4. High-pass filter

Allows passes above a certain value and blocks filters below that value. The main function is to preserve the edges. Common high-pass filters are Sobel (Sobel), Scharr (Scharr), and Laplacian (Laplace).

4.1 Sobel (Sobel) (Gaussian)

It can only be derived once in the x direction or y direction, and then add the results.
dst1 = cv2.Sobel(src, ddepth, dx, dy, ksize = 3, scale = 1, delta = 0, borderType = BORDER_DEFAULT )
src: input original image
ddepth: bit depth, default is -1
dx, dy: only Choose a direction, either 0, 1, or 1, 0
ksize: convolution kernel size, the default is 3, when -1 is Shar
scale: scale size, generally use the default value
delta: offset, generally use Default value
borderType: Border extension type, generally use the default value

The size of the convolution kernel can be changed

import cv2
import numpy as np

img = cv2.imread('./image/lena.jpg')


# 索贝尔
dx = cv2.Sobel(img, -1, 1, 0, ksize=3)
dy = cv2.Sobel(img, -1, 0, 1, ksize=3)

# dst = dx+dy
dst = cv2.add(dx,dy)
# 展示图像
cv2.imshow('img', img)
cv2.imshow('dx', dx)
cv2.imshow('dy', dy)
cv2.imshow('dst', dst)

cv2.waitKey(0)

insert image description here
The edges of an image are well segmented.

4.2 Scharr (Scharr)

Similar to Sobel, except that the ksize value used is different, Scharr cannot change the size of the convolution kernel, it can only be 3*3. Similarly, only edges in one direction can be found.

cv2.Scharr(src, ddepth, dx, dy, scale = 1, delta = 0, borderType = BORDER_DEFAULT).
src: input original image
ddepth: bit depth, default is -1
dx, dy: only one direction can be selected or 0, 1, or 1, 0
scale: zoom size, generally use the default value
delta: offset, generally use the default value
borderType: boundary expansion type, generally use the default value

import cv2
import numpy as np

img = cv2.imread('./image/lena.jpg')


# # 索贝尔,当ksize=-1时,就是沙尔
# dx = cv2.Sobel(img, -1, 1, 0, ksize=-1)
# dy = cv2.Sobel(img, -1, 0, 1, ksize=-1)

dx = cv2.Scharr(img, -1, 1, 0)
dy = cv2.Scharr(img, -1, 0, 1)

# dst = dx+dy
dst = cv2.add(dx,dy)
# 展示图像
cv2.imshow('img', img)
cv2.imshow('dx', dx)
cv2.imshow('dy', dy)
cv2.imshow('dst', dst)

cv2.waitKey(0)

insert image description here

4.3 Laplacian (Laplace)

Laplacian can find edges in two directions at the same time, but it is sensitive to noise. Generally, it needs to denoise first and then call Laplacian.

dst = cv2.Laplacian(src, ddepth, ksize = 1 ,scale = 1, borderType = BORDER_DEFAULT)
src: input original image
ddepth: bit depth, default is -1
ksize: convolution kernel size, default is 1
scale: scaling size , generally use the default value
delta: offset, generally use the default value
borderType: border extension type, generally use the default value

insert image description here
The result of the convolution kernel size is 5*5

import cv2
import numpy as np

img = cv2.imread('./image/lena.jpg')


# # 索贝尔,当ksize=-1时,就是沙尔
# dx = cv2.Sobel(img, -1, 1, 0, ksize=-1)
# dy = cv2.Sobel(img, -1, 0, 1, ksize=-1)

# dx = cv2.Scharr(img, -1, 1, 0)
# dy = cv2.Scharr(img, -1, 0, 1)

dst = cv2.Laplacian(img, -1, ksize = 5)

# dst = dx+dy
# dst = cv2.add(dx,dy)
# 展示图像
cv2.imshow('img', img)
# cv2.imshow('dx', dx)
# cv2.imshow('dy', dy)
cv2.imshow('dst', dst)

cv2.waitKey(0)

4.4 Canny

Using 5*5 Gaussian filtering to eliminate noise, you can calculate the edges (0, 45, 90, 135) in the four directions of the image, take the local maximum value, and add a threshold calculation. Above the threshold we think it is an edge, below the threshold is not an edge, obviously A is an edge, if, but B and C are between the maximum and minimum values, BC is neither an edge nor an edge, but C and A are on a straight line , so C is also an edge.

insert image description here

dst = cv2.Canny(img, minVal, maxVal)
img: original image
minVal: minimum threshold
maxVal: maximum threshold
below the minimum threshold is not an edge, above the maximum threshold is an edge.

import cv2
import numpy as np

img = cv2.imread('./image/lena.jpg')


# canny
dst = cv2.Canny(img, 100, 200)

cv2.imshow('img',img)
cv2.imshow('dst',dst)

cv2.waitKey(0)

insert image description here

The above is the basic introduction about the filter. Readers need to learn the details by themselves. If you have any questions, please discuss them in the comment area.

Guess you like

Origin blog.csdn.net/weixin_45153969/article/details/131060924