# Hengyuan cloud_ Notes on [image smoothing]

Time：2022-5-6

Source of the article | Hengyuan cloud community (a shared computing platform focusing on AI industry:Hengyuan zhixiangyun)

Original author | inster

### Learning objectives

Understand the types of noise in the image
Understand the content of average filtering, Gaussian filtering, median filtering, etc
Be able to process images using filters

### 1 image noise

Because the process of image acquisition, processing and transmission will inevitably be polluted by noise, which hinders people’s understanding, analysis and processing of images. Common image noises include Gaussian noise, salt and pepper noise and so on.

#### 1.1 salt and pepper noise

Salt and pepper noise is also known asimpulse noise It is a kind of noise often seen in images. It is a random white or black dot, there may be black pixels in bright areas or white pixels in dark areas (or both). It may be caused by the strong interference of digital converter or analog signal. For example, a failed sensor causes the pixel value to be the minimum value, and a saturated sensor causes the pixel value to be the maximum value.

#### 1.2 Gaussian noise

Gaussian noise is a kind of noise whose noise density function obeys Gaussian distribution. Due to the mathematical ease of Gaussian noise in space and frequency domain, this noise (also known as normal noise) model is often used in practice. The probability density function of Gaussian random variable Z is given by the following formula:

$$p(z)=\frac{1}{\sqrt{2 \pi} \sigma} e^{\frac{-(z-\mu)^{2}}{2 \sigma^{2}}}2$$

Where Z represents the gray value, μ Represents the average or expected value of Z, σ Represents the standard deviation of Z. The square \ (\ sigma ^ {2} \) of the standard deviation is called the variance of Z. The curve of Gaussian function is shown in the figure.

### 2 Introduction to image smoothing

From the perspective of signal processing, image smoothing is to remove the high-frequency information and retain the low-frequency information. Therefore, we can implement low-pass filtering on the image.Low pass filteringIt can remove the noise in the image and smooth the image.

According to different filters, they can be divided intoMean filteringGaussian filteringmedian filtering Bilateral filtering

#### 2.1 mean filtering

The mean filtering template is used to filter the image noise. Let \ (s_x{y} \) indicate that the center is at the point (x, y) and the dimension is m × The coordinate group of the rectangular sub image window of n. The mean filter can be expressed as:
$$\hat{f}(x, y)=\frac{1}{m n} \sum_{(s, t) \in S_{x y}}$$
It is completed by a normalized convolution frame. It just replaces the central element with the average value of all pixels in the convolution frame coverage area.

For example, the 3×3 standardized average filter is as follows:
$$K=\frac{1}{9}\begin{bmatrix} \ 1 \ \ 1\ \ 1\ \ 1 \ \ 1\ \ 1\ \ 1 \ \ 1\ \ 1 \end{bmatrix}$$
The advantage of mean filtering is that the algorithm is simple and the calculation speed is fast. The disadvantage is that many details are removed while denoising, which makes the image blurred.
API:

cv.blur(src, ksize, anchor, borderType)

Parameters:

• SRC: input image
• Ksize: size of convolution kernel
• Anchor: the default value (- 1, – 1) indicates the core center
• Bordertype: boundary type

#### 2.2 Gaussian filtering

Two dimensional Gaussian filter is the basis of constructing Gaussian filter, and its probability distribution function is as follows:
$$G(x,y) = \frac{1}{2\pi \sigma^2}e^{-\frac{x^2+y^2}{2\sigma^2}}$$

\The distribution of (g (x, y) \) is the shape of a raised hat. there σ It can be regarded as two values, one is the standard deviation \ (\ sigma_x \) in the X direction, and the other is the standard deviation \ (\ sigma_y \) in the Y direction

When the values of \ (\ sigma_x \) and \ (\ sigma_y \) are larger, the whole shape tends to be flat; When \ (\ sigma_x \) and \ (\ sigma_y \), the whole shape is more prominent.
Normal distribution is a bell curve. The closer it is to the center, the greater the value. The farther it is away from the center, the smaller the value. When calculating the smoothing result, you only need to take 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.

Gaussian smoothing process:

• Firstly, the weight matrix is determined

Assuming that the coordinates of the center point are (0,0), the coordinates of the 8 closest points are as follows:
Further points, and so on.

In order to calculate the weight matrix, you need to set σ Value of. assume σ= 1.5, the weight matrix with fuzzy radius of 1 is as follows:
The total weight of these nine points is equal to 0.4787147. If only the weighted average of these nine points is calculated, the sum of their weights must be equal to 1. Therefore, the above nine values must be divided by 0.4787147 respectively to obtain the final weight matrix.

• Calculate Gaussian blur

With the weight matrix, the value of Gaussian blur can be calculated.
Assuming that there are 9 pixels, the gray value (0-255) is as follows:
Multiply each point by the corresponding weight value:
obtain

Add up these nine values to get the Gaussian blur value of the center point.

Repeat this process for all points to get the Gaussian blurred image. If the original image is a color image, Gaussian smoothing can be performed on the three RGB channels respectively.

API：

cv2.GaussianBlur(src,ksize,sigmaX,sigmay,borderType)

Parameters:

SRC: input image

• Ksize: the size of Gaussian convolution kernel. Note: the width and height of convolution kernel should be odd and can be different
• Sigma X: standard deviation in horizontal direction
• Sigmay: the standard deviation in the vertical direction. The default value is 0, which means it is the same as sigmax
• Bordertype: fill boundary type

Examples

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
#2 Gaussian filtering
blur = cv.GaussianBlur(img,(3,3),1)
#3 image display
plt.figure(figsize=(10,8),dpi=100)
plt. subplot(121),plt. imshow(img[:,:,::-1]),plt. Title ('original picture ')
plt.xticks([]), plt.yticks([])
plt. subplot(122),plt. imshow(blur[:,:,::-1]),plt. Title ('result after Gaussian filtering ')
plt.xticks([]), plt.yticks([])
plt.show()

#### 2.3 median filtering

Median filter is a typical methodNonlinear filtering technology, the basic idea is to useThe gray value of the pixel is replaced by the median of the gray value of the neighborhood of the pixel

Median filtering is particularly useful for salt and pepper noise because it does not depend on the values in the neighborhood that are very different from the typical values.
API：

cv.medianBlur(src, ksize )

Parameters:

• SRC: input image
• Ksize: size of convolution kernel

Example:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
#2-value filtering
blur = cv.medianBlur(img,5)
#3 image display
plt.figure(figsize=(10,8),dpi=100)
plt. subplot(121),plt. imshow(img[:,:,::-1]),plt. Title ('original picture ')
plt.xticks([]), plt.yticks([])
plt. subplot(122),plt. imshow(blur[:,:,::-1]),plt. Title ('result after median filtering ')
plt.xticks([]), plt.yticks([])
plt.show()

## [animation notes] rolling Division — find the maximum common divisor and the minimum common multiple

Recently, I touched up the C language and tried to write a calculation tool in combination with the recently learned operations research. On the way, I met a demand: score reduction. How to get the score in one step? The answer is to find the denominator and numeratorgreatest common divisor。 So how to calculate the […]