Gaussian blur and image convolution filtering

1. Image convolution filtering and Gaussian blur

1.1 Image convolution filtering kernel

For filtering, it can be said to be the most basic method of image processing, which can produce many different effects. In the following figure, the
Insert picture description here
matrices in the figure are the two-dimensional original pixel matrix, the two-dimensional image filter matrix ( also called the convolution kernel, the filter and the convolution kernel are the same concept below ), and the final filtered New pixel map. For each pixel of the original image, calculate the scores of its domain pixels and the corresponding elements of the filter matrix, and then add them up as the value of the current center pixel position.

It can be seen that an original image can be transformed into another image after being processed by a certain convolution kernel. For the filter, there are certain rules.

The size of the filter should be odd, so that it has a center, such as 3x3


, 5x5 or 7x7. There is a center, also known as a radius, for example, the radius of a 5x5 core is 2. ② The sum of all elements of the filter matrix should be equal to 1, this is to ensure that the brightness of the image remains unchanged before and after filtering. Of course, this is not a hard requirement. If the sum of all elements of the filter matrix is ​​greater than 1, then the filtered image will be brighter than the original image, otherwise, if less than 1, the resulting image will become darker. If the sum is 0, the image will not turn black, but it will also be very dark. ④ ** For the filtered structure, a negative number or a value greater than 255 may appear. In this case, we can simply truncate them between 0 and 255 . For negative numbers, you can also take absolute values.

1.2 Some usages of convolution kernel

Now that we know that filters can be used to manipulate the original image, are there any more specific examples? The relevant picture of the convolution kernel in the article comes from the network
1.2.1 Empty convolution kernel. As
Insert picture description here
you can see, this filter does nothing, and the resulting image is the same as the original image. Because only the value of the center point is 1. The weights of the neighboring points are all 0, which has no effect on the filtered values.

1.2.2 Image sharpening filter
Image sharpening is very similar to edge detection. First find the edge, and then add the edge to the original image. This strengthens the edge of the image and makes the image look sharper. The unity of the two operations is the sharpening filter, that is, on the basis of the edge detection filter, then add 1 to the center position, so that the filtered image will have the same brightness as the original image, but Will be sharper.
Insert picture description here
We can increase the kernel to get a finer sharpening effect.
Insert picture description here
1.2.3 Relief
Relief filter can give the image a 3D shadow effect. Just subtract the pixels on one side of the center from the pixels on the other side. At this time, the pixel value may be a negative number. We use negative numbers as shadows and positive numbers as light, and then we add an offset of 128 to the resulting image. At this time, most of the image becomes gray.
The following is a relief filter of 45 degrees.
Insert picture description here
As long as we increase the filter, we can get a more exaggerated effect.
Insert picture description here
1.2.4 Mean blur

We can average the current pixel and its four-neighbor pixels together, and then divide by 5, or directly take the value of 0.2 in 5 places of the filter, as shown in the figure below: we
Insert picture description here
can see that this blur is still relatively gentle Yes, we can make the filter bigger, and it will become rough: pay attention to divide and divide by 13.
Insert picture description here
You can see that the average blur can also make the picture blur, but its blur is not very smooth, not smooth The main reason is that the point far away from the center point has the same weight value as the one close to the center point. The resulting blur effect is the same
and you want to smooth it. Let the weight value follow the center point. The normal distribution (large in the middle, small at both ends) is realized by this feature.

1.3 Gaussian Blur

With the previous knowledge, we know that if we want to achieve the characteristics of Gaussian blur, we need to filter by constructing the corresponding weight matrix.

1.3.1 Normal
Insert picture description here
distribution In normal distribution, the closer to the center point, the larger the value, and the farther from the center, the smaller the value.
When calculating the average, we only need to use the "center point" as the origin, and other points are assigned weights according to their positions on the normal curve, and a weighted average can be obtained. Normal distribution is obviously a desirable weight distribution model.

1.3.2 Gaussian function

How to reflect the normal distribution? You need to use high function to achieve.
The normal distribution above is one-dimensional, and for the images are two-dimensional, so we need a two-dimensional normal distribution.
Insert picture description here
The density function of the normal distribution is called the "Gaussian function" (Gaussian function). Its one-dimensional form is:
Insert picture description here
where, μ is the mean of x, and σ is the variance of x. Since the center point is the origin when calculating the average value, μ is equal to 0.
Insert picture description here
According to the one-dimensional Gaussian function, a two-dimensional Gaussian function can be derived:
Insert picture description here
with this function, the weight of each point can be calculated.

1.3.3 Obtaining the weight matrix

Assuming that the coordinates of the center point are (0,0), then the coordinates of the 8 points closest to it are as follows:
Insert picture description here
points further away and so on.
In order to calculate the weight matrix, it is necessary to set the value of σ. Assuming σ = 1.5, the weight matrix with a blur radius of 1 is as follows: the
Insert picture description here
sum of the weights of these 9 points is equal to 0.4787147. If only the weighted average of these 9 points is calculated, the sum of their weights must also be equal to 1. The values ​​must be divided by 0.4787147 to obtain the final weight matrix.
Insert picture description here
The process of dividing by the total value is also called the "normalization problem". The
purpose is to make the total weight of the filter equal to 1. Otherwise, using a filter with a total value greater than 1 will make the image brighter, and a filter less than 1 will make the image darker.

1.3.4 Calculate the fuzzy value
With the weight matrix, you can calculate the Gaussian fuzzy value.
Assuming that there are 9 pixels, the gray value (0-255) is as follows:
Insert picture description here
each point is multiplied by its own weight value:
Insert picture description here
get
Insert picture description here
these 9 values ​​added together, which is the Gaussian blur value of the center point.
Repeat this process for all points to get a Gaussian blurred image. For color pictures, you need to do Gaussian blur for the three RGB channels.

1.3.5 Boundary value problem

Since it is processed according to the weight matrix, what
Insert picture description here
if a point is on the boundary and there are not enough points around it?

① Symmetric processing is to copy the existing points to the corresponding positions on the other side, and simulate the complete matrix.
② Assign 0, imagine that the image is a part of an infinitely long image, except for the part we give a value, the pixel values ​​of other parts are all 0.
③ Assign a boundary value, imagine that the image is unlimited, but the default value is not 0 but The value corresponding to the boundary point

Author: Hohohong
link: https: //www.jianshu.com/p/8d2d93c4229b
Source: Jane books
are copyrighted by the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

Published 19 original articles · praised 2 · visits 739

Guess you like

Origin blog.csdn.net/zan1763921822/article/details/104520962