Article directory
learning target
Understand the types of noise in images
Learn about average filtering, Gaussian filtering, median filtering, etc.
Ability to use filters to process images
1. Noise in images
Because the processes of image collection, processing, and transmission are inevitably contaminated by noise, it hinders people's understanding and analysis of images. Common image noises include Gaussian noise, salt and pepper noise , etc.
1. Salt and pepper noise
Salt and pepper noise, also known as impulse noise, is a kind of noise often seen in images. It is a white or black point that appears randomly. It may be a black pixel in a bright
area or a white pixel in a dark area. (Or both).
Salt and pepper noise may be caused by sudden strong interference to the image signal, analog-to-digital converter or bit transmission errors, etc. For example, a failed sensor results in a minimum pixel value, and a saturated sensor results in a maximum pixel value.
2. Gaussian noise
Gaussian noise refers to a type of noise whose noise density function obeys Gaussian distribution. Due to the mathematical tractability of Gaussian noise in the spatial and frequency domains, this noise (also called normal noise) model is often used in practice. The probability density function of the Gaussian random variable z is given by the following formula:
where z represents the gray value, μ represents the mean or expected value of z, and δ represents the standard deviation of z. The square of the label difference δ2 is called the variance of z. The curve of the Gaussian function is shown in the figure.
2. Image smoothing operation
From the perspective of signal processing, image smoothing is to remove high-frequency information and retain low-frequency information. So we can implement low pass filtering on the image. Low-pass filtering can remove noise in the image and smooth the image.
According to the difference of filtering, it can be divided into mean filtering, Gaussian filtering, median filtering and bilateral filtering.
1. Mean filtering
Use mean filtering to filter out image noise. It also represents the coordinate group of the matrix sub-image window with the center at (x, y) point and size mxn. The mean filter can be expressed as:
completed by a normalized convolution box. It replaces the center element with the average of all pixels in the area covered by the convolution box.
For example, a 3X3 normalized averaging filter looks like this:
The advantages of mean filtering are simple algorithm and fast calculation speed. The disadvantage is that it removes a lot of details while denoising, making the image very blurry.
(1)API
cv.blur(src, ksize, anchor, borderType)
Parameters:
src: input image
ksize: convolution kernel size
anchor: default value (-1,-1), indicating the core center
borderType: border type
(2) Code examples
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# 1 图像读取
img = cv.imread('./image/dogsp.jpeg')
# 2 均值滤波
blur = cv.blur(img,(5,5))
# 3 图像显示
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img[:,:,::-1]),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur[:,:,::-1]),plt.title('均值滤波后结果')
plt.xticks([]), plt.yticks([])
plt.show()
2. Gaussian filter
Two-dimensional Gaussian is the basis for constructing Gaussian filters. Its probability distribution function is as follows: The
distribution of G(x, y) has the shape of a raised hat. δ here can be regarded as two values, one in the x direction The standard deviation δx, the other is the label difference δy in the y direction.
When the values of δx and δy are larger, the entire shape tends to be flat; when δx and δy, the entire shape is more protruding.
The normal distribution is a bell-shaped curve, with larger values closer to the center and smaller values further away from the center. When calculating the smoothing result, you only need to use the "center point" as the origin, and assign weights to other points according to their positions on the normal curve to obtain a weighted average.
Gaussian smoothing is very effective in removing Gaussian noise from images. <\font>
(1) Gaussian smoothing process
1) First determine the weight matrix
Assuming that the coordinates of the center point are (0, 0), then the coordinates of the 8 closest points to it are as follows: the
further points are the same, and so on.
In order to calculate the weight matrix, you need to set the value of δ. Assuming δ = 1.5, the weight matrix with a blur radius of 1 is as follows: the sum of the weights of
these 9 points is equal to 0.4787147. If you only calculate the weighted average of these 9 points, you must also Let the sum of their weights be equal to 1, so the above 9 values must be divided by 0.4787147 to obtain the final weight matrix.
2) Calculate Gaussian blur
With the weight matrix, the value of Gaussian blur can be calculated.
Assume that there are now 9 pixels, and the grayscale value (0-255) is as follows:
each point is multiplied by the corresponding weight value:
Finally, the matrix is obtained as:
Adding these 9 values is the Gaussian blur value of the center point.
Repeat this process for all points to obtain the Gaussian blurred image. If the original image is a color image, Gaussian smoothing can be performed on the three RGB channels.
(2)API
cv2.GaussianBlur(src,ksize,sigmaX,sigmay,borderType)
Parameters:
src: input image
ksize: the size of the Gaussian convolution kernel, note: the width and height of the convolution kernel should be odd numbers, and can be different
sigmaX: the standard deviation in the horizontal direction
sigmaY: the standard deviation in the vertical direction, the default value is 0, indicating the same as sigmaX
borderType: padding border type
(3) Code examples
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# 1 图像读取
img = cv.imread('./image/dogGasuss.jpeg')
# 2 高斯滤波
blur = cv.GaussianBlur(img,(3,3),1)
# 3 图像显示
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img[:,:,::-1]),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur[:,:,::-1]),plt.title('高斯滤波后结果')
plt.xticks([]), plt.yticks([])
plt.show()
3. Median filtering
Median filtering is a typical nonlinear filtering technology. The basic idea is to replace the gray value of a pixel with the median value of the neighborhood gray value of the pixel.
Median filtering is particularly useful for salt-and-pepper noise because it does not rely on values in the neighborhood that are very different from typical values.
(1)API
cv.medianBlur(src, ksize )
Parameters:
src: input image
ksize: size of convolution kernel
(2) Code examples
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# 1 图像读取
img = cv.imread('./image/dogsp.jpeg')
# 2 中值滤波
blur = cv.medianBlur(img,5)
# 3 图像展示
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img[:,:,::-1]),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur[:,:,::-1]),plt.title('中值滤波后结果')
plt.xticks([]), plt.yticks([])
plt.show()
Summarize
image noise
salt and pepper noise
Gaussian noise
Image smoothing
mean filter
The algorithm is simple and the calculation speed is fast, but a lot of useful information will be removed during the denoising process, making the image very blurry.
Gaussian filter
Gaussian noise can be removed, but the calculation amount is large and the process is complicated.
median filter
It is easier to use to remove salt and pepper noise.