Introduction to Gaussian Blur

Transfer from: 1. Gaussian blur algorithm: http://www.ruanyifeng.com/blog/2012/11/gaussian_blur.html

 

Usually, image processing software will provide "blur" (blur) filters to make the picture blurry.

 

There are many kinds of "blur" algorithms, one of which is called "Gaussian Blur" (Gaussian Blur). It uses the normal distribution (aka "Gaussian distribution") for image processing.

This article introduces the "Gaussian Blur" algorithm, you will see that this is a very simple and easy to understand algorithm. In essence, it is a data smoothing technology , suitable for many occasions, and image processing just provides an intuitive application example.

1. The principle of Gaussian blur

The so-called "blur" can be understood as taking the average value of surrounding pixels for each pixel.

In the above figure, 2 is the middle point, and the peripheral points are all 1.

"Middle point" takes the average value of "peripheral points" and it becomes 1. Numerically, this is a kind of "smoothing". In graphics, it is equivalent to producing a "blur" effect, and the "middle point" loses details.

Obviously, when calculating the average value, the larger the value range, the stronger the "blur effect".

The above is the original image, the blur radius is 3 pixels, and the blur radius is 10 pixels. The larger the blur radius, the more blurred the image. From a numerical point of view, the smoother the numerical value.

The next question is, since each point needs to take the average of the surrounding pixels, how should the weight be assigned?

If you use simple average, it is obviously not very reasonable, because the images are continuous, the closer the point is, the closer the relationship, and the farther away the relationship is, the more distant. Therefore, the weighted average is more reasonable, the closer the distance is, the greater the weight, and the farther the distance is, the smaller the weight.

Second, the weight of the normal distribution

The normal distribution is obviously a desirable weight distribution model.

On the graph, the normal distribution is a bell-shaped curve. The closer to the center, the larger the value, and the farther away from the center, the smaller the value.

When calculating the average value, we 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.

Three, 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 the "Gaussian function" . Its one-dimensional form is:

Among them, μ is the mean value of x, and σ is the variance of x. Because when calculating the average, the center point is the origin, so μ is equal to 0.

According to the one-dimensional Gaussian function, the two-dimensional Gaussian function can be derived:

With this function, the weight of each point can be calculated.

Fourth, the weight matrix

Assuming that the coordinates of the center point are (0,0), then the coordinates of the 8 nearest points are as follows:

And so on for further points.

In order to calculate the weight matrix, the value of σ needs to be set. Assuming σ=1.5, the weight matrix with blur radius of 1 is as follows:

The 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 be equal to 1. Therefore, the above 9 values ​​have to be divided by 0.4787147 to obtain the final weight matrix.

Five, calculate Gaussian blur

With the weight matrix, the value of Gaussian blur can be calculated.

Assuming there are 9 pixels, the gray value (0-255) is as follows:

Each point is multiplied by its own weight value:

get

Adding up these 9 values ​​is the Gaussian blur value at the center point.

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

Six, the processing of boundary points

What if a point is on the boundary and there are not enough points around it?

A workaround is to copy the existing points to the corresponding positions on the other side to simulate a complete matrix.

references

1. Gaussian blur algorithm: http://www.ruanyifeng.com/blog/2012/11/gaussian_blur.html

2.How to program a Gaussian Blur without using 3rd party libraries

 

Transfer from: 1. Gaussian blur algorithm: http://www.ruanyifeng.com/blog/2012/11/gaussian_blur.html

 

Usually, image processing software will provide "blur" (blur) filters to make the picture blurry.

 

There are many kinds of "blur" algorithms, one of which is called "Gaussian Blur" (Gaussian Blur). It uses the normal distribution (aka "Gaussian distribution") for image processing.

This article introduces the "Gaussian Blur" algorithm, you will see that this is a very simple and easy to understand algorithm. In essence, it is a data smoothing technology , suitable for many occasions, and image processing just provides an intuitive application example.

1. The principle of Gaussian blur

The so-called "blur" can be understood as taking the average value of surrounding pixels for each pixel.

In the above figure, 2 is the middle point, and the peripheral points are all 1.

"Middle point" takes the average value of "peripheral points" and it becomes 1. Numerically, this is a kind of "smoothing". In graphics, it is equivalent to producing a "blur" effect, and the "middle point" loses details.

Obviously, when calculating the average value, the larger the value range, the stronger the "blur effect".

The above is the original image, the blur radius is 3 pixels, and the blur radius is 10 pixels. The larger the blur radius, the more blurred the image. From a numerical point of view, the smoother the numerical value.

The next question is, since each point needs to take the average of the surrounding pixels, how should the weight be assigned?

If you use simple average, it is obviously not very reasonable, because the images are continuous, the closer the point is, the closer the relationship, and the farther away the relationship is, the more distant. Therefore, the weighted average is more reasonable, the closer the distance is, the greater the weight, and the farther the distance is, the smaller the weight.

Second, the weight of the normal distribution

The normal distribution is obviously a desirable weight distribution model.

On the graph, the normal distribution is a bell-shaped curve. The closer to the center, the larger the value, and the farther away from the center, the smaller the value.

When calculating the average value, we 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.

Three, 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 the "Gaussian function" . Its one-dimensional form is:

Among them, μ is the mean value of x, and σ is the variance of x. Because when calculating the average, the center point is the origin, so μ is equal to 0.

According to the one-dimensional Gaussian function, the two-dimensional Gaussian function can be derived:

With this function, the weight of each point can be calculated.

Fourth, the weight matrix

Assuming that the coordinates of the center point are (0,0), then the coordinates of the 8 nearest points are as follows:

And so on for further points.

In order to calculate the weight matrix, the value of σ needs to be set. Assuming σ=1.5, the weight matrix with blur radius of 1 is as follows:

The 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 be equal to 1. Therefore, the above 9 values ​​have to be divided by 0.4787147 to obtain the final weight matrix.

Five, calculate Gaussian blur

With the weight matrix, the value of Gaussian blur can be calculated.

Assuming there are 9 pixels, the gray value (0-255) is as follows:

Each point is multiplied by its own weight value:

get

Adding up these 9 values ​​is the Gaussian blur value at the center point.

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

Six, the processing of boundary points

What if a point is on the boundary and there are not enough points around it?

A workaround is to copy the existing points to the corresponding positions on the other side to simulate a complete matrix.

references

1. Gaussian blur algorithm: http://www.ruanyifeng.com/blog/2012/11/gaussian_blur.html

2.How to program a Gaussian Blur without using 3rd party libraries

 

Guess you like

Origin blog.csdn.net/lr_shadow/article/details/114936866