Gaussian blur principle, algorithm

Reference article: Some knowledge points of image convolution and filtering

To learn Gaussian blur we first need to know some basic concepts:

**Basic concepts of linear filtering and convolution**

linear filtering can be said to be the most basic method of image processing, it can allow us to process the image, resulting in a lot of different effects. The practice is simple. First, we have a two-dimensional filter matrix (with a tall name called convolution kernel) and a two-dimensional image to be processed. Then, for each pixel of the image, the product of its neighborhood pixel and the corresponding element of the filter matrix is computed and then added up as the value of the pixel position. This completes the filtering process.

multiplying the image and the filter matrix by element multiplication and summing is equivalent to moving a two-dimensional function to all the positions of another two-dimensional function, which is called convolution or association correlation. The difference between convolution and co-correlation is that convolution requires a 180 rollover of the filter matrix, but if the matrix is symmetric, then there is no difference between the two.

Correlation and convolution can be said to be the most basic operation of image processing, but it is very useful. These two operations have two key features: they are linear and have translational invariance shift-invariant. Translational invariance means that we perform the same action at every point in the image. Linear refers to this operation is linear, that is, we use a linear combination of the neighborhood of each pixel to replace this pixel. These two properties make this operation very simple, because the linear operation is the simplest, and it is easier to do the same in all places.

In fact, in the field of signal processing, convolution has a wide range of meanings, and there are strict mathematical definitions, but this is not a concern here.

The 4-double loop requires 4 nested loops, so it's not fast unless we use very small convolution cores. 3x3 or 5x5 is commonly used here. Also, for filters, there are certain rules that require:

1) The size of the filter should be odd so that it has a center, such as 3x3,5x5 or 7x7. There is the center, also has the name of the radius, for example, the size of the kernel of the 5x5 radius is 2.

2) The sum of all elements of the filter matrix should be equal to 1, which is to ensure that the brightness of the image remains unchanged before and after filtering. Of course, this is not a mandatory requirement.

3) 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, conversely, if it is less than 1, then the resulting image will be dimmed. If and to 0, the image will not darken, but it will also be very dark.

4) for a filtered structure, there may be negative or greater than 255 values. In this case, we will truncate them directly between 0 and 255. For negative numbers, you can also take absolute values.

**The Magic convolution core**

It says that the filtering of images is the application of a small convolution core to the image, and what magic is there in this little convolution nucleus. Let's take a glimpse of some simple, but not simple, convolution-kernel magic.

**1, do not do anything**

Haha, what can we see? The filter does nothing, and the resulting image is the same as the original. Because only the value of the center point is 1. The weighted value of the neighboring point is 0, which has no effect on the filtered value.

let's move on to the next point.

**2. Image Sharpening Filter sharpness filter**

Image sharpening and edge detection is very much like, first find the edge, and then add the edge to the original image above, thus strengthening the edge of the image, so that the image looks sharper. The combination of the two operations is sharpening the filter, that is, on the basis of the edge detection filter, and then in the center of the position plus 1, so that the filtered image and the original image will have the same brightness, but will be more sharp.

If we increase the nuclear, we can get a finer sharpening effect.

In addition, the following filters will emphasize the edges more:

the main emphasis is on the details of the image. The simplest 3x3 sharpening filter is as follows:

as you can see, sharpening the filter is the difference between calculating the current point and the surrounding point, and then adding the difference to the original position.

**3 Edge detection Edges Detection**

We want to find the edge of the level: it should be noted that the element of the matrix here and is 0, so the filtered image will be very dark, only the edge of the place is bright.

Why is this filter able to find the horizontal edge? Because the convolution of this filter is equivalent to a discrete version of the derivative: you subtract the current pixel value from the previous pixel value, so you can get the difference or slope of the function in both positions. The following filter can find the vertical edge, where the pixel values on and below the pixels are used:

the next filter can find the edge of 45 degrees: take-2 not for what, just to let the matrix element and 0.

The filter below can then detect the edges in all directions:

to detect edges, we need to calculate the gradient in the direction of the image. Convolution the image with the following convolution core, it is possible. But in practice, this simple method will amplify the noise. Also, it should be noted that all the values of the matrix add up to 0.

**4. Embossed Embossing Filter**

The Emboss filter can give an image a 3D shadow effect. Just subtract the pixels from the other side of the pixel on the center side. At this point, the pixel value may be negative, we use negative numbers as shadows, positive numbers as light, and then we offset the resulting image by 128. At this point, most of the images become gray.

Here's a 45-degree emboss filter.

we just need to increase the filter to get a more dramatic effect.

The effect is pretty, like carving an image on a stone and illuminating it in one direction. Unlike the previous filter, it is asymmetric. In addition, it produces negative values, so we need to offset the results to get the range of image grayscale.

A: the original image. B: Sharpening. C: Edge detection. D: Emboss

**5. Motion Blur Motion Blur**

Motion blur can be achieved by blurring only in one direction, such as the 9x9 motion blur filter below. Note that the summation result is divided by 9.

the effect is as if the camera is moving from the upper-left corner to the lower-right corner.

** **After watching some fun filters we can go into the subject, first of all to see the mean value blur:

**Mean value blur box Filter (averaging)**

We can average the current pixel with the pixels of its neighborhood domain, then divide by 5, or take 0.2 of the value directly in the 5 places of the filter, such as:

As you can see, this blur is still relatively gentle, and we can make the filter bigger so that it becomes rough: pay attention to dividing it by one.

So, if you want a more blurry effect, increase the size of the filter. Or you can apply multiple blur to an image.

**Gaussian Blur**

In fact, the fuzzy filter is to the surrounding pixels weighted average processing, the mean fuzzy is very simple, the weights of the surrounding pixels are the same, so it is not very smooth. Gaussian Blur has this advantage, so it is widely used in image noise reduction. Especially before edge detection, it is used to remove details. So let's see how the Gaussian blur weights are allocated.

**weight of normal distribution**The normal distribution is obviously a desirable weight distribution pattern. On the graph, the normal distribution is a bell-shaped curve, the closer the center, the greater the value, the farther away from the center, the smaller the value. when we calculate the mean, we just need to use the "center point" as the origin, and the other points to assign weights according to their position on the normal curve, we can get a weighted average value.

**Gaussian function**The normal distribution above is one-dimensional and the images are two-dimensional, so we need a two-dimensional normal distribution. the density function of the normal distribution is called "Gaussian function" (Gaussian functions). Its one-dimensional form is:where μ is the mean value of X, Σ is the standard deviation of x. Because the center point is the origin point when calculating the mean, μ equals 0. namely: according to a Gaussian function, the Ivigos function can be deduced:with this function, you can calculate the weight of each point. assuming the coordinates of the center point are (0,0), the coordinates of the nearest 8 points are as follows:farther points and so on. Here is the Gaussian filter and smoothing effect of the 5*5:

Gaussian blur principle, algorithm